def test_api_request_returns_one_item(client): logstar_on() http_request() response = client.get(url_for('api_requests')) assert len(response.json) == 1
def test_api_no_items_higher_than_id(client): logstar_on() above_id = http_request().id response = client.get(url_for('api_requests', above_id=above_id)) assert response.json == []
def test_api_request_returns_request_data(client): logstar_on() request = http_request() response = client.get(url_for('api_requests')) assert response.json[0]['url'] == request.url
def test_api_call_delete_logs_request(): logstar_on() requests.delete('http://127.0.0.1:8000/user-agent?name=pete') request_items = get_all_requests() assert len(request_items) == 1 assert request_items[0].method == 'DELETE'
def test_api_call_patch_logs_request(): logstar_on() requests.patch('http://127.0.0.1:8000/user-agent?name=pete') request_items = get_all_requests() assert len(request_items) == 1 assert request_items[0].method == 'PATCH'
def test_api_gets_requets_older_than_id(client): logstar_on() http_request() highest_id = http_request().id response = client.get(url_for('api_requests', below_id=highest_id)) assert response.json[0]['id'] < highest_id
def test_api_request_ordered_newest_first(client): logstar_on() http_request() http_request() response = client.get(url_for('api_requests')) assert response.json[0]['id'] > response.json[1]['id']
def test_api_get_requests_newer_than_id(client): logstar_on() above_id = http_request().id http_request() response = client.get(url_for('api_requests', above_id=above_id)) assert len(response.json) == 1 assert response.json[0]['id'] == above_id + 1
def test_call_api_get_with_headers(): logstar_on() requests.get( 'http://127.0.0.1:8000/user-agent', headers={'user-agent': 'my-app/0.0.1'} ) request_items = get_all_requests() assert len(request_items) == 1 assert request_items[0].headers == "{'user-agent': 'my-app/0.0.1'}"
def test_api_pagination_limit(client): logstar_on() http_request() http_request() latest_request_id = http_request().id response = client.get(url_for('api_requests')) assert len(response.json) == 2 assert response.json[0]['id'] == latest_request_id
def test_call_api_post_with_payload(): """ Simple post call with requests to a URL with headers """ logstar_on() requests.post('http://127.0.0.1:8000/post', data={'key': 'value'}) request_items = get_all_requests() assert len(request_items) == 1 assert request_items[0].payload == "{'key': 'value'}"
def test_api_call_post_logs_request(): logstar_on() requests.post('http://127.0.0.1:8000/user-agent?name=pete') request_items = get_all_requests() assert len(request_items) == 1 assert request_items[0].url == 'http://127.0.0.1:8000/user-agent?name=pete' assert request_items[0].method == 'POST' assert '<h1>Method Not Allowed</h1>' in request_items[0].response_content assert request_items[0].response_status_code == 405 assert type(request_items[0].created_at) == datetime.datetime
def test_api_call_external_library_get_logs_requests(): logstar_on() weather = Weather() lookup = weather.lookup(560743) lookup.condition() request_items = get_all_requests() assert len(request_items) == 1 assert 'http://query.yahooapis.com/v1/public/yql?q=select' in request_items[0].url assert request_items[0].method == 'GET' assert 'Full Forecast at Yahoo! Weather' in request_items[0].response_content assert request_items[0].response_status_code == 200 assert type(request_items[0].created_at) == datetime.datetime
def test_api_call_get_logs_request(): logstar_on() requests.get('http://127.0.0.1:8000/user-agent?name=pete') request_items = get_all_requests() assert len(request_items) == 1 assert request_items[0].url == 'http://127.0.0.1:8000/user-agent?name=pete' assert request_items[0].method == 'GET' assert 'python-requests/2.18.4' in request_items[0].response_content assert request_items[0].response_status_code == 200 assert type(request_items[0].created_at) == datetime.datetime assert request_items[0].headers is None assert request_items[0].payload is None assert "'Server': 'gunicorn/19.7.1'" in request_items[0].response_headers assert type(request_items[0].time) == Decimal
""" This simple script switches Logstar on. A request is then made with requests. Request and response data for both API requests are logged by Logstar """ import requests from random import randint from logstar import logstar_on logstar_on() url = 'http://127.0.0.1:8000/user-agent?name={}'.format(randint(0, 1000)) requests.get(url, data={'key': 'value'})