99 "testing"
1010
1111 "github.com/modelcontextprotocol/go-sdk/mcp"
12+ "github.com/stretchr/testify/assert"
1213 "github.com/stretchr/testify/require"
1314)
1415
@@ -40,9 +41,7 @@ func TestMCPHTTPServerAddr_BindsToLoopback(t *testing.T) {
4041 }
4142
4243 for _ , tc := range testCases {
43- if got := mcpHTTPServerAddr (tc .port ); got != tc .want {
44- t .Errorf ("mcpHTTPServerAddr(%d) = %q, want %q" , tc .port , got , tc .want )
45- }
44+ assert .Equal (t , tc .want , mcpHTTPServerAddr (tc .port ), "mcpHTTPServerAddr(%d)" , tc .port )
4645 }
4746}
4847
@@ -58,56 +57,29 @@ func TestMCPHTTPServerDisplayURL_UsesLoopbackAddress(t *testing.T) {
5857 }
5958
6059 for _ , tc := range testCases {
61- if got := mcpHTTPServerDisplayURL (tc .port ); got != tc .want {
62- t .Errorf ("mcpHTTPServerDisplayURL(%d) = %q, want %q" , tc .port , got , tc .want )
63- }
60+ assert .Equal (t , tc .want , mcpHTTPServerDisplayURL (tc .port ), "mcpHTTPServerDisplayURL(%d)" , tc .port )
6461 }
6562}
6663
67- func TestSanitizeForLog_NoSpecialChars (t * testing.T ) {
64+ func TestSanitizeForLog (t * testing.T ) {
6865 t .Parallel ()
69- input := "/api/v1/workflows"
70- got := sanitizeForLog (input )
71- if got != input {
72- t .Errorf ("sanitizeForLog(%q) = %q, want %q" , input , got , input )
73- }
74- }
75-
76- func TestSanitizeForLog_NewlineInjection (t * testing.T ) {
77- t .Parallel ()
78- input := "/path\n INJECTED LOG ENTRY"
79- got := sanitizeForLog (input )
80- expected := "/pathINJECTED LOG ENTRY"
81- if got != expected {
82- t .Errorf ("sanitizeForLog(%q) = %q, want %q" , input , got , expected )
83- }
84- }
85-
86- func TestSanitizeForLog_CarriageReturn (t * testing.T ) {
87- t .Parallel ()
88- input := "/path\r malicious"
89- got := sanitizeForLog (input )
90- expected := "/pathmalicious"
91- if got != expected {
92- t .Errorf ("sanitizeForLog(%q) = %q, want %q" , input , got , expected )
66+ testCases := []struct {
67+ name string
68+ input string
69+ want string
70+ }{
71+ {name : "no special chars" , input : "/api/v1/workflows" , want : "/api/v1/workflows" },
72+ {name : "newline injection" , input : "/path\n INJECTED LOG ENTRY" , want : "/pathINJECTED LOG ENTRY" },
73+ {name : "carriage return" , input : "/path\r malicious" , want : "/pathmalicious" },
74+ {name : "both newline and carriage return" , input : "line1\r \n line2" , want : "line1line2" },
75+ {name : "empty" , input : "" , want : "" },
9376 }
94- }
9577
96- func TestSanitizeForLog_BothNewlineAndCarriageReturn (t * testing.T ) {
97- t .Parallel ()
98- input := "line1\r \n line2"
99- got := sanitizeForLog (input )
100- expected := "line1line2"
101- if got != expected {
102- t .Errorf ("sanitizeForLog(%q) = %q, want %q" , input , got , expected )
103- }
104- }
105-
106- func TestSanitizeForLog_Empty (t * testing.T ) {
107- t .Parallel ()
108- got := sanitizeForLog ("" )
109- if got != "" {
110- t .Errorf ("sanitizeForLog(\" \" ) = %q, want empty string" , got )
78+ for _ , tc := range testCases {
79+ t .Run (tc .name , func (t * testing.T ) {
80+ t .Parallel ()
81+ assert .Equal (t , tc .want , sanitizeForLog (tc .input ))
82+ })
11183 }
11284}
11385
@@ -117,9 +89,7 @@ func TestResponseWriter_DefaultStatusCode(t *testing.T) {
11789 ResponseWriter : httptest .NewRecorder (),
11890 statusCode : http .StatusOK ,
11991 }
120- if rw .statusCode != http .StatusOK {
121- t .Errorf ("default statusCode = %d, want %d" , rw .statusCode , http .StatusOK )
122- }
92+ assert .Equal (t , http .StatusOK , rw .statusCode , "default statusCode" )
12393}
12494
12595func TestResponseWriter_EmbeddedResponseWriter (t * testing.T ) {
@@ -131,12 +101,8 @@ func TestResponseWriter_EmbeddedResponseWriter(t *testing.T) {
131101 }
132102 // The responseWriter delegates writes to the embedded ResponseWriter
133103 rw .WriteHeader (http .StatusCreated )
134- if rec .Code != http .StatusCreated {
135- t .Errorf ("embedded ResponseWriter code = %d, want %d" , rec .Code , http .StatusCreated )
136- }
137- if rw .statusCode != http .StatusCreated {
138- t .Errorf ("responseWriter statusCode = %d, want %d" , rw .statusCode , http .StatusCreated )
139- }
104+ assert .Equal (t , http .StatusCreated , rec .Code , "embedded ResponseWriter code" )
105+ assert .Equal (t , http .StatusCreated , rw .statusCode , "responseWriter statusCode" )
140106}
141107
142108func TestLoggingHandler_PassesRequestToHandler (t * testing.T ) {
@@ -153,11 +119,24 @@ func TestLoggingHandler_PassesRequestToHandler(t *testing.T) {
153119 rec := httptest .NewRecorder ()
154120 handler .ServeHTTP (rec , req )
155121
156- if ! handlerCalled {
157- t .Error ("loggingHandler did not call the inner handler" )
158- }
159- if rec .Code != http .StatusOK {
160- t .Errorf ("response code = %d, want %d" , rec .Code , http .StatusOK )
122+ assert .True (t , handlerCalled , "loggingHandler should call the inner handler" )
123+ assert .Equal (t , http .StatusOK , rec .Code , "response code" )
124+ }
125+
126+ func TestLoggingHandler_PreservesResponseStatus (t * testing.T ) {
127+ t .Parallel ()
128+ for _ , status := range []int {http .StatusOK , http .StatusNotFound , http .StatusInternalServerError } {
129+ t .Run (http .StatusText (status ), func (t * testing.T ) {
130+ t .Parallel ()
131+ inner := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
132+ w .WriteHeader (status )
133+ })
134+
135+ rec := httptest .NewRecorder ()
136+ loggingHandler (inner ).ServeHTTP (rec , httptest .NewRequest (http .MethodGet , "/status" , nil ))
137+
138+ assert .Equal (t , status , rec .Code , "status should reach the client unchanged" )
139+ })
161140 }
162141}
163142
@@ -177,7 +156,5 @@ func TestLoggingHandler_SanitizesPath(t *testing.T) {
177156 // Should not panic
178157 handler .ServeHTTP (rec , req )
179158
180- if rec .Code != http .StatusOK {
181- t .Errorf ("response code = %d, want %d" , rec .Code , http .StatusOK )
182- }
159+ assert .Equal (t , http .StatusOK , rec .Code , "response code" )
183160}
0 commit comments