[tor-commits] [oonib/master] Implement unittests for HTTP test helpers.

art at torproject.org art at torproject.org
Mon Sep 1 09:54:04 UTC 2014


commit 5ee55d4d69a1ac846ebb5f57bbc0a938b113b202
Author: Arturo Filastò <art at fuffa.org>
Date:   Fri Aug 29 13:09:01 2014 +0200

    Implement unittests for HTTP test helpers.
    
    Fix some bugs found in unittesting.
---
 oonib/test/handler_helpers.py     |    5 +++--
 oonib/test/test_testhelpers.py    |   28 ++++++++++++++++++++++++++++
 oonib/testhelpers/__init__.py     |    5 -----
 oonib/testhelpers/http_helpers.py |   12 ++++++++++--
 4 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/oonib/test/handler_helpers.py b/oonib/test/handler_helpers.py
index 4e9905d..06beb6d 100644
--- a/oonib/test/handler_helpers.py
+++ b/oonib/test/handler_helpers.py
@@ -79,11 +79,12 @@ class HandlerTestCase(unittest.TestCase):
             self._listener.stopListening()
 
     @defer.inlineCallbacks
-    def request(self, path, method="GET", postdata=None):
+    def request(self, path, method="GET", postdata=None, headers={}):
         url = "http://localhost:%s%s" % (self.port, path)
         if isinstance(postdata, dict):
             postdata = json.dumps(postdata)
 
         response = yield httpclient.fetch(url, method=method,
-                                          postdata=postdata)
+                                          postdata=postdata,
+                                          headers=headers)
         defer.returnValue(response)
diff --git a/oonib/test/test_testhelpers.py b/oonib/test/test_testhelpers.py
new file mode 100644
index 0000000..1f1a5da
--- /dev/null
+++ b/oonib/test/test_testhelpers.py
@@ -0,0 +1,28 @@
+import json
+from twisted.internet import defer
+
+from oonib.test.handler_helpers import HandlerTestCase
+from oonib.testhelpers import http_helpers
+
+
+class TestHTTPReturnJSONHeaders(HandlerTestCase):
+    app = http_helpers.HTTPReturnJSONHeadersHelper()
+
+    @defer.inlineCallbacks
+    def test_get_request(self):
+        response = yield self.request('/', "GET",
+                                      headers={'X-Antani': ['Spam']})
+        response_body = json.loads(response.body)
+        self.assertIn("headers_dict", response_body)
+        self.assertIn("X-Antani", response_body['headers_dict'])
+        self.assertIn(["X-Antani", "Spam"], response_body['request_headers'])
+        self.assertEqual("GET / HTTP/1.1", response_body['request_line'])
+
+
+class TestHTTPRandomPage(HandlerTestCase):
+    app = http_helpers.HTTPRandomPageHelper
+
+    @defer.inlineCallbacks
+    def test_get_request(self):
+        response = yield self.request('/100/ham', "GET")
+        self.assertEqual(len(response.body), 104)
diff --git a/oonib/testhelpers/__init__.py b/oonib/testhelpers/__init__.py
index 3a0e438..e43a702 100644
--- a/oonib/testhelpers/__init__.py
+++ b/oonib/testhelpers/__init__.py
@@ -1,6 +1 @@
-from . import dns_helpers
-from . import http_helpers
-from . import ssl_helpers
-from . import tcp_helpers
-
 __all__ = ['dns_helpers', 'http_helpers', 'ssl_helpers', 'tcp_helpers']
diff --git a/oonib/testhelpers/http_helpers.py b/oonib/testhelpers/http_helpers.py
index 8e03164..a28cbad 100644
--- a/oonib/testhelpers/http_helpers.py
+++ b/oonib/testhelpers/http_helpers.py
@@ -85,7 +85,7 @@ class SimpleHTTPChannel(basic.LineReceiver, policies.TimeoutMixin):
 
         if len(self.headers) >= self.maxHeaders:
             log.err("Maximum number of headers received.")
-            return self.transport.loseConnection()
+            self.closeConnection()
 
     def allHeadersReceived(self):
         headers_dict = {}
@@ -102,6 +102,14 @@ class SimpleHTTPChannel(basic.LineReceiver, policies.TimeoutMixin):
         json_response = json.dumps(response)
         self.transport.write('HTTP/1.1 200 OK\r\n\r\n')
         self.transport.write('%s' % json_response)
+
+        self.closeConnection()
+
+    def closeConnection(self):
+        if self._TimeoutMixin__timeoutCall and \
+                not self._TimeoutMixin__timeoutCall.called:
+            self._TimeoutMixin__timeoutCall.cancel()
+            self._TimeoutMixin__timeoutCall = None
         self.transport.loseConnection()
 
 
@@ -158,7 +166,7 @@ class HTTPRandomPage(HTTPTrapAll):
         length = 100
         if length > 100000:
             length = 100000
-        return self.genRandomPage(length, keyword)
+        self.write(self.genRandomPage(length, keyword))
 
 HTTPRandomPageHelper = Application([
     # XXX add regexps here



More information about the tor-commits mailing list