@@ -175,39 +175,69 @@ def test_proxy_order_right_most(self):
175175class TestIPv4ProxyCount (unittest .TestCase ):
176176 """IPv4 Proxy Count Test"""
177177
178- def setUp (self ):
179- self .ipware = IpWare (proxy_count = 1 )
180-
181- def tearDown (self ):
182- self .ipware = None
183-
184- def test_singleton_proxy_count (self ):
178+ def test_proxy_count_one_missing_proxy_fail (self ):
179+ ipware = IpWare (proxy_count = 1 )
185180 meta = {
186181 "HTTP_X_FORWARDED_FOR" : "177.139.233.139" ,
187182 }
188- r = self .ipware .get_client_ip (meta )
183+ r = ipware .get_client_ip (meta )
184+
189185 self .assertEqual (r , (None , False ))
190186
191- def test_singleton_proxy_count_private (self ):
187+ def test_proxy_count_one_at_least_one_proxy_pass (self ):
188+ ipware = IpWare (proxy_count = 1 )
192189 meta = {
193- "HTTP_X_FORWARDED_FOR" : "10.0.0.0" ,
194- "HTTP_X_REAL_IP" : "177.139.233.139" ,
190+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157, 198.84.193.158" ,
195191 }
196- r = self .ipware .get_client_ip (meta )
192+ r = ipware .get_client_ip (meta )
193+ self .assertEqual (r , (IPv4Address ("198.84.193.157" ), True ))
194+
195+ def test_proxy_count_one_exactly_one_proxy_fail (self ):
196+ ipware = IpWare (proxy_count = 1 )
197+ meta = {
198+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157, 198.84.193.158" ,
199+ }
200+ r = ipware .get_client_ip (meta , strict = True )
197201 self .assertEqual (r , (None , False ))
198202
199- def test_proxy_count_relax (self ):
203+ def test_proxy_count_one_exactly_one_proxy_pass (self ):
204+ ipware = IpWare (proxy_count = 1 )
205+ meta = {
206+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157" ,
207+ }
208+ r = ipware .get_client_ip (meta , strict = True )
209+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), True ))
210+
211+ def test_proxy_count_one_dont_care_proxy_pass (self ):
212+ ipware = IpWare ()
200213 meta = {
201214 "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157, 198.84.193.158" ,
202215 }
203- r = self . ipware .get_client_ip (meta , strict = False )
204- self .assertEqual (r , (IPv4Address ("198.84.193.157 " ), True ))
216+ r = ipware .get_client_ip (meta )
217+ self .assertEqual (r , (IPv4Address ("177.139.233.139 " ), False ))
205218
206- def test_proxy_count_strict (self ):
219+ def test_proxy_count_zero_dont_care_proxy_pass (self ):
220+ ipware = IpWare (proxy_count = 0 )
207221 meta = {
208- "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139 , 198.84.193.158" ,
222+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157 , 198.84.193.158" ,
209223 }
210- r = self .ipware .get_client_ip (meta , strict = True )
224+ r = ipware .get_client_ip (meta )
225+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), False ))
226+
227+ def test_proxy_count_zero_exact_zero_proxy_pass (self ):
228+ ipware = IpWare (proxy_count = 0 )
229+ meta = {
230+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139" ,
231+ }
232+ r = ipware .get_client_ip (meta , strict = True )
233+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), False ))
234+
235+ def test_proxy_count_zero_exact_zero_proxy_fail (self ):
236+ ipware = IpWare (proxy_count = 0 )
237+ meta = {
238+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157, 198.84.193.158" ,
239+ }
240+ r = ipware .get_client_ip (meta , strict = True )
211241 self .assertEqual (r , (None , False ))
212242
213243
@@ -245,26 +275,68 @@ def test_proxy_list_success(self):
245275class TestIPv4ProxyCountProxyList (unittest .TestCase ):
246276 """IPv4 Proxy Count Test"""
247277
248- def setUp (self ):
249- self .ipware = IpWare (
250- proxy_count = 2 , proxy_list = ["198.84.193.157" , "198.84.193.158" ]
251- )
278+ def test_proxy_list_relax (self ):
279+ ipware = IpWare (proxy_list = ["198.84.193.157" , "198.84.193.158" ])
280+ meta = {
281+ "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
282+ }
283+ r = ipware .get_client_ip (meta )
284+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), True ))
252285
253- def tearDown (self ):
254- self .ipware = None
286+ def test_proxy_list_strict_pass (self ):
287+ ipware = IpWare (proxy_list = ["198.84.193.157" , "198.84.193.158" ])
288+ meta = {
289+ "HTTP_X_FORWARDED_FOR" : "177.139.233.139, 198.84.193.157, 198.84.193.158" ,
290+ }
291+ r = ipware .get_client_ip (meta , strict = True )
292+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), True ))
255293
256- def test_proxy_list_relax (self ):
294+ def test_proxy_list_strict_fail (self ):
295+ ipware = IpWare (proxy_list = ["198.84.193.157" , "198.84.193.158" ])
257296 meta = {
258297 "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
259298 }
260- r = self .ipware .get_client_ip (meta )
299+ r = ipware .get_client_ip (meta , strict = True )
300+ self .assertEqual (r , (None , False ))
301+
302+ def test_proxy_list_relax_exact_pass (self ):
303+ ipware = IpWare (proxy_count = 2 , proxy_list = ["198.84.193.157" , "198.84.193.158" ])
304+ meta = {
305+ "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
306+ }
307+ r = ipware .get_client_ip (meta )
261308 self .assertEqual (r , (IPv4Address ("177.139.233.139" ), True ))
262309
263- def test_proxy_list_strict (self ):
310+ def test_proxy_list_relax_count_under_pass (self ):
311+ ipware = IpWare (proxy_count = 1 , proxy_list = ["198.84.193.157" , "198.84.193.158" ])
264312 meta = {
265313 "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
266314 }
267- r = self .ipware .get_client_ip (meta , strict = True )
315+ r = ipware .get_client_ip (meta )
316+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), True ))
317+
318+ def test_proxy_list_relax_count_over_pass (self ):
319+ ipware = IpWare (proxy_count = 5 , proxy_list = ["198.84.193.157" , "198.84.193.158" ])
320+ meta = {
321+ "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
322+ }
323+ r = ipware .get_client_ip (meta )
324+ self .assertEqual (r , (None , False ))
325+
326+ def test_proxy_list_count_exact_pass (self ):
327+ ipware = IpWare (proxy_count = 2 , proxy_list = ["198.84.193.157" , "198.84.193.158" ])
328+ meta = {
329+ "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
330+ }
331+ r = ipware .get_client_ip (meta )
332+ self .assertEqual (r , (IPv4Address ("177.139.233.139" ), True ))
333+
334+ def test_proxy_list_count_exact_fail (self ):
335+ ipware = IpWare (proxy_count = 4 , proxy_list = ["198.84.193.157" , "198.84.193.158" ])
336+ meta = {
337+ "HTTP_X_FORWARDED_FOR" : "177.139.233.138, 177.139.233.139, 198.84.193.157, 198.84.193.158" ,
338+ }
339+ r = ipware .get_client_ip (meta )
268340 self .assertEqual (r , (None , False ))
269341
270342
0 commit comments