Skip to content

Commit c7988f1

Browse files
test(mcp): add connect-timeout default behaviour tests
Add four tests in package mcp covering the connectTimeout defaulting logic in NewHTTPConnection: - TestNewHTTPConnection_DefaultConnectTimeout_ZeroInput Ensures that connectTimeout=0 falls back to defaultConnectTimeout (30 s). - TestNewHTTPConnection_DefaultConnectTimeout_NegativeInput Ensures that connectTimeout<0 also falls back (guards the <= 0 fix for the bug identified in #3933). - TestNewHTTPConnection_DefaultConnectTimeout_CustomValue Ensures that a positive connectTimeout is stored unchanged. - TestDefaultConnectTimeout_Value Guards the constant value so any drift from config.DefaultConnectTimeout (30 s) causes an immediate, obvious test failure rather than a silent bug. These tests directly exercise the paths fixed in fix(mcp):#3933 and provide regression coverage for future changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 28f4c74 commit c7988f1

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package mcp
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// newMinimalTestServer returns an httptest server that responds with a minimal
16+
// JSON-RPC initialize result so NewHTTPConnection can complete its handshake.
17+
func newMinimalTestServer(t *testing.T) *httptest.Server {
18+
t.Helper()
19+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20+
resp := map[string]interface{}{
21+
"jsonrpc": "2.0",
22+
"id": 1,
23+
"result": map[string]interface{}{
24+
"protocolVersion": "2024-11-05",
25+
"serverInfo": map[string]interface{}{"name": "test"},
26+
},
27+
}
28+
w.Header().Set("Content-Type", "application/json")
29+
json.NewEncoder(w).Encode(resp) //nolint:errcheck
30+
}))
31+
}
32+
33+
// TestNewHTTPConnection_DefaultConnectTimeout_ZeroInput verifies that a zero
34+
// connectTimeout is replaced with defaultConnectTimeout (30 s).
35+
func TestNewHTTPConnection_DefaultConnectTimeout_ZeroInput(t *testing.T) {
36+
srv := newMinimalTestServer(t)
37+
defer srv.Close()
38+
39+
conn, err := NewHTTPConnection(context.Background(), "test", srv.URL,
40+
map[string]string{"Authorization": "test"}, nil, "", 0, 0)
41+
require.NoError(t, err)
42+
require.NotNil(t, conn)
43+
defer conn.Close()
44+
45+
assert.Equal(t, defaultConnectTimeout, conn.connectTimeout,
46+
"zero connectTimeout should be replaced with defaultConnectTimeout")
47+
}
48+
49+
// TestNewHTTPConnection_DefaultConnectTimeout_NegativeInput verifies that a
50+
// negative connectTimeout is also replaced with defaultConnectTimeout.
51+
func TestNewHTTPConnection_DefaultConnectTimeout_NegativeInput(t *testing.T) {
52+
srv := newMinimalTestServer(t)
53+
defer srv.Close()
54+
55+
conn, err := NewHTTPConnection(context.Background(), "test", srv.URL,
56+
map[string]string{"Authorization": "test"}, nil, "", 0, -1*time.Second)
57+
require.NoError(t, err)
58+
require.NotNil(t, conn)
59+
defer conn.Close()
60+
61+
assert.Equal(t, defaultConnectTimeout, conn.connectTimeout,
62+
"negative connectTimeout should be replaced with defaultConnectTimeout")
63+
}
64+
65+
// TestNewHTTPConnection_DefaultConnectTimeout_CustomValue verifies that a
66+
// positive connectTimeout is stored as-is without being replaced.
67+
func TestNewHTTPConnection_DefaultConnectTimeout_CustomValue(t *testing.T) {
68+
srv := newMinimalTestServer(t)
69+
defer srv.Close()
70+
71+
custom := 10 * time.Second
72+
conn, err := NewHTTPConnection(context.Background(), "test", srv.URL,
73+
map[string]string{"Authorization": "test"}, nil, "", 0, custom)
74+
require.NoError(t, err)
75+
require.NotNil(t, conn)
76+
defer conn.Close()
77+
78+
assert.Equal(t, custom, conn.connectTimeout,
79+
"a positive connectTimeout should be stored unchanged")
80+
}
81+
82+
// TestDefaultConnectTimeout_Value guards against the constant value drifting
83+
// away from config.DefaultConnectTimeout (30 s) unintentionally.
84+
func TestDefaultConnectTimeout_Value(t *testing.T) {
85+
assert.Equal(t, 30*time.Second, defaultConnectTimeout,
86+
"defaultConnectTimeout must remain 30 s to stay in sync with config.DefaultConnectTimeout")
87+
}

0 commit comments

Comments
 (0)