package response_test import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" errs "gateway/internal/library/errors" "gateway/internal/library/errors/code" "gateway/internal/response" "gateway/internal/types" ) var errb = errs.For(code.Facade) func TestWriteSuccess(t *testing.T) { t.Parallel() w := httptest.NewRecorder() response.Write(context.Background(), w, map[string]string{"pong": "ok"}, nil) if w.Code != http.StatusOK { t.Fatalf("status = %d, want 200", w.Code) } var body types.Status if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Fatal(err) } if body.Code != 0 || body.Message != code.DefaultSuccessMessage { t.Fatalf("envelope = %+v", body) } if body.Error != nil { t.Fatal("success response must not include error") } } func TestWriteBusinessError(t *testing.T) { t.Parallel() w := httptest.NewRecorder() e := errb.ResNotFound("user", "1") response.Write(context.Background(), w, nil, e) if w.Code != http.StatusNotFound { t.Fatalf("status = %d, want 404", w.Code) } var body types.Status if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Fatal(err) } if body.Code != int64(e.Code()) { t.Fatalf("code = %d, want %d", body.Code, e.Code()) } if body.Data != nil { t.Fatal("error response should not include data") } }