(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestInitializeCreatesSession(t *testing.T) { |
| 18 | gin.SetMode(gin.TestMode) |
| 19 | srv := newTestServer(nil) |
| 20 | |
| 21 | r := gin.New() |
| 22 | r.POST("/mcp", func(c *gin.Context) { |
| 23 | common.GinAppendValues(c, conf.UserKey, &model.User{ID: 1, Role: model.ADMIN}) |
| 24 | srv.handlePost(c) |
| 25 | }) |
| 26 | |
| 27 | req := httptest.NewRequest(http.MethodPost, "http://example.com/mcp", strings.NewReader(`{ |
| 28 | "jsonrpc":"2.0", |
| 29 | "id":1, |
| 30 | "method":"initialize", |
| 31 | "params":{ |
| 32 | "protocolVersion":"2025-11-25", |
| 33 | "capabilities":{}, |
| 34 | "clientInfo":{"name":"test-client","version":"1.0.0"} |
| 35 | } |
| 36 | }`)) |
| 37 | req.Header.Set("Accept", "application/json, text/event-stream") |
| 38 | req.Header.Set("Origin", "http://example.com") |
| 39 | |
| 40 | w := httptest.NewRecorder() |
| 41 | r.ServeHTTP(w, req) |
| 42 | |
| 43 | if w.Code != http.StatusOK { |
| 44 | t.Fatalf("unexpected status: got %d want %d", w.Code, http.StatusOK) |
| 45 | } |
| 46 | if got := w.Header().Get(SessionHeader); got == "" { |
| 47 | t.Fatal("expected session header to be set") |
| 48 | } |
| 49 | |
| 50 | var resp response |
| 51 | if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { |
| 52 | t.Fatalf("failed to decode response: %v", err) |
| 53 | } |
| 54 | if resp.Error != nil { |
| 55 | t.Fatalf("unexpected error response: %+v", resp.Error) |
| 56 | } |
| 57 | result, ok := resp.Result.(map[string]any) |
| 58 | if !ok { |
| 59 | t.Fatalf("unexpected result type: %T", resp.Result) |
| 60 | } |
| 61 | if result["protocolVersion"] != ProtocolVersion { |
| 62 | t.Fatalf("unexpected protocol version: %v", result["protocolVersion"]) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestInitializeNegotiatesSupportedOlderProtocolVersion(t *testing.T) { |
| 67 | gin.SetMode(gin.TestMode) |
nothing calls this directly
no test coverage detected