chore: add more unit tests for mcp (#4928)

This commit is contained in:
Kevin Wan
2025-06-07 20:41:57 +08:00
committed by GitHub
parent d4cccca387
commit 91c885b5b0
2 changed files with 19 additions and 8 deletions

View File

@@ -175,6 +175,20 @@ func TestHandleRequest_badRequest(t *testing.T) {
mock.server.handleRequest(w, r) mock.server.handleRequest(w, r)
assert.Equal(t, http.StatusBadRequest, w.Code) assert.Equal(t, http.StatusBadRequest, w.Code)
}) })
t.Run("bad id", func(t *testing.T) {
mock := newMockMcpServer(t)
defer mock.shutdown()
addTestClient(mock.server, "test-session", true)
body := `{"jsonrpc": "2.0", "id": {}, "method": "tools.call", "params": {}}`
r := httptest.NewRequest(http.MethodPost, "/?session_id=test-session", bytes.NewReader([]byte(body)))
w := httptest.NewRecorder()
mock.server.handleRequest(w, r)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "Invalid request.ID")
})
} }
func TestRegisterTool(t *testing.T) { func TestRegisterTool(t *testing.T) {

View File

@@ -22,23 +22,20 @@ type Request struct {
} }
func (r Request) isNotification() (bool, error) { func (r Request) isNotification() (bool, error) {
var isNotification bool
switch val := r.ID.(type) { switch val := r.ID.(type) {
case int: case int:
isNotification = val == 0 return val == 0, nil
case int64: case int64:
isNotification = val == 0 return val == 0, nil
case float64: case float64:
isNotification = val == 0.0 return val == 0.0, nil
case string: case string:
isNotification = len(val) == 0 return len(val) == 0, nil
case nil: case nil:
isNotification = true return true, nil
default: default:
return false, fmt.Errorf("invalid type %T", val) return false, fmt.Errorf("invalid type %T", val)
} }
return isNotification, nil
} }
type PaginatedParams struct { type PaginatedParams struct {