MCPcopy
hub / github.com/gofiber/fiber / Test_Ctx_IsFromLocal_RemoteAddr

Function Test_Ctx_IsFromLocal_RemoteAddr

ctx_test.go:8918–9004  ·  view source on GitHub ↗

go test -run Test_Ctx_IsFromLocal_RemoteAddr

(t *testing.T)

Source from the content-addressed store, hash-verified

8916
8917// go test -run Test_Ctx_IsFromLocal_RemoteAddr
8918func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) {
8919 t.Parallel()
8920
8921 localIPv4 := net.Addr(&net.TCPAddr{IP: net.ParseIP("127.0.0.1")})
8922 localIPv6 := net.Addr(&net.TCPAddr{IP: net.ParseIP("::1")})
8923 localIPv6long := net.Addr(&net.TCPAddr{IP: net.ParseIP("0:0:0:0:0:0:0:1")})
8924
8925 zeroIPv4 := net.Addr(&net.TCPAddr{IP: net.IPv4zero})
8926
8927 someIPv4 := net.Addr(&net.TCPAddr{IP: net.ParseIP("93.46.8.90")})
8928 someIPv6 := net.Addr(&net.TCPAddr{IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")})
8929
8930 // Test for the case fasthttp remoteAddr is set to "127.0.0.1".
8931 {
8932 app := New()
8933 fastCtx := &fasthttp.RequestCtx{}
8934 fastCtx.SetRemoteAddr(localIPv4)
8935 c := app.AcquireCtx(fastCtx)
8936 defer app.ReleaseCtx(c)
8937
8938 require.Equal(t, "127.0.0.1", c.IP())
8939 require.True(t, c.IsFromLocal())
8940 }
8941 // Test for the case fasthttp remoteAddr is set to "::1".
8942 {
8943 app := New()
8944 fastCtx := &fasthttp.RequestCtx{}
8945 fastCtx.SetRemoteAddr(localIPv6)
8946 c := app.AcquireCtx(fastCtx)
8947 defer app.ReleaseCtx(c)
8948 require.Equal(t, "::1", c.Req().IP())
8949 require.True(t, c.Req().IsFromLocal())
8950 }
8951 // Test for the case fasthttp remoteAddr is set to "0:0:0:0:0:0:0:1".
8952 {
8953 app := New()
8954 fastCtx := &fasthttp.RequestCtx{}
8955 fastCtx.SetRemoteAddr(localIPv6long)
8956 c := app.AcquireCtx(fastCtx)
8957 defer app.ReleaseCtx(c)
8958 // fasthttp should return "::1" for "0:0:0:0:0:0:0:1".
8959 // otherwise IsFromLocal() will break.
8960 require.Equal(t, "::1", c.IP())
8961 require.True(t, c.IsFromLocal())
8962 }
8963 // Test for the case fasthttp remoteAddr is set to "0.0.0.0".
8964 {
8965 app := New()
8966 fastCtx := &fasthttp.RequestCtx{}
8967 fastCtx.SetRemoteAddr(zeroIPv4)
8968 c := app.AcquireCtx(fastCtx)
8969 defer app.ReleaseCtx(c)
8970 require.Equal(t, "0.0.0.0", c.IP())
8971 require.False(t, c.IsFromLocal())
8972 }
8973 // Test for the case fasthttp remoteAddr is set to "93.46.8.90".
8974 {
8975 app := New()

Callers

nothing calls this directly

Calls 7

AddrMethod · 0.80
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
NewFunction · 0.70
IPMethod · 0.65
IsFromLocalMethod · 0.65
ReqMethod · 0.65

Tested by

no test coverage detected