mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
chore: add more unit tests for mcp (#4928)
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
13
mcp/types.go
13
mcp/types.go
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user