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

Function TestProxyRealIPHeader

middleware/proxy_test.go:137–178  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

135}
136
137func TestProxyRealIPHeader(t *testing.T) {
138 // Setup
139 upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
140 defer upstream.Close()
141 url, _ := url.Parse(upstream.URL)
142 rrb := NewRoundRobinBalancer([]*ProxyTarget{{Name: "upstream", URL: url}})
143 e := echo.New()
144 e.Use(ProxyWithConfig(ProxyConfig{Balancer: rrb}))
145 req := httptest.NewRequest(http.MethodGet, "/", nil)
146 rec := httptest.NewRecorder()
147
148 remoteAddrIP, _, _ := net.SplitHostPort(req.RemoteAddr)
149 realIPHeaderIP := "203.0.113.1"
150 extractedRealIP := "203.0.113.10"
151 tests := []*struct {
152 hasRealIPheader bool
153 hasIPExtractor bool
154 expectedXRealIP string
155 }{
156 {false, false, remoteAddrIP},
157 {false, true, extractedRealIP},
158 {true, false, realIPHeaderIP},
159 {true, true, extractedRealIP},
160 }
161
162 for _, tt := range tests {
163 if tt.hasRealIPheader {
164 req.Header.Set(echo.HeaderXRealIP, realIPHeaderIP)
165 } else {
166 req.Header.Del(echo.HeaderXRealIP)
167 }
168 if tt.hasIPExtractor {
169 e.IPExtractor = func(*http.Request) string {
170 return extractedRealIP
171 }
172 } else {
173 e.IPExtractor = nil
174 }
175 e.ServeHTTP(rec, req)
176 assert.Equal(t, tt.expectedXRealIP, req.Header.Get(echo.HeaderXRealIP), "hasRealIPheader: %t / hasIPExtractor: %t", tt.hasRealIPheader, tt.hasIPExtractor)
177 }
178}
179
180func TestProxyRewrite(t *testing.T) {
181 var testCases = []struct {

Callers

nothing calls this directly

Calls 7

NewRoundRobinBalancerFunction · 0.85
ProxyWithConfigFunction · 0.85
SetMethod · 0.80
ServeHTTPMethod · 0.80
CloseMethod · 0.45
UseMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…