MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / TestReplaceIP

Method TestReplaceIP

server/router_test.go:242–308  ·  view source on GitHub ↗

Test IP address handling

()

Source from the content-addressed store, hash-verified

240
241// Test IP address handling
242func (s *RouterTestSuite) TestReplaceIP() {
243 var capturedRemoteAddr string
244 handler := func(reqID string, rw server.ResponseWriter, req *http.Request) *server.Error {
245 capturedRemoteAddr = req.RemoteAddr
246 rw.WriteHeader(http.StatusOK)
247 return nil
248 }
249
250 s.router.GET("/test", handler)
251
252 tests := []struct {
253 name string
254 originalAddr string
255 headers map[string]string
256 expectedAddr string
257 }{
258 {
259 name: "CFConnectingIP",
260 originalAddr: "original:8080",
261 headers: map[string]string{
262 httpheaders.CFConnectingIP: "1.2.3.4",
263 },
264 expectedAddr: "1.2.3.4:8080",
265 },
266 {
267 name: "XForwardedForMulti",
268 originalAddr: "original:8080",
269 headers: map[string]string{
270 httpheaders.XForwardedFor: "5.6.7.8, 9.10.11.12",
271 },
272 expectedAddr: "5.6.7.8:8080",
273 },
274 {
275 name: "XForwardedForSingle",
276 originalAddr: "original:8080",
277 headers: map[string]string{
278 httpheaders.XForwardedFor: "13.14.15.16",
279 },
280 expectedAddr: "13.14.15.16:8080",
281 },
282 {
283 name: "XRealIP",
284 originalAddr: "original:8080",
285 headers: map[string]string{
286 httpheaders.XRealIP: "17.18.19.20",
287 },
288 expectedAddr: "17.18.19.20:8080",
289 },
290 }
291
292 for _, tt := range tests {
293 s.Run(tt.name, func() {
294 req := httptest.NewRequest(http.MethodGet, "/api/test", nil)
295 req.RemoteAddr = tt.originalAddr
296
297 for header, value := range tt.headers {
298 req.Header.Set(header, value)
299 }

Callers

nothing calls this directly

Calls 6

WriteHeaderMethod · 0.80
RunMethod · 0.80
NewRequestMethod · 0.80
ServeHTTPMethod · 0.80
GETMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected