|
| 1 | +// Copyright 2025 The Outline Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package net |
| 16 | + |
| 17 | +import ( |
| 18 | + "net" |
| 19 | + "net/http" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestGetClientIPFromRequest(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + headers map[string]string |
| 29 | + remoteAddr string |
| 30 | + wantIP string |
| 31 | + wantErr bool |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "X-Forwarded-For (Single IP)", |
| 35 | + headers: map[string]string{"X-Forwarded-For": "10.0.0.1"}, |
| 36 | + wantIP: "10.0.0.1", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "X-Forwarded-For (Multiple IPs)", |
| 40 | + headers: map[string]string{"X-Forwarded-For": "10.0.0.1, 172.16.0.1"}, |
| 41 | + wantIP: "10.0.0.1", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "X-Real-IP", |
| 45 | + headers: map[string]string{"X-Real-IP": "192.168.2.200"}, |
| 46 | + wantIP: "192.168.2.200", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Forwarded", |
| 50 | + headers: map[string]string{"Forwarded": "for=192.168.3.100"}, |
| 51 | + wantIP: "192.168.3.100", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "RemoteAddr (host:port)", |
| 55 | + remoteAddr: "172.17.0.1:12345", |
| 56 | + wantIP: "172.17.0.1", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "RemoteAddr (IP only)", |
| 60 | + remoteAddr: "172.17.0.1", |
| 61 | + wantErr: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "No Headers, No RemoteAddr", |
| 65 | + wantErr: true, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "Invalid IP in header", |
| 69 | + headers: map[string]string{"X-Forwarded-For": "invalid-ip"}, |
| 70 | + wantErr: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "Invalid RemoteAddr", |
| 74 | + remoteAddr: "invalid-ip:port", |
| 75 | + wantErr: true, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + r := &http.Request{ |
| 82 | + Header: make(http.Header), |
| 83 | + RemoteAddr: tt.remoteAddr, |
| 84 | + } |
| 85 | + for h, v := range tt.headers { |
| 86 | + r.Header.Set(h, v) |
| 87 | + } |
| 88 | + |
| 89 | + gotIP, err := GetClientIPFromRequest(r) |
| 90 | + if !tt.wantErr { |
| 91 | + require.NoError(t, err) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + wantIP := net.ParseIP(tt.wantIP) |
| 96 | + if !gotIP.Equal(wantIP) { |
| 97 | + t.Errorf("err = %v, want %v", gotIP, wantIP) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments