|
| 1 | +package httpcontext |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFromContext_EmptyContext(t *testing.T) { |
| 10 | + ctx := context.Background() |
| 11 | + hc := FromContext(ctx) |
| 12 | + |
| 13 | + if hc.UserAgent != "" { |
| 14 | + t.Errorf("expected empty UserAgent, got %q", hc.UserAgent) |
| 15 | + } |
| 16 | + if hc.ForwardedFor != "" { |
| 17 | + t.Errorf("expected empty ForwardedFor, got %q", hc.ForwardedFor) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestContextWithHTTPContext_RoundTrip(t *testing.T) { |
| 22 | + ctx := context.Background() |
| 23 | + expected := HTTPContext{ |
| 24 | + UserAgent: "TestAgent/1.0", |
| 25 | + ForwardedFor: "10.0.0.1, 192.168.1.1", |
| 26 | + } |
| 27 | + |
| 28 | + ctx = ContextWithHTTPContext(ctx, expected) |
| 29 | + actual := FromContext(ctx) |
| 30 | + |
| 31 | + if actual.UserAgent != expected.UserAgent { |
| 32 | + t.Errorf("expected UserAgent %q, got %q", expected.UserAgent, actual.UserAgent) |
| 33 | + } |
| 34 | + if actual.ForwardedFor != expected.ForwardedFor { |
| 35 | + t.Errorf("expected ForwardedFor %q, got %q", expected.ForwardedFor, actual.ForwardedFor) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestContextWithHTTPRequest_UserAgent(t *testing.T) { |
| 40 | + ctx := context.Background() |
| 41 | + req, _ := http.NewRequest("GET", "/", nil) |
| 42 | + req.Header.Set("User-Agent", "Claude-Desktop/1.2.3") |
| 43 | + req.RemoteAddr = "192.168.1.100:54321" |
| 44 | + |
| 45 | + ctx = ContextWithHTTPRequest(ctx, req) |
| 46 | + hc := FromContext(ctx) |
| 47 | + |
| 48 | + if hc.UserAgent != "Claude-Desktop/1.2.3" { |
| 49 | + t.Errorf("expected UserAgent %q, got %q", "Claude-Desktop/1.2.3", hc.UserAgent) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestContextWithHTTPRequest_XFFChainBuilding(t *testing.T) { |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + existingXFF string |
| 57 | + remoteAddr string |
| 58 | + expectedXFF string |
| 59 | + }{ |
| 60 | + { |
| 61 | + name: "no existing XFF", |
| 62 | + existingXFF: "", |
| 63 | + remoteAddr: "192.168.1.100:54321", |
| 64 | + expectedXFF: "192.168.1.100", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "with existing XFF", |
| 68 | + existingXFF: "10.0.0.1", |
| 69 | + remoteAddr: "192.168.1.100:54321", |
| 70 | + expectedXFF: "10.0.0.1, 192.168.1.100", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "multiple entries in existing XFF", |
| 74 | + existingXFF: "10.0.0.1, 172.16.0.1", |
| 75 | + remoteAddr: "192.168.1.100:54321", |
| 76 | + expectedXFF: "10.0.0.1, 172.16.0.1, 192.168.1.100", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + ctx := context.Background() |
| 83 | + req, _ := http.NewRequest("GET", "/", nil) |
| 84 | + if tt.existingXFF != "" { |
| 85 | + req.Header.Set("X-Forwarded-For", tt.existingXFF) |
| 86 | + } |
| 87 | + req.RemoteAddr = tt.remoteAddr |
| 88 | + |
| 89 | + ctx = ContextWithHTTPRequest(ctx, req) |
| 90 | + hc := FromContext(ctx) |
| 91 | + |
| 92 | + if hc.ForwardedFor != tt.expectedXFF { |
| 93 | + t.Errorf("expected ForwardedFor %q, got %q", tt.expectedXFF, hc.ForwardedFor) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestContextWithHTTPRequest_LastEntryDeduplication(t *testing.T) { |
| 100 | + ctx := context.Background() |
| 101 | + req, _ := http.NewRequest("GET", "/", nil) |
| 102 | + req.Header.Set("X-Forwarded-For", "10.0.0.1, 192.168.1.100") |
| 103 | + req.RemoteAddr = "192.168.1.100:54321" // Same as last entry in XFF |
| 104 | + |
| 105 | + ctx = ContextWithHTTPRequest(ctx, req) |
| 106 | + hc := FromContext(ctx) |
| 107 | + |
| 108 | + // Should not add duplicate |
| 109 | + expected := "10.0.0.1, 192.168.1.100" |
| 110 | + if hc.ForwardedFor != expected { |
| 111 | + t.Errorf("expected ForwardedFor %q (no duplicate), got %q", expected, hc.ForwardedFor) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestContextWithHTTPRequest_EarlierDuplicatePreserved(t *testing.T) { |
| 116 | + ctx := context.Background() |
| 117 | + req, _ := http.NewRequest("GET", "/", nil) |
| 118 | + // IP appears earlier in chain but not as last entry |
| 119 | + req.Header.Set("X-Forwarded-For", "192.168.1.100, 10.0.0.1") |
| 120 | + req.RemoteAddr = "192.168.1.100:54321" |
| 121 | + |
| 122 | + ctx = ContextWithHTTPRequest(ctx, req) |
| 123 | + hc := FromContext(ctx) |
| 124 | + |
| 125 | + // Should add it since it's not the last entry |
| 126 | + expected := "192.168.1.100, 10.0.0.1, 192.168.1.100" |
| 127 | + if hc.ForwardedFor != expected { |
| 128 | + t.Errorf("expected ForwardedFor %q (earlier duplicate preserved), got %q", expected, hc.ForwardedFor) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestGetClientIP_PortStripping(t *testing.T) { |
| 133 | + tests := []struct { |
| 134 | + name string |
| 135 | + remoteAddr string |
| 136 | + expected string |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "IPv4 with port", |
| 140 | + remoteAddr: "192.168.1.100:54321", |
| 141 | + expected: "192.168.1.100", |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "IPv6 with port", |
| 145 | + remoteAddr: "[::1]:54321", |
| 146 | + expected: "::1", |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "IPv4 without port", |
| 150 | + remoteAddr: "192.168.1.100", |
| 151 | + expected: "192.168.1.100", |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "empty string", |
| 155 | + remoteAddr: "", |
| 156 | + expected: "", |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tt := range tests { |
| 161 | + t.Run(tt.name, func(t *testing.T) { |
| 162 | + result := getClientIP(tt.remoteAddr) |
| 163 | + if result != tt.expected { |
| 164 | + t.Errorf("expected %q, got %q", tt.expected, result) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestBuildXFF(t *testing.T) { |
| 171 | + tests := []struct { |
| 172 | + name string |
| 173 | + existingXFF string |
| 174 | + remoteAddr string |
| 175 | + expected string |
| 176 | + }{ |
| 177 | + { |
| 178 | + name: "empty XFF, valid remoteAddr", |
| 179 | + existingXFF: "", |
| 180 | + remoteAddr: "192.168.1.100:54321", |
| 181 | + expected: "192.168.1.100", |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "existing XFF, valid remoteAddr", |
| 185 | + existingXFF: "10.0.0.1", |
| 186 | + remoteAddr: "192.168.1.100:54321", |
| 187 | + expected: "10.0.0.1, 192.168.1.100", |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "empty remoteAddr", |
| 191 | + existingXFF: "10.0.0.1", |
| 192 | + remoteAddr: "", |
| 193 | + expected: "10.0.0.1", |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "both empty", |
| 197 | + existingXFF: "", |
| 198 | + remoteAddr: "", |
| 199 | + expected: "", |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "XFF with spaces", |
| 203 | + existingXFF: "10.0.0.1, 172.16.0.1", |
| 204 | + remoteAddr: "192.168.1.100:54321", |
| 205 | + expected: "10.0.0.1, 172.16.0.1, 192.168.1.100", |
| 206 | + }, |
| 207 | + } |
| 208 | + |
| 209 | + for _, tt := range tests { |
| 210 | + t.Run(tt.name, func(t *testing.T) { |
| 211 | + result := buildXFF(tt.existingXFF, tt.remoteAddr) |
| 212 | + if result != tt.expected { |
| 213 | + t.Errorf("expected %q, got %q", tt.expected, result) |
| 214 | + } |
| 215 | + }) |
| 216 | + } |
| 217 | +} |
| 218 | + |
0 commit comments