Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCodedErrorLegacyEnvelope(t *testing.T) {
|
||||
t.Parallel()
|
||||
rec := httptest.NewRecorder()
|
||||
CodedError(rec, http.StatusUnauthorized, "unauthorized", "Unauthorized")
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
var body struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("json: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if body.Error.Code != "unauthorized" || body.Error.Message != "Unauthorized" {
|
||||
t.Fatalf("got %+v", body.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldErrorAdditiveShape(t *testing.T) {
|
||||
t.Parallel()
|
||||
rec := httptest.NewRecorder()
|
||||
FieldError(rec, http.StatusUnauthorized, "invalid credentials", "invalid_credentials", map[string]string{
|
||||
"email": "invalid credentials",
|
||||
"password": "invalid credentials",
|
||||
})
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
var body struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
Fields map[string]string `json:"fields"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("json: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if body.Error != "invalid credentials" || body.Code != "invalid_credentials" {
|
||||
t.Fatalf("got error=%q code=%q", body.Error, body.Code)
|
||||
}
|
||||
if body.Fields["email"] != "invalid credentials" || body.Fields["password"] != "invalid credentials" {
|
||||
t.Fatalf("fields=%v", body.Fields)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOKLegacyDataMetaEnvelope(t *testing.T) {
|
||||
t.Parallel()
|
||||
rec := httptest.NewRecorder()
|
||||
OK(rec, http.StatusOK, map[string]any{"id": "x"}, map[string]any{"page": 1, "limit": 25, "total": 10})
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := body["data"].(map[string]any)
|
||||
meta, _ := body["meta"].(map[string]any)
|
||||
if data["id"] != "x" {
|
||||
t.Fatalf("data=%v", data)
|
||||
}
|
||||
if meta["page"].(float64) != 1 || meta["total"].(float64) != 10 {
|
||||
t.Fatalf("meta=%v", meta)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user