[tor-commits] [bridgedb/develop] Fix X-Forwarded-For unittests in CaptchaProtectedResourceTests.

isis at torproject.org isis at torproject.org
Wed Mar 26 05:49:31 UTC 2014


commit bbfa41304d0b4a7ec30bceadfc6bc5b3b923a7d7
Author: Isis Lovecruft <isis at torproject.org>
Date:   Wed Mar 19 18:00:49 2014 +0000

    Fix X-Forwarded-For unittests in CaptchaProtectedResourceTests.
    
    Apparently, we can't do:
    
        request.setHeader('X-Forwarded-For', '1.1.1.1')
    
    because then BridgeDB never finds the header. Instead, we must do:
    
        request.headers.update({'X-Forwarded-For': '1.1.1.1'})
---
 lib/bridgedb/test/test_HTTPServer.py |   34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/lib/bridgedb/test/test_HTTPServer.py b/lib/bridgedb/test/test_HTTPServer.py
index cd55b46..507b1f0 100644
--- a/lib/bridgedb/test/test_HTTPServer.py
+++ b/lib/bridgedb/test/test_HTTPServer.py
@@ -86,19 +86,39 @@ class CaptchaProtectedResourceTests(unittest.TestCase):
         finally:
             HTTPServer.lookup = oldLookup
 
+    def createRequestWithIPs(self):
+        """Set the IP address returned from ``request.getClientIP()`` to
+        '3.3.3.3', and the IP address reported in the 'X-Forwarded-For' header
+        to '2.2.2.2'.
+        """
+        request = DummyRequest([self.pagename])
+        # Since we do not set ``request.getClientIP`` here like we do in some
+        # of the other unittests, an exception would be raised here if
+        # ``getBridgesForRequest()`` is unable to get the IP address from this
+        # 'X-Forwarded-For' header (because ``ip`` would get set to ``None``).
+        request.headers.update({'x-forwarded-for': '2.2.2.2'})
+        # See :api:`twisted.test.requesthelper.DummyRequest.getClientIP`
+        request.client = requesthelper.IPv4Address('TCP', '3.3.3.3', 443)
+        request.method = b'GET'
+        return request
+
     def test_getClientIP_XForwardedFor(self):
         """CaptchaProtectedResource.getClientIP() should return the IP address
         from the 'X-Forwarded-For' header when ``useForwardedHeader=True``.
         """
-        requestIP = b'6.6.6.6'
-        request = requesthelper.DummyRequest([self.pagename])
-        request.setHeader(b'X-Forwarded-For', requestIP)
-        request.method = b'GET'
+        self.captchaResource.useForwardedHeader = True
+        request = self.createRequestWithIPs()
+        clientIP = self.captchaResource.getClientIP(request)
+        self.assertEqual(clientIP, '2.2.2.2')
 
-        #child = root.getChild(pagename, request)
-        page = self.captchaResource.render_GET(request)
+    def test_getClientIP_fromRequest(self):
+        """CaptchaProtectedResource.getClientIP() should return the IP address
+        from the request instance when ``useForwardedHeader=False``.
+        """
+        self.captchaResource.useForwardedHeader = False
+        request = self.createRequestWithIPs()
         clientIP = self.captchaResource.getClientIP(request)
-        #self.assertEquals(requestIP, clientIP)
+        self.assertEqual(clientIP, '3.3.3.3')
 
     def test_render_POST(self):
         """render_POST() with a wrong 'captcha_response_field' should return





More information about the tor-commits mailing list