def __init__(self, environment, refresh_token=None): HttpClient.__init__(self, environment) self._refresh_token = refresh_token self._access_token = None self.environment = environment self.add_injector(injector=self)
def test_HttpClient_onSuccess_returnsResponse_with_empty_body(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "GET" self.stub_request_with_response(request, status=204) response = client.execute(request) self.assertIsNone(response.result)
def test_HttpClient_execute_addsHeaders(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "GET" self.stub_request_with_empty_reponse(request) client.execute(request) call = responses.calls[0].request self.assertEqual(call.headers["User-Agent"], client.get_user_agent())
def testHttpClient_addInjector_throwsWhenArgumentNotFunctional(self): client = HttpClient(self.environment()) try: client.add_injector(1) self.fail( "client.add_injector did not throw for non-functional argument" ) except TypeError as e: self.assertEqual( str(e), "injector must be a function or implement the __call__ method")
def test_HttpClient_executeDoesNotModifyRequest(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "GET" self.stub_request_with_response(request, "{\"valid-key\": \"valid-data\"}", 201) client.execute(request) self.assertEqual(len(request.headers), 0)
def test_HttpClient_onSuccess_escapesDashesWhenUnmarshaling(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "GET" self.stub_request_with_response(request, "{\"valid-key\": \"valid-data\"}", 201) try: response = client.execute(request) self.assertEqual(response.result.valid_key, "valid-data") except BaseException as exception: self.fail(exception.message)
def test_HttpClient_onError_throwsHttpErrorForNon200StatusCode(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/error" request.verb = "POST" self.stub_request_with_response(request, status=400, response_body="An error occurred!") try: client.execute(request) except BaseException as e: self.assertEqual("HttpError", e.__class__.__name__) self.assertEqual("An error occurred!", str(e))
def test_HttpClient_execute_usesAllParamsInRequest(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "POST" request.headers = {"Test": "Header", "Content-Type": "text/plain"} request.body = "Some data" self.stub_request_with_empty_reponse(request) client.execute(request) self.assertEqual(len(responses.calls), 1) call = responses.calls[0].request self.assertEqual(call.method, "POST") self.assertEqual(call.url, "http://localhost/") self.assertEqual(call.headers["Test"], "Header") self.assertEqual(call.body, "Some data")
def test_HttpClient_execute_filesArePreservedInCopy(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "GET" request.headers["Content-Type"] = "multipart/related" license = File("LICENSE") request.body = {"license": license} self.stub_request_with_empty_reponse(request) client.execute(request) self.assertTrue(str(license.read()) in responses.calls[0].request.body) license.close()
def test_HttpClient_addInjector_usesInjectorLambda(self): client = HttpClient(self.environment()) client.add_injector(lambda req: req.headers.clear()) request = GenericRequest() request.path = "/" request.verb = "GET" request.headers = {"Foo": "Bar"} self.stub_request_with_empty_reponse(request) client.execute(request) call = responses.calls[0].request self.assertFalse("Foo" in call.headers)
def test_HttpClient_addInjector_usesInjectorFunction(self): client = HttpClient(self.environment()) def inj(request): request.headers["Foo"] = "Bar" client.add_injector(inj) request = GenericRequest() request.path = "/" request.verb = "GET" self.stub_request_with_empty_reponse(request) client.execute(request) call = responses.calls[0].request self.assertEqual(call.headers["Foo"], "Bar")