MCPcopy Create free account
hub / github.com/OpenListTeam/OpenList / TestInitializeReusesExistingSession

Function TestInitializeReusesExistingSession

server/mcp/handler_test.go:108–156  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

106}
107
108func TestInitializeReusesExistingSession(t *testing.T) {
109 gin.SetMode(gin.TestMode)
110 srv := newTestServer(nil)
111 currentSession := srv.createSession(1)
112 currentSession.initialized = true
113
114 r := gin.New()
115 r.POST("/mcp", func(c *gin.Context) {
116 common.GinAppendValues(c, conf.UserKey, &model.User{ID: 1, Role: model.ADMIN})
117 srv.handlePost(c)
118 })
119
120 req := httptest.NewRequest(http.MethodPost, "http://example.com/mcp", strings.NewReader(`{
121 "jsonrpc":"2.0",
122 "id":1,
123 "method":"initialize",
124 "params":{
125 "protocolVersion":"2025-06-18",
126 "capabilities":{},
127 "clientInfo":{"name":"test-client","version":"1.0.0"}
128 }
129 }`))
130 req.Header.Set("Accept", "application/json, text/event-stream")
131 req.Header.Set("Origin", "http://example.com")
132 req.Header.Set(SessionHeader, currentSession.id)
133
134 w := httptest.NewRecorder()
135 r.ServeHTTP(w, req)
136
137 if w.Code != http.StatusOK {
138 t.Fatalf("unexpected status: got %d want %d", w.Code, http.StatusOK)
139 }
140 if got := w.Header().Get(SessionHeader); got != currentSession.id {
141 t.Fatalf("unexpected session header: got %q want %q", got, currentSession.id)
142 }
143 if len(srv.sessions) != 1 {
144 t.Fatalf("unexpected session count: got %d want %d", len(srv.sessions), 1)
145 }
146 reusedSession, ok := srv.getSession(currentSession.id)
147 if !ok {
148 t.Fatal("expected existing session to be reused")
149 }
150 if reusedSession.initialized {
151 t.Fatal("expected reused session to require initialized notification again")
152 }
153 if reusedSession.protocolVersion != "2025-06-18" {
154 t.Fatalf("unexpected protocol version: got %q want %q", reusedSession.protocolVersion, "2025-06-18")
155 }
156}
157
158func TestInitializeNegotiatesUnsupportedProtocolVersion(t *testing.T) {
159 gin.SetMode(gin.TestMode)

Callers

nothing calls this directly

Calls 9

GinAppendValuesFunction · 0.92
newTestServerFunction · 0.85
handlePostMethod · 0.80
ServeHTTPMethod · 0.80
HeaderMethod · 0.80
getSessionMethod · 0.80
GetMethod · 0.65
createSessionMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected