Files
palette/internal/api/issue105_errorcode_test.go
poslop 9df6224a27
CI / test (push) Successful in 23s
CI / docker (push) Skipped
Result box: color-code by status + friendly error messages from error codes (#105)
- Backend create/can validation paths emit machine-readable error codes
  (slug_taken, slug_invalid, content_empty, content_too_large,
  expiry_invalid, rate_limited, ...) alongside the human message
- new.html JS maps codes to plain-language guidance with generic fallback
- Result card colored via --ok/--err/--warn left border (result-ok/err/warn)
- docs/API.md error section documents the code field
- Tests assert the code on every validation path
2026-09-09 17:28:25 -05:00

124 lines
3.8 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// #105: every validation path on create returns a machine-readable `code`
// alongside the human `error` message, so the new-page JS can map known
// codes to plain-language guidance.
func decodeErr(t *testing.T, rec *httptest.ResponseRecorder) (status int, errMsg, code string) {
t.Helper()
var got struct {
Error string `json:"error"`
Code string `json:"code"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("bad json: %v (%s)", err, rec.Body.String())
}
return rec.Code, got.Error, got.Code
}
func postCreate(h http.Handler, body string) *httptest.ResponseRecorder {
req := httptest.NewRequest("POST", "/api/pastes", strings.NewReader(body))
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func TestErrorCodeContentEmpty(t *testing.T) {
h := testServer(t).routes()
_, _, code := decodeErr(t, postCreate(h, `{"content":" "}`))
if code != "content_empty" {
t.Fatalf("code = %q, want content_empty", code)
}
}
func TestErrorCodeContentTooLarge(t *testing.T) {
h := testServer(t).routes()
big := strings.Repeat("x", 6*1024*1024) // over the 5MB test cap
_, _, code := decodeErr(t, postCreate(h, `{"content":"`+big+`"}`))
if code != "content_too_large" {
t.Fatalf("code = %q, want content_too_large", code)
}
}
func TestErrorCodeSlugTaken(t *testing.T) {
h := testServer(t).routes()
if rec := postCreate(h, `{"content":"a","custom_slug":"taken-slug"}`); rec.Code != 201 {
t.Fatalf("seed create: %d %s", rec.Code, rec.Body.String())
}
_, _, code := decodeErr(t, postCreate(h, `{"content":"b","custom_slug":"taken-slug"}`))
if code != "slug_taken" {
t.Fatalf("code = %q, want slug_taken", code)
}
}
func TestErrorCodeSlugInvalid(t *testing.T) {
h := testServer(t).routes()
_, _, code := decodeErr(t, postCreate(h, `{"content":"a","custom_slug":"bad slug!"}`))
if code != "slug_invalid" {
t.Fatalf("code = %q, want slug_invalid", code)
}
}
func TestErrorCodeExpiryInvalid(t *testing.T) {
h := testServer(t).routes()
_, _, code := decodeErr(t, postCreate(h, `{"content":"a","expires_in":"2s"}`))
if code != "expiry_invalid" {
t.Fatalf("code = %q, want expiry_invalid", code)
}
}
func TestErrorCodeRateLimited(t *testing.T) {
h := testServer(t).routes()
var code string
for i := 0; i < 20; i++ {
_, _, code = decodeErr(t, postCreate(h, `{"content":"a"}`))
if code == "rate_limited" {
return
}
}
t.Fatalf("never got rate_limited, last code = %q", code)
}
func TestErrorCodeCanValidation(t *testing.T) {
h := testServer(t).routes()
post := func(fields map[string]string) *httptest.ResponseRecorder {
var b strings.Builder
for k, v := range fields {
b.WriteString("--B\r\nContent-Disposition: form-data; name=\"" + k + "\"\r\n\r\n" + v + "\r\n")
}
b.WriteString("--B--\r\n")
req := httptest.NewRequest("POST", "/api/pastes/can", strings.NewReader(b.String()))
req.Header.Set("Content-Type", "multipart/form-data; boundary=B")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
// bad visibility
if _, _, code := decodeErr(t, post(map[string]string{"title": "x", "visibility": "nope"})); code != "invalid_visibility" {
t.Fatalf("visibility code = %q, want invalid_visibility", code)
}
// invalid expiry
if _, _, code := decodeErr(t, post(map[string]string{"title": "x", "expires_in": "1s"})); code != "expiry_invalid" {
t.Fatalf("expiry code = %q, want expiry_invalid", code)
}
// slug taken on can create
if rec := postCreate(h, `{"content":"a","custom_slug":"can-slug"}`); rec.Code != 201 {
t.Fatalf("seed create: %d", rec.Code)
}
if _, _, code := decodeErr(t, post(map[string]string{"title": "x", "custom_slug": "can-slug", "json_items": `[{"title":"a","content":"b"}]`})); code != "slug_taken" {
t.Fatalf("can slug code = %q, want slug_taken", code)
}
}