Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def open(self):
self.opened()
response = _response.test_response("spam")
return ResponseCloseWrapper(response, self.closed, self._read)
req = Request("http://example.com")
# all 2xx are passed through
r = mechanize._response.test_response()
newr = h.http_response(req, r)
self.assertTrue(r is newr)
self.assertTrue(not hasattr(o, "proto")) # o.error not called
r = mechanize._response.test_response(code=202, msg="Accepted")
newr = h.http_response(req, r)
self.assertTrue(r is newr)
self.assertTrue(not hasattr(o, "proto")) # o.error not called
r = mechanize._response.test_response(code=206, msg="Partial content")
newr = h.http_response(req, r)
self.assertTrue(r is newr)
self.assertTrue(not hasattr(o, "proto")) # o.error not called
# anything else calls o.error (and MockOpener returns None, here)
r = mechanize._response.test_response(code=502, msg="Bad gateway")
self.assertTrue(h.http_response(req, r) is None)
self.assertEqual(o.proto, "http") # o.error called
self.assertEqual(o.args[:4], (req, r, 502, "Bad gateway"))
def make_response(self, encodings):
return mechanize._response.test_response(
headers=[("Content-type", "text/html; charset=\"%s\"" % encoding)
for encoding in encodings])
def open(self, fullurl, data=None,
timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
self.calls.append((fullurl, data, timeout))
headers = [("Foo", "Bar")]
if self._content_length is not None:
if self._content_length is True:
content_length = str(len(self.data))
else:
content_length = str(self._content_length)
headers.append(("content-length", content_length))
return _response.test_response(self.data, headers)
def test_errors(self):
h = HTTPErrorProcessor()
o = h.parent = MockOpener()
req = Request("http://example.com")
# all 2xx are passed through
r = mechanize._response.test_response()
newr = h.http_response(req, r)
self.assertTrue(r is newr)
self.assertTrue(not hasattr(o, "proto")) # o.error not called
r = mechanize._response.test_response(code=202, msg="Accepted")
newr = h.http_response(req, r)
self.assertTrue(r is newr)
self.assertTrue(not hasattr(o, "proto")) # o.error not called
r = mechanize._response.test_response(code=206, msg="Partial content")
newr = h.http_response(req, r)
self.assertTrue(r is newr)
self.assertTrue(not hasattr(o, "proto")) # o.error not called
# anything else calls o.error (and MockOpener returns None, here)
r = mechanize._response.test_response(code=502, msg="Bad gateway")
self.assertTrue(h.http_response(req, r) is None)
self.assertEqual(o.proto, "http") # o.error called
self.assertEqual(o.args[:4], (req, r, 502, "Bad gateway"))
def http_open(self, request):
r = _response.test_response(url=request.get_full_url())
# these tests aren't interested in auto-.reload() behaviour of
# .back(), so read the response to prevent that happening
r.get_data()
return r
def test_raise_http_errors(self):
# HTTPDefaultErrorHandler should raise HTTPError if no error handler
# handled the error response
from mechanize import _response
h = mechanize.HTTPDefaultErrorHandler()
url = "http://example.com"
code = 500
msg = "Error"
request = mechanize.Request(url)
response = _response.test_response(url=url, code=code, msg=msg)
# case 1. it's not an HTTPError
try:
h.http_error_default(request, response, code, msg, response.info())
except mechanize.HTTPError as exc:
self.assertTrue(exc is not response)
self.assertTrue(exc.fp is response)
else:
self.assertTrue(False)
# case 2. response object is already an HTTPError, so just re-raise it
error = mechanize.HTTPError(url, code, msg, "fake headers", response)
try:
h.http_error_default(request, error, code, msg, error.info())
except mechanize.HTTPError as exc:
self.assertTrue(exc is error)
def test_pickle_cookie(self):
from mechanize._clientcookie import cookies_equal
cookiejar = mechanize.CookieJar()
url = "http://example.com/"
request = mechanize.Request(url)
response = mechanize._response.test_response(
headers=[("Set-Cookie", "spam=eggs")], url=url)
[cookie] = cookiejar.make_cookies(response, request)
def check_equality(b):
self.assertTrue(cookies_equal(cookie, b))
test_pickling(cookie, check_equality)
def http_open(self, request):
return mechanize._response.test_response(
url=request.get_full_url())