class RequestTest(unittest.TestCase): def setUp(self): self._request = Request() def test_copy_raises_on_invalid_object(self): self.assertRaises(ValueError, Request().copy, object()) def test_copy_set_attributes(self): request = self._request.update_verb("POST").update_url("/call_me")\ .update_params({"name": "popo"})\ .update_data({"text": "cool beans"}) dest = request.copy(Request()) self.assertEqual(request.url, dest.url) self.assertEqual(request.verb, dest.verb) self.assertEqual(request.params, dest.params) self.assertEqual(request.data, dest.data) def test_set_verb(self): def assign_verb(new_verb): """To make it possible to use assertRaises.""" self._request.verb = new_verb self.assertRaises(ValueError, assign_verb, "Nop!") self.assertRaises(ValueError, self._request.update_verb, "Nop!") def test_call_chaining(self): post_request = self._request.update_verb("POST").update_url("/call_me")\ .update_params({"name": "popo"}).update_data({"text": "cool beans"}) self.assertEqual(post_request, self._request) self.assertEqual(post_request.verb, "POST") self.assertEqual(post_request.url, "/call_me") self.assertDictEqual(post_request.params, {"name": "popo"}) self.assertDictEqual(post_request.data, {"text": "cool beans"})
def test_request_raises(self): request = Request().update_url("fake_url").update_verb(VERBS.GET) responses.add(responses.GET, "https://agilezen.com/api/v1/fake_url", status=404, content_type='application/json') self.assertRaises(requests.HTTPError, self._client.send_request, request)
def test_copy_set_attributes(self): request = self._request.update_verb("POST").update_url("/call_me")\ .update_params({"name": "popo"})\ .update_data({"text": "cool beans"}) dest = request.copy(Request()) self.assertEqual(request.url, dest.url) self.assertEqual(request.verb, dest.verb) self.assertEqual(request.params, dest.params) self.assertEqual(request.data, dest.data)
def test_issue_request(self): items = {"items": [1, 2]} request = Request().update_url("fake_url").update_params({"k": "v"})\ .update_verb(VERBS.GET) responses.add(responses.GET, "https://agilezen.com/api/v1/fake_url?k=v", match_querystring=True, body=json.dumps(items), status=200, content_type="application/json") self.assertEqual(self._client.send_request(request), items)
def test_copy_raises_on_invalid_object(self): self.assertRaises(ValueError, Request().copy, object())
def setUp(self): self._request = Request()
def __init__(self, api_key): Request.__init__(self) self._api_key = api_key self._client = ApiClient(api_key)