| 11 | ) |
| 12 | |
| 13 | func TestEventRequestRealIP(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | |
| 16 | headers := map[string][]string{ |
| 17 | "CF-Connecting-IP": {"1.2.3.4", "1.1.1.1"}, |
| 18 | "Fly-Client-IP": {"1.2.3.4", "1.1.1.2"}, |
| 19 | "X-Real-IP": {"1.2.3.4", "1.1.1.3,1.1.1.4"}, |
| 20 | "X-Forwarded-For": {"1.2.3.4", "invalid,1.1.1.5,1.1.1.6,invalid"}, |
| 21 | } |
| 22 | |
| 23 | scenarios := []struct { |
| 24 | name string |
| 25 | headers map[string][]string |
| 26 | trustedHeaders []string |
| 27 | useLeftmostIP bool |
| 28 | expected string |
| 29 | }{ |
| 30 | { |
| 31 | "no trusted headers", |
| 32 | headers, |
| 33 | nil, |
| 34 | false, |
| 35 | "127.0.0.1", |
| 36 | }, |
| 37 | { |
| 38 | "non-matching trusted header", |
| 39 | headers, |
| 40 | []string{"header1", "header2"}, |
| 41 | false, |
| 42 | "127.0.0.1", |
| 43 | }, |
| 44 | { |
| 45 | "trusted X-Real-IP (rightmost)", |
| 46 | headers, |
| 47 | []string{"header1", "x-real-ip", "x-forwarded-for"}, |
| 48 | false, |
| 49 | "1.1.1.4", |
| 50 | }, |
| 51 | { |
| 52 | "trusted X-Real-IP (leftmost)", |
| 53 | headers, |
| 54 | []string{"header1", "x-real-ip", "x-forwarded-for"}, |
| 55 | true, |
| 56 | "1.1.1.3", |
| 57 | }, |
| 58 | { |
| 59 | "trusted X-Forwarded-For (rightmost)", |
| 60 | headers, |
| 61 | []string{"header1", "x-forwarded-for"}, |
| 62 | false, |
| 63 | "1.1.1.6", |
| 64 | }, |
| 65 | { |
| 66 | "trusted X-Forwarded-For (leftmost)", |
| 67 | headers, |
| 68 | []string{"header1", "x-forwarded-for"}, |
| 69 | true, |
| 70 | "1.1.1.5", |