test: expand handler tests to 53.4% coverage
Some checks failed
CI / lint-test (push) Failing after 1s
Some checks failed
CI / lint-test (push) Failing after 1s
This commit is contained in:
@@ -2,9 +2,12 @@ package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func TestTaskHandler_Create_InvalidBody(t *testing.T) {
|
||||
@@ -31,3 +34,115 @@ func TestTaskHandler_Create_EmptyTitle(t *testing.T) {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// helper: add chi URL param to request
|
||||
func withChiParam(r *http.Request, key, val string) *http.Request {
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add(key, val)
|
||||
return r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
|
||||
}
|
||||
|
||||
func TestTaskHandler_Get_NoChiParam(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/tasks/1", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Get(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Get_InvalidID(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/tasks/abc", nil)
|
||||
req = withChiParam(req, "id", "abc")
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Get(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Update_NoChiParam(t *testing.T) {
|
||||
req := httptest.NewRequest("PUT", "/tasks/1", bytes.NewBufferString("{}"))
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Update(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Update_InvalidBody(t *testing.T) {
|
||||
req := httptest.NewRequest("PUT", "/tasks/1", bytes.NewBufferString("bad json"))
|
||||
req = withChiParam(req, "id", "1")
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Update(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Delete_NoChiParam(t *testing.T) {
|
||||
req := httptest.NewRequest("DELETE", "/tasks/1", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Delete(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Delete_InvalidID(t *testing.T) {
|
||||
req := httptest.NewRequest("DELETE", "/tasks/abc", nil)
|
||||
req = withChiParam(req, "id", "abc")
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Delete(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Complete_NoChiParam(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/tasks/1/complete", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Complete(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Complete_InvalidID(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/tasks/abc/complete", nil)
|
||||
req = withChiParam(req, "id", "abc")
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Complete(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Uncomplete_NoChiParam(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/tasks/1/uncomplete", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Uncomplete(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskHandler_Uncomplete_InvalidID(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/tasks/abc/uncomplete", nil)
|
||||
req = withChiParam(req, "id", "abc")
|
||||
rr := httptest.NewRecorder()
|
||||
h := &TaskHandler{taskService: nil}
|
||||
h.Uncomplete(rr, req)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user