diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-27 11:45:00 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-27 11:45:00 +0800 |
| commit | c41a8f36676bca96b3a3c4af7ca6ab96f2ad9fc1 (patch) | |
| tree | 143b7388a766c82a45890e37f1c256dbb0ff67f9 /context_test.go | |
| parent | bb58f6cd1d9cce5b12e2f9a30b1c9a6aaefdd54c (diff) | |
refactor: use standard Go tests
Diffstat (limited to 'context_test.go')
| -rw-r--r-- | context_test.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/context_test.go b/context_test.go new file mode 100644 index 0000000..560d9f5 --- /dev/null +++ b/context_test.go @@ -0,0 +1,64 @@ +package growel + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestContextPlain(t *testing.T) { + w := httptest.NewRecorder() + c := &Context{W: w} + + c.Plain(201, "ok") + + if w.Code != 201 { + t.Fatalf("expected 201, got %d", w.Code) + } + if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "text/plain") { + t.Fatalf("wrong content-type: %s", ct) + } + if w.Body.String() != "ok" { + t.Fatalf("wrong body: %s", w.Body.String()) + } +} + +func TestContextJSON(t *testing.T) { + w := httptest.NewRecorder() + c := &Context{W: w} + + c.JSON(200, map[string]string{"x": "y"}) + + if w.Code != 200 { + t.Fatalf("expected 200, got %d", w.Code) + } + if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") { + t.Fatalf("unexpected content-type: %s", ct) + } + if !strings.Contains(w.Body.String(), `"x":"y"`) { + t.Fatalf("wrong json: %s", w.Body.String()) + } +} + +func TestContextError(t *testing.T) { + w := httptest.NewRecorder() + c := &Context{W: w} + + c.Error(http.StatusBadRequest, "bad input") + + if w.Code != 400 { + t.Fatalf("expected 400, got %d", w.Code) + } + + body := w.Body.String() + if !strings.Contains(body, `"error":"Bad Request"`) { + t.Fatalf("missing human readable status: %s", body) + } + if !strings.Contains(body, `"message":"bad input"`) { + t.Fatalf("missing message: %s", body) + } + if !strings.Contains(body, `"status":"400"`) { + t.Fatalf("missing status field: %s", body) + } +} |
