def test_http_response(): with PluginTester(): # Run test cases. for title, kwargs in cases_http_response: print "Testing %s..." % title request = HTTP_Request("http://www.example.com/index.html") kw_1 = kwargs.copy() kw_2 = {"raw_response": kw_1.pop("raw_response")} kw_3 = kw_1.copy() kw_3["headers"] = HTTP_Headers(kw_3.pop("raw_headers")) for kw in (kw_1, kw_2, kw_3): response = HTTP_Response(request, **kw) assert response.identity in request.links assert request.identity in response.links assert str(response.headers) == kwargs["raw_headers"] for key, value in kwargs.iteritems(): if key == "raw_response" and "broken" in title: continue try: assert getattr(response, key) == value except AssertionError: print " key == %r" % key print " value == %r" % value print " getattr(response, key) == %r" % getattr( response, key) raise # Test HTTP 0.9. print "Testing HTTP 0.9 response..." request = HTTP_Request("http://www.example.com/index.html", version="0.9") response = HTTP_Response(request, data="hola manola") assert response.raw_response == "hola manola" assert response.raw_headers == None assert response.headers == None assert response.status == "200" assert response.reason == "OK" assert response.protocol == "HTTP" assert response.version == "0.9"
def __reconstruct_http(self, raw_url): url = URL(raw_url) req = HTTP_Request( method = "GET", url = raw_url, ) req.add_resource(url) resp = HTTP_Response( request = req, status = self.reconstruct_http_code[raw_url], headers = eval(self.reconstruct_http_headers[raw_url]), data = self.reconstruct_http_data[raw_url], ) self.reconstructed_http[raw_url] = resp.identity del self.reconstruct_http_code[raw_url] del self.reconstruct_http_headers[raw_url] del self.reconstruct_http_data[raw_url] return url, req, resp