package app import ( "encoding/json" "lingniu/vehicle-data-platform/apps/api/internal/config" "net/http" "net/http/httptest" "strings" "testing" "time" ) func TestWithRequestTimeoutAddsContextDeadline(t *testing.T) { handler := withRequestTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { deadline, ok := r.Context().Deadline() if !ok { t.Fatal("request context should have deadline") } if time.Until(deadline) > 250*time.Millisecond { t.Fatalf("deadline too far away: %s", time.Until(deadline)) } w.WriteHeader(http.StatusNoContent) }), 200*time.Millisecond) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/ops/health", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusNoContent { t.Fatalf("status = %d", rec.Code) } } func TestWithRequestTimeoutReturnsEnvelopeWithTraceID(t *testing.T) { handler := withRequestTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() }), time.Millisecond) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/ops/health", nil) req.Header.Set("X-Trace-Id", "trace-from-test") handler.ServeHTTP(rec, req) if rec.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } var body struct { Error struct { Code string `json:"code"` Message string `json:"message"` } `json:"error"` TraceID string `json:"traceId"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("timeout response should be JSON: %v body=%s", err, rec.Body.String()) } if body.Error.Code != "REQUEST_TIMEOUT" || body.Error.Message != "请求处理超时" || body.TraceID != "trace-from-test" { t.Fatalf("unexpected timeout envelope: %+v body=%s", body, rec.Body.String()) } } func TestAppConfigScriptIsRuntimeRendered(t *testing.T) { handler := withAppConfig(http.NotFoundHandler(), config.Config{ AMapWebJSKey: "web-key", AMapSecurityCode: "security-code", AMapServiceHost: "/_AMapService", }) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/app-config.js", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if got := rec.Header().Get("Cache-Control"); got != "no-store" { t.Fatalf("Cache-Control = %q, want no-store", got) } body := rec.Body.String() for _, want := range []string{"window.__LINGNIU_APP_CONFIG__=", `"amapWebJsKey":"web-key"`, `"amapSecurityServiceHost":"/_AMapService"`} { if !strings.Contains(body, want) { t.Fatalf("app config script missing %q: %s", want, body) } } if strings.Contains(body, "security-code") || strings.Contains(body, "amapSecurityJsCode") { t.Fatalf("app config script should not expose AMap security code: %s", body) } } func TestAMapSecurityProxyAppendsServerSideJscode(t *testing.T) { var gotPath string var gotJscode string upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path gotJscode = r.URL.Query().Get("jscode") w.Header().Set("X-AMap-Test", "ok") _, _ = w.Write([]byte("proxied")) })) defer upstream.Close() handler := withAMapSecurityProxy(http.NotFoundHandler(), config.Config{ AMapSecurityCode: "security-code", AMapServiceHost: "/_AMapService", }, amapProxyUpstreams{RestAPI: upstream.URL}, http.DefaultClient) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/_AMapService/v3/weather/weatherInfo?city=440100", nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } if gotPath != "/v3/weather/weatherInfo" || gotJscode != "security-code" { t.Fatalf("upstream path=%q jscode=%q", gotPath, gotJscode) } if rec.Body.String() != "proxied" || rec.Header().Get("X-AMap-Test") != "ok" { t.Fatalf("proxy response not copied: headers=%v body=%s", rec.Header(), rec.Body.String()) } } func TestAMapSecurityProxyRoutesKnownMapResources(t *testing.T) { seen := map[string]string{} newUpstream := func(name string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { seen[name] = r.URL.Path _, _ = w.Write([]byte(name)) })) } rest := newUpstream("rest") webapi := newUpstream("webapi") fmap := newUpstream("fmap") defer rest.Close() defer webapi.Close() defer fmap.Close() handler := withAMapSecurityProxy(http.NotFoundHandler(), config.Config{ AMapSecurityCode: "security-code", AMapServiceHost: "/_AMapService", }, amapProxyUpstreams{RestAPI: rest.URL, WebAPI: webapi.URL, FMap: fmap.URL}, http.DefaultClient) for _, path := range []string{ "/_AMapService/v4/map/styles", "/_AMapService/v3/vectormap", "/_AMapService/v3/weather/weatherInfo", } { rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, path, nil) handler.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("%s status = %d body=%s", path, rec.Code, rec.Body.String()) } } if seen["webapi"] != "/v4/map/styles" || seen["fmap"] != "/v3/vectormap" || seen["rest"] != "/v3/weather/weatherInfo" { t.Fatalf("unexpected proxy routing: %+v", seen) } }