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

Function runCtxProxyTrustUnixRemoteAddrCase

ctx_test.go:3703–3757  ·  view source on GitHub ↗
(t *testing.T, unixSocket bool)

Source from the content-addressed store, hash-verified

3701}
3702
3703func runCtxProxyTrustUnixRemoteAddrCase(t *testing.T, unixSocket bool) string {
3704 t.Helper()
3705
3706 app := New(Config{
3707 TrustProxy: true,
3708 TrustProxyConfig: TrustProxyConfig{
3709 UnixSocket: unixSocket,
3710 },
3711 ProxyHeader: HeaderXForwardedFor,
3712 })
3713 app.Get("/ip", func(c Ctx) error {
3714 return c.SendString(fmt.Sprintf("%t|%s", c.IsProxyTrusted(), c.IP()))
3715 })
3716
3717 tmp, err := os.MkdirTemp(os.TempDir(), "fiber-ctx-unix")
3718 require.NoError(t, err)
3719 t.Cleanup(func() { require.NoError(t, os.RemoveAll(tmp)) })
3720 sock := filepath.Join(tmp, "fiber.sock")
3721
3722 result := make(chan string, 1)
3723 errCh := make(chan error, 1)
3724 go func() {
3725 time.Sleep(1000 * time.Millisecond)
3726
3727 client := &fasthttp.HostClient{
3728 Addr: sock,
3729 Dial: func(addr string) (net.Conn, error) {
3730 return net.Dial(NetworkUnix, addr)
3731 },
3732 }
3733
3734 req := &fasthttp.Request{}
3735 resp := &fasthttp.Response{}
3736 req.SetRequestURI("http://fiber/ip")
3737 req.Header.Set(HeaderXForwardedFor, "1.1.1.1")
3738
3739 if err = client.Do(req, resp); err != nil {
3740 result <- "" // Ensure result channel always receives a value
3741 errCh <- errors.Join(err, app.Shutdown())
3742 return
3743 }
3744
3745 result <- string(resp.Body())
3746 errCh <- app.Shutdown()
3747 }()
3748
3749 require.NoError(t, app.Listen(sock, ListenConfig{
3750 DisableStartupMessage: true,
3751 ListenerNetwork: NetworkUnix,
3752 UnixSocketFileMode: 0o660,
3753 }))
3754 require.NoError(t, <-errCh)
3755
3756 return <-result
3757}
3758
3759// go test -run Test_Ctx_IPs -parallel
3760func Test_Ctx_IPs(t *testing.T) {

Callers 1

Calls 10

BodyMethod · 0.95
ShutdownMethod · 0.80
ListenMethod · 0.80
NewFunction · 0.70
GetMethod · 0.65
SendStringMethod · 0.65
IsProxyTrustedMethod · 0.65
IPMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65

Tested by

no test coverage detected