Skip to content
Myr

refactor(context): replace hardcoded localhost IPs with constants (#4481)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Paulo Henrique · committed by GitHub · parent 26c3a62 · Browse files · 3 files+9 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+9 added−3 removed

  • other:+9 added−3 removed

3 files+9 added−3 removed

modified:context_test.go+2 added-2 removed
1908 unmodified lines
1909 // Tests exercising the TrustedProxies functionality1910 resetContextForClientIPTests(c)19111912 // IPv6 support1913 c.Request.RemoteAddr = "[::1]:12345"1913 c.Request.RemoteAddr = fmt.Sprintf("[%s]:12345", localhostIPv6)1914 assert.Equal(t, "20.20.20.20", c.ClientIP())19151916 resetContextForClientIPTests(c)1917 // No trusted proxies
1293 unmodified lines
3211 must(s.Serve(l))3212 }()32133214 addr := strings.Split(l.Addr().String(), ":")3215 res, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/", addr[len(addr)-1]))3215 res, err := http.Get(fmt.Sprintf("http://%s:%s/", localhostIP, addr[len(addr)-1]))3216 if err != nil {3217 t.Error(fmt.Errorf("request error: %w", err))3218 return3219 }
460 unmodified lines
modified:gin_test.go+1 added-1 removed
81 unmodified lines
82 assert.Equal(t, "<h1>Hello world</h1>", string(resp))83}8485func TestH2c(t *testing.T) {86 ln, err := net.Listen("tcp", "127.0.0.1:0")86 ln, err := net.Listen("tcp", localhostIP+":0")87 if err != nil {88 t.Error(err)89 }90 r := Default()
996 unmodified lines
modified:utils.go+6 added
17 unmodified lines
1819// BindKey indicates a default bind key.20const BindKey = "_gin-gonic/gin/bindkey"2122// localhostIP indicates the default localhost IP address.23const localhostIP = "127.0.0.1"2425// localhostIPv6 indicates the default localhost IPv6 address.26const localhostIPv6 = "::1"2728// Bind is a helper function for given interface object and returns a Gin middleware.29func Bind(val any) HandlerFunc {30 value := reflect.ValueOf(val)31 if value.Kind() == reflect.Ptr {
156 unmodified lines