84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestHealth(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
NewRouter().ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", response.Code, http.StatusOK)
|
|
}
|
|
if got := response.Header().Get("Content-Type"); got != "application/json; charset=utf-8" {
|
|
t.Fatalf("Content-Type = %q, want application/json; charset=utf-8", got)
|
|
}
|
|
|
|
var body healthResponse
|
|
if err := json.NewDecoder(response.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if body.Status != "ok" {
|
|
t.Fatalf("status body = %q, want ok", body.Status)
|
|
}
|
|
}
|
|
|
|
func TestHealthRejectsOtherMethods(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodPost, "/health", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
NewRouter().ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("status = %d, want %d", response.Code, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func TestRouterDoesNotExposeMCPWithoutConfiguredHandler(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodPost, "/mcp", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
NewRouter().ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", response.Code, http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func TestRouterRegistersConfiguredMCPHandler(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodPost, "/mcp", nil)
|
|
response := httptest.NewRecorder()
|
|
mcp := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusAccepted)
|
|
})
|
|
|
|
NewRouter(RouterOptions{MCP: mcp}).ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want %d", response.Code, http.StatusAccepted)
|
|
}
|
|
}
|
|
|
|
func TestRouterRegistersConfiguredDashScopeChatHandler(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodPost, "/api/v1/apps/test-app/completion", nil)
|
|
response := httptest.NewRecorder()
|
|
chat := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.PathValue("app_id") != "test-app" {
|
|
t.Fatalf("app_id = %q", r.PathValue("app_id"))
|
|
}
|
|
w.WriteHeader(http.StatusAccepted)
|
|
})
|
|
|
|
NewRouter(RouterOptions{DashScopeChat: chat}).ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want %d", response.Code, http.StatusAccepted)
|
|
}
|
|
}
|