コード例 #1
0
def test_get_client_ip(ip_address, expected_ip):
    """Test providing a valid IP in X-Forwarded-For returns the valid IP.
    Otherwise, if no valid IP were found, returns the requester's IP.
    """
    expected_ip = expected_ip
    headers = {"HTTP_X_FORWARDED_FOR": ip_address} if ip_address else {}
    request = RequestFactory(**headers).get("/")
    assert get_client_ip(request) == expected_ip
コード例 #2
0
 def middleware(request):
     client_ip = get_client_ip(request)
     request.region = None
     if client_ip and request.country:
         country, region = get_country_region_by_ip(client_ip)
         # If `request.country` doesn't match the one returned by geolite2,
         # then the region doesn't exist in the country, so don't set it.
         if country == request.country:
             request.region = region
     return get_response(request)