commit d4ee153fc0c08627b3c4d36e1b614a14e15a9fa1 Author: Arturo Filastò art@fuffa.org Date: Sun Nov 18 18:36:43 2012 +0100
Refactor HTTP Test Template --- nettests/core/http_host.py | 3 +- ooni/templates/httpt.py | 54 ++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 30 deletions(-)
diff --git a/nettests/core/http_host.py b/nettests/core/http_host.py index 252561b..3912dfc 100644 --- a/nettests/core/http_host.py +++ b/nettests/core/http_host.py @@ -49,7 +49,8 @@ class HTTPHost(httpt.HTTPTest): """ headers = {} headers["Host"] = [self.input] - return self.doRequest(self.localOptions['backend'], headers=headers) + return self.doRequest(self.localOptions['backend'], + headers=headers)
def check_for_censorship(self, body): """ diff --git a/ooni/templates/httpt.py b/ooni/templates/httpt.py index 3a4201c..c2db6f5 100644 --- a/ooni/templates/httpt.py +++ b/ooni/templates/httpt.py @@ -44,6 +44,8 @@ class HTTPTest(NetTestCase):
randomizeUA = True followRedirects = False + request = {} + response = {}
def _setUp(self): log.debug("Setting up HTTPTest") @@ -67,8 +69,6 @@ class HTTPTest(NetTestCase): "This may make the testing less precise.") self.report['errors'].append("Could not import RedirectAgent")
- self.request = {} - self.response = {} self.processInputs() log.debug("Finished test setup")
@@ -111,7 +111,7 @@ class HTTPTest(NetTestCase): pass
def doRequest(self, url, method="GET", - headers=None, body=None, headers_processor=None, + headers={}, body=None, headers_processor=None, body_processor=None): """ Perform an HTTP request with the specified method. @@ -136,36 +136,17 @@ class HTTPTest(NetTestCase): """ log.debug("Performing request %s %s %s" % (url, method, headers))
- d = self.build_request(url, method, headers, body) - - def errback(failure): - failure.trap(ConnectionRefusedError, SOCKSError) - if type(failure.value) is ConnectionRefusedError: - log.err("Connection refused. The backend may be down") - else: - log.err("Sock error. The SOCK proxy may be down") - self.report["failure"] = str(failure.value) - - def finished(data): - return - - d.addErrback(errback) - d.addCallback(self._cbResponse, headers_processor, body_processor) - d.addCallback(finished) - return d - - def build_request(self, url, method="GET", - headers=None, body=None): self.request['method'] = method self.request['url'] = url - self.request['headers'] = headers if headers else {} + self.request['headers'] = headers self.request['body'] = body
if self.randomizeUA: + log.debug("Randomizing user agent") self.randomize_useragent()
+ log.debug("Writing to report the request") self.report['request'] = self.request - self.report['url'] = url
# If we have a request body payload, set the request body to such # content @@ -176,9 +157,25 @@ class HTTPTest(NetTestCase):
headers = Headers(self.request['headers'])
- req = self.agent.request(self.request['method'], self.request['url'], - headers, body_producer) - return req + d = self.agent.request(self.request['method'], + self.request['url'], headers, body_producer) + + def errback(failure): + print failure.value + failure.trap(ConnectionRefusedError, SOCKSError) + if type(failure.value) is ConnectionRefusedError: + log.err("Connection refused. The backend may be down") + else: + log.err("Sock error. The SOCK proxy may be down") + self.report["failure"] = str(failure.value) + + def finished(data): + return + + d.addErrback(errback) + d.addCallback(self._cbResponse, headers_processor, body_processor) + d.addCallback(finished) + return d
def _cbResponse(self, response, headers_processor, body_processor): log.debug("Got response %s" % response) @@ -211,4 +208,3 @@ class HTTPTest(NetTestCase): user_agent = random.choice(userAgents) self.request['headers']['User-Agent'] = [user_agent]
-
tor-commits@lists.torproject.org