Skip to content

Commit 3110dab

Browse files
authored
fix: actually test fix for local port number in /ip response (#213)
1 parent e23bc46 commit 3110dab

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

httpbin/handlers_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,25 @@ func TestIP(t *testing.T) {
406406
assert.Equal(t, result.Origin, tc.wantOrigin, "incorrect origin")
407407
})
408408
}
409+
410+
t.Run("via real connection", func(t *testing.T) {
411+
// (*Request).RemoteAddr includes the local port for real incoming TCP
412+
// connections but not for direct ServeHTTP calls as the used in the
413+
// httptest.NewRecorder tests above, so we need to use a real server
414+
// to verify handling of both cases.
415+
srv := httptest.NewServer(app)
416+
defer srv.Close()
417+
418+
resp, err := client.Get(srv.URL + "/ip")
419+
assert.NilError(t, err)
420+
defer resp.Body.Close()
421+
422+
assert.StatusCode(t, resp, http.StatusOK)
423+
assert.ContentType(t, resp, jsonContentType)
424+
425+
result := must.Unmarshal[ipResponse](t, resp.Body)
426+
assert.Equal(t, result.Origin, "127.0.0.1", "incorrect origin")
427+
})
409428
}
410429

411430
func TestUserAgent(t *testing.T) {

0 commit comments

Comments
 (0)