def test_prepare_curl(self): """prepare_curl sets URL and auth header.""" curl = FakeCurl() getter = PycURLGetter(curl) request = self.make_request() getter.prepare_curl(request) self.assertEqual(request.get_full_url(), curl.options[pycurl.URL]) self.assertEqual(['Authorization: Basic cGV0ZTpwYXNz\n'], curl.options[pycurl.HTTPHEADER])
def test_handle_reponse(self): """On success, handle_response returns response body.""" curl = FakeCurl(body='My body!', header='My header!', effective_url='http://example.com/') getter = PycURLGetter(curl) output = getter.handle_response() self.assertEqual('My body!', output) self.assertEqual('My header!', getter.response_header.getvalue().decode('UTF-8')) self.assertEqual('http://example.com/', getter.curl.getinfo(pycurl.EFFECTIVE_URL))
def test_get_response_body(self): """On success, get_response_body returns response body.""" curl = FakeCurl(body='My body!', header='My header!', effective_url='http://example.com/') request = GetMazaDataAPI3('pete', 'pass').make_request() output = PycURLGetter.get_response_body(request, _curl=curl) self.assertEqual('My body!', output)
def test_handle_response_http_error(self): """On http error, handle_response raises urllib.error.HTTPError.""" curl = FakeCurl(500, 'My body!', '\nContent-type: fake\n\n') getter = PycURLGetter(curl) try: getter.handle_response() except urllib.error.HTTPError as e: httpe = e else: self.fail('No error was raised.') try: httpe.geturl() except AttributeError as attre: self.assertEqual("'HTTPError' object has no attribute 'url'", str(attre)) self.assertEqual(500, httpe.getcode()) self.assertEqual('My body!', httpe.msg.decode('UTF-8')) self.assertEqual([('Content-type', 'fake')], httpe.hdrs.items())
def test_prepare_curl_ssl_verify(self): """SSL cert verification can be disabled for testing purposes.""" curl = FakeCurl() getter = PycURLGetter(curl) getter.prepare_curl(self.make_request()) self.assertTrue(curl.options[pycurl.SSL_VERIFYPEER]) getter.verify_ssl = False getter.prepare_curl(self.make_request()) self.assertFalse(curl.options[pycurl.SSL_VERIFYPEER])
def test_handle_response_pycurl_error(self): """PycURLGetter allows other errors to propagate.""" error = pycurl.error(pycurl.E_MULTI_OUT_OF_MEMORY) getter = PycURLGetter(FakeCurl(perform_error=error)) with self.assertRaises(pycurl.error): getter.handle_response()
def test_cert_verification_failed(self): """Cert verification error raises CertificateVerificationFailed.""" error = pycurl.error(pycurl.E_SSL_CACERT) getter = PycURLGetter(FakeCurl(perform_error=error)) with self.assertRaises(CertificateVerificationFailed): getter.handle_response()
def test_handle_response_connection_error(self): """On connection error, handle_response raises CouldNotConnect.""" error = pycurl.error(pycurl.E_COULDNT_CONNECT) getter = PycURLGetter(FakeCurl(perform_error=error)) with self.assertRaises(CouldNotConnect): getter.handle_response()
def test_init(self): """Init should set the WRITEFUNCTION and HEADERFUNCTION.""" getter = PycURLGetter(FakeCurl()) options = getter.curl.options self.assertIsNot(None, options[pycurl.WRITEFUNCTION]) self.assertIsNot(None, options[pycurl.HEADERFUNCTION])