Skip to content
Snippets Groups Projects
Commit 21a324d2 authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

Added some unit tests for handlers

parent 21730bac
No related branches found
No related tags found
No related merge requests found
package handler
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateCampaignBadRequest(t *testing.T) {
rr := httptest.NewRecorder()
//No campaign in the body of the request -> nil
req := httptest.NewRequest(http.MethodPost, "/campaigns", nil)
handler := http.HandlerFunc(CreateCampaign)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler did not recognize bad request")
}
}
package handler
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHealth(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/health", nil)
handler := http.HandlerFunc(Health)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := `{"alive": true}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment