Files
Prism/internal/api/openapi_test.go
T
renjue 59d1087cc9
CI / test (push) Failing after 1m46s
CI / docker (push) Has been skipped
feat: Prism HTTP/SOCKS5 代理网关及管理台
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 15:08:50 +08:00

49 lines
1.0 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestServeOpenAPIJSON(t *testing.T) {
s := &Server{}
r := s.Router()
req := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.json", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") {
t.Fatalf("content-type: %s", ct)
}
var doc map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &doc); err != nil {
t.Fatal(err)
}
if doc["openapi"] != "3.0.3" {
t.Fatalf("unexpected openapi version: %v", doc["openapi"])
}
}
func TestServeOpenAPIDocs(t *testing.T) {
s := &Server{}
r := s.Router()
req := httptest.NewRequest(http.MethodGet, "/api/docs", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status %d", w.Code)
}
if !strings.Contains(w.Body.String(), "swagger-ui") {
t.Fatal("expected swagger ui html")
}
}