Files
GovAI/server/internal/response/response_test.go
T
selfrelease 65dc805eb5 feat: 系统优化 - ESLint、Tailwind、前端健壮性、后端工程化、运维可观测性
- 前端: ESLint+Prettier配置、Tailwind v4配置、ErrorBoundary、全局AuthLoader优化、ReactQuery分层
- 后端: MinIO凭证移除、Docker统一为govai品牌、zerolog日志封装、错误码枚举、文件上传校验、单元测试(13项全通过)
- 运维: 健康检查增强(PG/Redis ping)、Prometheus指标(/metrics端点)、多租户tenant包、RateLimit nil防御
- 移动: citation_prompt.txt → internal/assets/
2026-06-23 14:48:31 +08:00

80 lines
2.0 KiB
Go

package response
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]string{"key": "value"}
JSON(w, http.StatusOK, data)
if w.Code != http.StatusOK {
t.Errorf("status = %d, want %d", w.Code, http.StatusOK)
}
if ct := w.Header().Get("Content-Type"); ct != "application/json" {
t.Errorf("Content-Type = %s, want application/json", ct)
}
var resp APIResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if resp.Code != 0 {
t.Errorf("code = %d, want 0", resp.Code)
}
if resp.Message != "success" {
t.Errorf("message = %s, want success", resp.Message)
}
}
func TestBadRequest(t *testing.T) {
w := httptest.NewRecorder()
BadRequest(w, "参数错误")
if w.Code != http.StatusBadRequest {
t.Errorf("status = %d, want %d", w.Code, http.StatusBadRequest)
}
}
func TestUnauthorized(t *testing.T) {
w := httptest.NewRecorder()
Unauthorized(w, "未登录")
if w.Code != http.StatusUnauthorized {
t.Errorf("status = %d, want %d", w.Code, http.StatusUnauthorized)
}
}
func TestForbidden(t *testing.T) {
w := httptest.NewRecorder()
Forbidden(w, "无权限")
if w.Code != http.StatusForbidden {
t.Errorf("status = %d, want %d", w.Code, http.StatusForbidden)
}
}
func TestNotFound(t *testing.T) {
w := httptest.NewRecorder()
NotFound(w, "资源不存在")
if w.Code != http.StatusNotFound {
t.Errorf("status = %d, want %d", w.Code, http.StatusNotFound)
}
}
func TestInternalError(t *testing.T) {
w := httptest.NewRecorder()
InternalError(w, "内部错误")
if w.Code != http.StatusInternalServerError {
t.Errorf("status = %d, want %d", w.Code, http.StatusInternalServerError)
}
}
func TestTooManyRequests(t *testing.T) {
w := httptest.NewRecorder()
TooManyRequests(w, "过于频繁")
if w.Code != http.StatusTooManyRequests {
t.Errorf("status = %d, want %d", w.Code, http.StatusTooManyRequests)
}
}