Skip to content

Commit b7a781c

Browse files
vishrclaude
andcommitted
Add comprehensive tests for IP extraction improvements
Adds tests for issue #2757 IP extraction edge cases where RemoteAddr may not include a port. The enhanced extractIP function now properly handles IPv4/IPv6 addresses without ports using net.ParseIP validation. Test cases cover: - IPv4 without port - IPv6 without port - IPv6 with port brackets - Invalid IP format handling Existing tests for issue #2789 response flush error handling are already comprehensive and validate the improved error messages with ResponseWriter types. Fixes #2757 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d0137c3 commit b7a781c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ip_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,34 @@ func TestExtractIPDirect(t *testing.T) {
379379
},
380380
expectIP: "203.0.113.1",
381381
},
382+
{
383+
name: "remote addr is IP without port, extracts IP directly",
384+
whenRequest: http.Request{
385+
RemoteAddr: "203.0.113.1",
386+
},
387+
expectIP: "203.0.113.1",
388+
},
389+
{
390+
name: "remote addr is IPv6 without port, extracts IP directly",
391+
whenRequest: http.Request{
392+
RemoteAddr: "2001:db8::1",
393+
},
394+
expectIP: "2001:db8::1",
395+
},
396+
{
397+
name: "remote addr is IPv6 with port",
398+
whenRequest: http.Request{
399+
RemoteAddr: "[2001:db8::1]:8080",
400+
},
401+
expectIP: "2001:db8::1",
402+
},
403+
{
404+
name: "remote addr is invalid, returns empty string",
405+
whenRequest: http.Request{
406+
RemoteAddr: "invalid-ip-format",
407+
},
408+
expectIP: "",
409+
},
382410
{
383411
name: "request is from external IP has X-Real-Ip header, extractor still extracts IP from request remote addr",
384412
whenRequest: http.Request{

0 commit comments

Comments
 (0)