1
0
mirror of https://github.com/labstack/echo.git synced 2025-09-16 09:16:29 +02:00

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 <noreply@anthropic.com>
This commit is contained in:
Vishal Rana
2025-09-15 19:21:57 -07:00
parent d0137c3e80
commit b7a781ce5d

View File

@@ -379,6 +379,34 @@ func TestExtractIPDirect(t *testing.T) {
},
expectIP: "203.0.113.1",
},
{
name: "remote addr is IP without port, extracts IP directly",
whenRequest: http.Request{
RemoteAddr: "203.0.113.1",
},
expectIP: "203.0.113.1",
},
{
name: "remote addr is IPv6 without port, extracts IP directly",
whenRequest: http.Request{
RemoteAddr: "2001:db8::1",
},
expectIP: "2001:db8::1",
},
{
name: "remote addr is IPv6 with port",
whenRequest: http.Request{
RemoteAddr: "[2001:db8::1]:8080",
},
expectIP: "2001:db8::1",
},
{
name: "remote addr is invalid, returns empty string",
whenRequest: http.Request{
RemoteAddr: "invalid-ip-format",
},
expectIP: "",
},
{
name: "request is from external IP has X-Real-Ip header, extractor still extracts IP from request remote addr",
whenRequest: http.Request{