def test_collision_management(client: Fabricator, m: requests_mock.Mocker): # Register an endpoint that has the same name as one of the built ins client.get(name='register', path='/__health') client.start() # Mock the request m.get(HEALTH_TEST_URL, text='OK') # Call the endpoint resp = client.register() assert resp.status_code is 200 assert resp.text == 'OK'
def test_headers_sent_from_endpoint(client: Fabricator, m: requests_mock.Mocker): # Add an endpoint with a header client.get(name='health', path='/__health', headers={'X-CUSTOM': '1'}) client.start() # Mock it m.get(HEALTH_TEST_URL, text='OK') # Make the request resp = client.health() assert resp.status_code is 200 # Check that header was sent assert m.last_request.headers['X-CUSTOM'] == '1'
def test_direct_registration_with_multiple_methods(client: Fabricator, m: requests_mock.Mocker): # Add an endpoint using "register" client.register(name='update', path='/todos/:id', methods=['PUT', 'PATCH']) client.start() # Mock it m.request(requests_mock.ANY, '{}/todos/1'.format(BASE_URL), text='OK') def asrt(resp): assert resp.status_code is 200 assert m.last_request.json()['value'] == 'Thing to do' # Test it resp = client.update.put(id=1, value='Thing to do') asrt(resp) resp = client.update.patch(id=1, value='Thing to do') asrt(resp)
def test_standard_api(client: Fabricator, m: requests_mock.Mocker): client.standard(with_param='id') client.start() # Mock request m.get(BASE_URL + '/', text='OK') m.post(BASE_URL + '/', status_code=201) m.get(BASE_URL + '/1', text='OK') m.put(BASE_URL + '/1', status_code=202) m.patch(BASE_URL + '/1', status_code=202) m.delete(BASE_URL + '/1', status_code=204) # Call the endpoints assert client.all().status_code == 200 get = client.get(id=1) assert get.status_code == 200 and get.text == 'OK' assert client.create(name='user').status_code == 201 assert client.overwrite(id=1, name='user').status_code == 202 assert client.update(id=1, name='user').status_code == 202 assert client.delete(id=1).status_code == 204
def test_response_handler(client: Fabricator, m: requests_mock.Mocker): # Create and set a custom handler on the client def custom_handler(resp): return resp.text, resp.status_code client.set_handler(handler=custom_handler) # Register an endpoint client.get(name='health', path='/__health') client.start() # Mock the request m.get(HEALTH_TEST_URL, text='OK') # Call the endpoint text, code = client.health() assert text == 'OK' assert code is 200
def test_headers_shared_from_endpoint_parent(client: Fabricator, m: requests_mock.Mocker): # Add a header to the top level client client.add_header(name='X-CUSTOM', value='1') # Now add an endpoint client.get(name='health', path='/__health') client.start() # Mock the request m.get(HEALTH_TEST_URL, text='OK') # Make the request resp = client.health() assert resp.status_code is 200 # Now check what was sent to the endpoint in the request assert m.last_request.headers['X-CUSTOM'] == '1'
def TodoClient(): # Instantiate the Client client = Fabricator(base_url='https://todos.com') # Health endpoint client.get('health', path='/__health') # Let's make a group to work with Todos todos = client.group(name='todos', prefix='/todos') # Now let's use the group todos.get('all', path='/') todos.get('one', path='/:id') todos.post('create', path='/') todos.put('update', path='/:id') todos.delete('remove', path='/:id') # Start the client client.start() return client
def client() -> Fabricator: client = Fabricator(base_url=BASE_URL) return client