async def test_error_responses(self, status, exception, aresponses): aresponses.add( 'some.url', '/resources', 'DELETE', aresponses.Response(status=status, text=json.dumps({'id': '1234'}), content_type='application/json')) handler = HTTPHandler(IdentityMock()) with pytest.raises(exception): _ = await handler.delete('https://some.url/resources')
async def test_delete(self, aresponses): aresponses.add( 'some.url', '/resources', 'DELETE', aresponses.Response(status=200, text=json.dumps({'id': '1234'}), content_type='application/json')) handler = HTTPHandler(IdentityMock()) result = await handler.delete('https://some.url/resources') assert result == {'id': '1234'}
async def test_custom_handler(self, aresponses): aresponses.add( 'some.url', '/resources', 'POST', aresponses.Response(status=402, text=json.dumps({'id': '1234'}), content_type='application/json')) handler = HTTPHandler(IdentityMock()) with pytest.raises(ValueError): _ = await handler.post( 'https://some.url/resources', {'name': 'something'}, error_handlers={402: Mock(side_effect=ValueError())})
async def test_post(self, aresponses): aresponses.add( 'some.url', '/resources', 'POST', aresponses.Response(status=201, text=json.dumps({'id': '1234'}), content_type='application/json')) handler = HTTPHandler(IdentityMock()) result = await handler.post('https://some.url/resources', {'name': 'something'}) assert result == {'id': '1234'}
def test_with_custom_headers(self): handler = HTTPHandler(identity=IdentityMock(), headers={'my-custom-header': '1234'}) assert 'my-custom-header' in handler.headers assert handler.headers['my-custom-header'] == '1234'
def test_with_identity(self): iden = IdentityMock() handler = HTTPHandler(identity=iden) assert 'Authorization' in handler.headers assert handler.headers['Authorization'] == f'Bearer {iden.token()}'