def test_get(requests_mock, session, patch_auth): requests_mock.get( MOCK_ENDPOINT_URL, request_headers=AUTHORIZATION_HEADER, json={"foo": "bar"}, ) client = BaseClient(MOCK_SERVICE_URL, session) assert client._get(MOCK_ENDPOINT, DummySchema()) == DummyObject(foo="bar")
def test_stream_server_sent_events(mocker): response_content = mocker.MagicMock() response_content.iter_lines.return_value = STREAM_RESPONSE mocker.patch.object( BaseClient, "_get_raw", return_value=response_content, ) client = BaseClient(mocker.Mock(), mocker.Mock()) messages = list(client._stream_server_sent_events("endpoint")) assert messages == SSE_MOCK_MESSAGES
def test_put(requests_mock, session, patch_auth): mock = requests_mock.put( MOCK_ENDPOINT_URL, request_headers=AUTHORIZATION_HEADER, json={"foo": "bar"}, ) client = BaseClient(MOCK_SERVICE_URL, session) response = client._put(MOCK_ENDPOINT, DummySchema(), json={"test": "payload"}) assert response == DummyObject(foo="bar") assert mock.last_request.json() == {"test": "payload"}
def test_bad_responses( requests_mock, session, patch_auth, status_code, exception, http_method, check_status, ): mock_method = getattr(requests_mock, http_method.lower()) mock_method( MOCK_ENDPOINT_URL, request_headers=AUTHORIZATION_HEADER, status_code=status_code, json={"foo": "bar"}, ) client = BaseClient(MOCK_SERVICE_URL, session) method = getattr(client, "_{}".format(http_method.lower())) if check_status: with pytest.raises(exception): method(MOCK_ENDPOINT, DummySchema(), check_status=check_status) else: method(MOCK_ENDPOINT, DummySchema(), check_status=check_status)
def test_malformatted_json(requests_mock, session, patch_auth, http_method): mock_method = getattr(requests_mock, http_method.lower()) mock_method( MOCK_ENDPOINT_URL, request_headers=AUTHORIZATION_HEADER, json={"bad": "json"}, ) client = BaseClient(MOCK_SERVICE_URL, session) method = getattr(client, "_{}".format(http_method.lower())) with pytest.raises(ValidationError): method(MOCK_ENDPOINT, DummySchema())