(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestProxyHandler(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | proxy *config.Proxy |
| 14 | r *http.Request |
| 15 | expectedAddr string |
| 16 | }{ |
| 17 | { |
| 18 | name: "no proxy should forward default remote addr", |
| 19 | proxy: &config.Proxy{}, |
| 20 | r: &http.Request{ |
| 21 | RemoteAddr: "127.0.0.1:1234", |
| 22 | }, |
| 23 | expectedAddr: "127.0.0.1:1234", |
| 24 | }, |
| 25 | { |
| 26 | name: "proxy should forward proxy header X-Forwarded-For if set", |
| 27 | proxy: &config.Proxy{ |
| 28 | Enable: true, |
| 29 | }, |
| 30 | r: &http.Request{ |
| 31 | RemoteAddr: "127.0.0.1:1234", |
| 32 | Header: http.Header{ |
| 33 | "X-Forwarded-For": []string{"10.0.0.1, 10.3.2.1"}, |
| 34 | }, |
| 35 | }, |
| 36 | expectedAddr: "10.0.0.1", |
| 37 | }, |
| 38 | { |
| 39 | name: "proxy should ignore invalid IP values in the Proxy header", |
| 40 | proxy: &config.Proxy{ |
| 41 | Enable: true, |
| 42 | }, |
| 43 | r: &http.Request{ |
| 44 | RemoteAddr: "127.0.0.1:1234", |
| 45 | Header: http.Header{ |
| 46 | "X-Forwarded-For": []string{"nonsense"}, |
| 47 | }, |
| 48 | }, |
| 49 | expectedAddr: "127.0.0.1:1234", |
| 50 | }, |
| 51 | { |
| 52 | name: "proxy should forward proxy header X-Real-IP if set", |
| 53 | proxy: &config.Proxy{ |
| 54 | Enable: true, |
| 55 | }, |
| 56 | r: &http.Request{ |
| 57 | RemoteAddr: "127.0.0.1:1234", |
| 58 | Header: http.Header{ |
| 59 | "X-Real-Ip": []string{"10.0.0.1, 10.3.2.1"}, |
| 60 | }, |
| 61 | }, |
| 62 | expectedAddr: "10.0.0.1", |
| 63 | }, |
| 64 | { |
| 65 | name: "proxy should forward proxy header Forwarded if set", |
| 66 | proxy: &config.Proxy{ |
| 67 | Enable: true, |
nothing calls this directly
no test coverage detected