MCPcopy Index your code
hub / github.com/labstack/echo / TestContext_RealIP

Function TestContext_RealIP

context_test.go:1338–1376  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1336}
1337
1338func TestContext_RealIP(t *testing.T) {
1339 _, ipv6ForRemoteAddrExternalRange, _ := net.ParseCIDR("2001:db8::/64")
1340
1341 var testCases = []struct {
1342 name string
1343 givenIPExtrator IPExtractor
1344 whenReq *http.Request
1345 expect string
1346 }{
1347 {
1348 name: "ip from remote addr",
1349 givenIPExtrator: nil,
1350 whenReq: &http.Request{RemoteAddr: "89.89.89.89:1654"},
1351 expect: "89.89.89.89",
1352 },
1353 {
1354 name: "ip from ip extractor",
1355 givenIPExtrator: ExtractIPFromRealIPHeader(TrustIPRange(ipv6ForRemoteAddrExternalRange)),
1356 whenReq: &http.Request{
1357 Header: http.Header{
1358 HeaderXRealIP: []string{"[2001:db8::113:199]"},
1359 HeaderXForwardedFor: []string{"[2001:db8::113:198], [2001:db8::113:197]"}, // <-- should not affect anything
1360 },
1361 RemoteAddr: "[2001:db8::113:1]:8080",
1362 },
1363 expect: "2001:db8::113:199",
1364 },
1365 }
1366 for _, tc := range testCases {
1367 t.Run(tc.name, func(t *testing.T) {
1368 e := New()
1369 c := e.NewContext(tc.whenReq, nil)
1370 if tc.givenIPExtrator != nil {
1371 e.IPExtractor = tc.givenIPExtrator
1372 }
1373 assert.Equal(t, tc.expect, c.RealIP())
1374 })
1375 }
1376}
1377
1378func TestContext_File(t *testing.T) {
1379 var testCases = []struct {

Callers

nothing calls this directly

Calls 5

RealIPMethod · 0.95
TrustIPRangeFunction · 0.85
NewFunction · 0.85
NewContextMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…