[tor-commits] [bridgedb/develop] Fix issue with transport URL params in HTTPServer unittests.

isis at torproject.org isis at torproject.org
Thu Apr 17 05:10:02 UTC 2014


commit 5e37dc53df4704c9b4ab0f3943159a5198f0c515
Author: Isis Lovecruft <isis at torproject.org>
Date:   Wed Apr 16 19:26:19 2014 +0000

    Fix issue with transport URL params in HTTPServer unittests.
    
    Also fixes several PEP8 whitespace issues in test_HTTPServer.py.
---
 lib/bridgedb/test/test_HTTPServer.py |   83 +++++++++++++++++++++-------------
 1 file changed, 52 insertions(+), 31 deletions(-)

diff --git a/lib/bridgedb/test/test_HTTPServer.py b/lib/bridgedb/test/test_HTTPServer.py
index 111adc1..52c2fb7 100644
--- a/lib/bridgedb/test/test_HTTPServer.py
+++ b/lib/bridgedb/test/test_HTTPServer.py
@@ -178,8 +178,8 @@ class GimpCaptchaProtectedResourceTests(unittest.TestCase):
 
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', expectedChallenge)
-        self.request.addArg('captcha_response_field', expectedResponse) 
-        
+        self.request.addArg('captcha_response_field', expectedResponse)
+
         response = self.captchaResource.extractClientSolution(self.request)
         (challenge, response) = response
         self.assertEqual(challenge, expectedChallenge)
@@ -192,8 +192,8 @@ class GimpCaptchaProtectedResourceTests(unittest.TestCase):
 
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', expectedChallenge)
-        self.request.addArg('captcha_response_field', expectedResponse) 
-        
+        self.request.addArg('captcha_response_field', expectedResponse)
+
         valid = self.captchaResource.checkSolution(self.request)
         self.assertFalse(valid)
 
@@ -238,7 +238,7 @@ class GimpCaptchaProtectedResourceTests(unittest.TestCase):
         """
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', '')
-        self.request.addArg('captcha_response_field', '') 
+        self.request.addArg('captcha_response_field', '')
 
         page = self.captchaResource.render_POST(self.request)
         self.assertEqual(BeautifulSoup(page).find('meta')['http-equiv'],
@@ -253,7 +253,7 @@ class GimpCaptchaProtectedResourceTests(unittest.TestCase):
 
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', expectedChallenge)
-        self.request.addArg('captcha_response_field', expectedResponse) 
+        self.request.addArg('captcha_response_field', expectedResponse)
 
         page = self.captchaResource.render_POST(self.request)
         self.assertEqual(BeautifulSoup(page).find('meta')['http-equiv'],
@@ -357,8 +357,8 @@ class ReCaptchaProtectedResourceTests(unittest.TestCase):
         """
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', '')
-        self.request.addArg('captcha_response_field', '') 
-        
+        self.request.addArg('captcha_response_field', '')
+
         self.assertEqual((False, self.request),
                          self.successResultOf(
                              self.captchaResource.checkSolution(self.request)))
