-
Notifications
You must be signed in to change notification settings - Fork 21
[Repo Assist] test(mcp): add connect-timeout default behaviour tests #3947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lpcox
merged 2 commits into
main
from
repo-assist/test-connect-timeout-defaults-981ead161748c823
Apr 16, 2026
+100
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // newMinimalTestServer returns an httptest server that responds with the | ||
| // minimal HTTP semantics needed for NewHTTPConnection to complete its SDK | ||
| // transport handshake reliably. | ||
| func newMinimalTestServer(t *testing.T) *httptest.Server { | ||
| t.Helper() | ||
| return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.Method { | ||
| case http.MethodGet: | ||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| case http.MethodPost: | ||
| resp := map[string]interface{}{ | ||
| "jsonrpc": "2.0", | ||
| "id": 1, | ||
| "result": map[string]interface{}{ | ||
| "protocolVersion": "2024-11-05", | ||
| "serverInfo": map[string]interface{}{"name": "test"}, | ||
| }, | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.Header().Set("Mcp-Session-Id", "test-session") | ||
| json.NewEncoder(w).Encode(resp) //nolint:errcheck | ||
| return | ||
| default: | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| // TestNewHTTPConnection_DefaultConnectTimeout_ZeroInput verifies that a zero | ||
| // connectTimeout is replaced with defaultConnectTimeout (30 s). | ||
| func TestNewHTTPConnection_DefaultConnectTimeout_ZeroInput(t *testing.T) { | ||
| srv := newMinimalTestServer(t) | ||
| defer srv.Close() | ||
|
|
||
| conn, err := NewHTTPConnection(context.Background(), "test", srv.URL, | ||
| map[string]string{"Authorization": "test"}, nil, "", 0, 0) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, conn) | ||
| defer conn.Close() | ||
|
|
||
| assert.Equal(t, defaultConnectTimeout, conn.connectTimeout, | ||
| "zero connectTimeout should be replaced with defaultConnectTimeout") | ||
| } | ||
|
|
||
| // TestNewHTTPConnection_DefaultConnectTimeout_NegativeInput verifies that a | ||
| // negative connectTimeout is also replaced with defaultConnectTimeout. | ||
| func TestNewHTTPConnection_DefaultConnectTimeout_NegativeInput(t *testing.T) { | ||
| srv := newMinimalTestServer(t) | ||
| defer srv.Close() | ||
|
|
||
| conn, err := NewHTTPConnection(context.Background(), "test", srv.URL, | ||
| map[string]string{"Authorization": "test"}, nil, "", 0, -1*time.Second) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, conn) | ||
| defer conn.Close() | ||
|
|
||
| assert.Equal(t, defaultConnectTimeout, conn.connectTimeout, | ||
| "negative connectTimeout should be replaced with defaultConnectTimeout") | ||
| } | ||
|
|
||
| // TestNewHTTPConnection_DefaultConnectTimeout_CustomValue verifies that a | ||
| // positive connectTimeout is stored as-is without being replaced. | ||
| func TestNewHTTPConnection_DefaultConnectTimeout_CustomValue(t *testing.T) { | ||
| srv := newMinimalTestServer(t) | ||
| defer srv.Close() | ||
|
|
||
| custom := 10 * time.Second | ||
| conn, err := NewHTTPConnection(context.Background(), "test", srv.URL, | ||
| map[string]string{"Authorization": "test"}, nil, "", 0, custom) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, conn) | ||
| defer conn.Close() | ||
|
|
||
| assert.Equal(t, custom, conn.connectTimeout, | ||
| "a positive connectTimeout should be stored unchanged") | ||
| } | ||
|
|
||
| // TestDefaultConnectTimeout_Value guards against the constant value drifting | ||
| // away from config.DefaultConnectTimeout (30 s) unintentionally. | ||
| func TestDefaultConnectTimeout_Value(t *testing.T) { | ||
| assert.Equal(t, 30*time.Second, defaultConnectTimeout, | ||
| "defaultConnectTimeout must remain 30 s to stay in sync with config.DefaultConnectTimeout") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These assertions reference
defaultConnectTimeout, but there is nodefaultConnectTimeoutidentifier defined anywhere ininternal/mcp(the current implementation hard-codes30 * time.SecondinNewHTTPConnection). As written, this file will not compile. Either introducedefaultConnectTimeoutin the production code (and use it inNewHTTPConnection), or have the tests derive the expected value frominternal/config.DefaultConnectTimeout(e.g.,time.Duration(config.DefaultConnectTimeout) * time.Second) / inline the expected duration.This issue also appears on line 82 of the same file.