149 lines
4.0 KiB
Go
149 lines
4.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func TestTaskHandler_Create_InvalidBody(t *testing.T) {
|
|
req := httptest.NewRequest("POST", "/tasks", bytes.NewBufferString("not json"))
|
|
rr := httptest.NewRecorder()
|
|
|
|
h := &TaskHandler{taskService: nil}
|
|
h.Create(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestTaskHandler_Create_EmptyTitle(t *testing.T) {
|
|
body := `{"title":""}`
|
|
req := httptest.NewRequest("POST", "/tasks", bytes.NewBufferString(body))
|
|
rr := httptest.NewRecorder()
|
|
|
|
h := &TaskHandler{taskService: nil}
|
|
h.Create(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
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)
|
|
}
|
|
}
|