def test_request_should_not_include_token(self): """ Test that the access token is not included in the call to requests if it isn't actually set. """ client = Client() with patch("requests.request") as request: request.return_value.json.return_value = {} client.request("GET", "http://www.google.com/") request.assert_called_once_with( "GET", "http://www.google.com/", headers=None, json=None, params={} )
def test_request_preserves_accept_header(self): """ Test that the accept header is not overwritten and that text is returned """ fake_client = Client() with patch("requests.request") as request: request.return_value.text = "response text" resp_text = fake_client.request( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, ) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, json=None, params=expected_params, ) assert resp_text == "response text"
def test_request_should_return_json_when_json_accept_header_provided(self): """ Test that the requests response.json() decoding is returned when no accept header is provided """ fake_client = Client() with patch("requests.request") as request: request.return_value.json.return_value = {"key": "value"} resp = fake_client.request("GET", "http://www.google.com/", headers={"Accept": "application/json"}) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/json"}, json=None, params=expected_params, stream=False, ) assert resp == {"key": "value"}
def test_request_should_return_byte_stream_when_requested(self): """ Test that the requests response.iter_content(None, False) is returned when stream is requested """ fake_client = Client() with patch("requests.request") as request: request.return_value.iter_content.return_value = [ b"response", b" ", b"text", ] resp = fake_client.request( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, as_stream=True, ) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, json=None, params=expected_params, stream=True, ) request.return_value.iter_content.assert_called_once_with( chunk_size=None, decode_unicode=False) assert resp == [b"response", b" ", b"text"]
def test_request_should_return_bytes_when_non_json_accept_header_provided( self): """ Test that the accept header is preserved and that bytes are returned """ fake_client = Client() with patch("requests.request") as request: request.return_value.content = b"response text" resp = fake_client.request( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, ) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, json=None, params=expected_params, stream=False, ) assert resp == b"response text"
def test_request_should_include_token_when_set(self): """ Test that the access token is not included in the call to requests if it isn't actually set. """ client = Client() client.set_client_access_token("ABCDEF1234") with patch("requests.request") as request: request.return_value.json.return_value = {} client.request("GET", "http://www.google.com/") expected_params = {"access_token": "ABCDEF1234"} request.assert_called_once_with( "GET", "http://www.google.com/", headers=None, json=None, params=expected_params, )
def test_request_should_include_token_when_set(self): """ Test that the access token is not included in the call to requests if it isn't actually set. """ fake_client = Client() fake_client.set_client_access_token("ABCDEF1234") with patch("requests.request") as request: request.return_value.json.return_value = {"key": "value"} ret = fake_client.request("GET", "http://www.google.com/") expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Authorization": "Bearer ABCDEF1234"}, json=None, params=expected_params, )