@@ -398,7 +398,7 @@ class ReCaptchaProtectedResourceTests(unittest.TestCase):
         """
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', '')
-        self.request.addArg('captcha_response_field', '') 
+        self.request.addArg('captcha_response_field', '')
 
         page = self.captchaResource.render_POST(self.request)
         self.assertEqual(page, HTTPServer.server.NOT_DONE_YET)
@@ -412,7 +412,7 @@ class ReCaptchaProtectedResourceTests(unittest.TestCase):
 
         self.request.method = b'POST'
         self.request.addArg('captcha_challenge_field', expectedChallenge)
-        self.request.addArg('captcha_response_field', expectedResponse) 
+        self.request.addArg('captcha_response_field', expectedResponse)
 
         page = self.captchaResource.render_POST(self.request)
         self.assertEqual(page, HTTPServer.server.NOT_DONE_YET)
@@ -580,34 +580,51 @@ class WebResourceBridgesTests(unittest.TestCase):
 
         page = self.bridgesResource.render(request)
         self.bridgesResource.useForwardedHeader = False  # Reset it
-        self.assertSubstring("To enter bridges into", page)
+
+        # The response should explain how to use the bridge lines:
+        self.assertTrue("To enter bridges into Tor Browser" in str(page))
 
     def test_render_GET_RTLlang(self):
-        """Test rendering a request for obfs3 bridges in Arabic."""
-        request = DummyRequest(["bridges?transport=obfs3"])
+        """Test rendering a request for plain bridges in Arabic."""
+        request = DummyRequest([b"bridges?transport=obfs3"])
+        request.method = b'GET'
+        request.getClientIP = lambda: '3.3.3.3'
+        # For some strange reason, the 'Accept-Language' value *should not* be
+        # a list, unlike all the other headers and args…
+        request.headers.update({'accept-language': 'ar,en,en_US,'})
+
+        page = self.bridgesResource.render(request)
+        self.assertSubstring("direction: rtl", page)
+        self.assertSubstring(
+            "إذا لم يعمل تور بنجاح معك، يجب عليك ارسال بريد إلكتروني إلي", page)
+
+        for bridgeLine in self.parseBridgesFromHTMLPage(page):
+            # Check that each bridge line had the expected number of fields:
+            bridgeLine = bridgeLine.split(' ')
+            self.assertEqual(len(bridgeLine), 2)
+
+    def test_render_GET_RTLlang_obfs3(self):
+        """Test rendering a request for obfs3 bridges in Farsi."""
+        request = DummyRequest([b"bridges?transport=obfs3"])
         request.method = b'GET'
         request.getClientIP = lambda: '3.3.3.3'
-        request.headers.update({'accept-language': 'ar'})
+        request.headers.update({'accept-language': 'fa,en,en_US,'})
         # We actually have to set the request args manually when using a
         # DummyRequest:
-        request.args.update({'transport': 'obfs3'})
+        request.args.update({'transport': ['obfs3']})
 
         page = self.bridgesResource.render(request)
         self.assertSubstring("direction: rtl", page)
         self.assertSubstring(
-            "إذا لم يعمل تور بنجاح معك، يجب عليك ارسال بريد إلكتروني إلي", page)
+            # "I need an alternative way to get bridges!"
+            "به یک راه دیگر برای دریافت پل‌ها احتیاج دارم!",
+            page)
 
         for bridgeLine in self.parseBridgesFromHTMLPage(page):
             # Check that each bridge line had the expected number of fields:
             bridgeLine = bridgeLine.split(' ')
             self.assertEqual(len(bridgeLine), 3)
-
-            print("""
-            FIXME: The first field should be the transport method:
-            DummyBridge.getConfigLine() receives transport='o', not 'obfs3'.
-            What the hell?
-            """)
-            #self.assertEqual(bridgeLine[0], 'obfs3')
+            self.assertEqual(bridgeLine[0], 'obfs3')
 
             # Check that the IP and port seem okay:
             ip, port = bridgeLine[1].rsplit(':')
@@ -616,24 +633,28 @@ class WebResourceBridgesTests(unittest.TestCase):
             self.assertGreater(int(port), 0)
             self.assertLessEqual(int(port), 65535)
 
+
     def test_renderAnswer_textplain(self):
         """If the request format specifies 'plain', we should return content
         with mimetype 'text/plain'.
         """
         request = DummyRequest([self.pagename])
-        request.args.update({'format': 'plain'})
+        request.args.update({'format': ['plain']})
         request.getClientIP = lambda: '4.4.4.4'
         request.method = b'GET'
 
         page = self.bridgesResource.render(request)
+        self.assertTrue("html" not in str(page))
 
-        print("""
-        FIXME: The result page should have a mimetype of 'text/plain', and yet
-        it's still HTML.
-        """)
-        #self.assertNotSubstring("html", page)
+        # We just need to strip and split it because it looks like:
+        #
+        #   94.235.85.233:9492 0d9d0547c3471cddc473f7288a6abfb54562dc06
+        #   255.225.204.145:9511 1fb89d618b3a12afe3529fd072127ea08fb50466
+        #
+        # (Yes, there are two leading spaces at the beginning of each line)
+        #
+        bridgeLines = [line.strip() for line in page.strip().split('\n')]
 
-        bridgeLines = self.parseBridgesFromHTMLPage(page)
         for bridgeLine in bridgeLines:
             bridgeLine = bridgeLine.split(' ')
             self.assertEqual(len(bridgeLine), 2)
@@ -665,7 +686,7 @@ class WebResourceOptionsTests(unittest.TestCase):
         request.headers.update({'accept-language': 'he'})
         # We actually have to set the request args manually when using a
         # DummyRequest:
-        request.args.update({'transport': 'obfs2'})
+        request.args.update({'transport': ['obfs2']})
 
         page = self.optionsResource.render(request)
         self.assertSubstring("direction: rtl", page)





More information about the tor-commits mailing list