Skip to content

Instantly share code, notes, and snippets.

@elena-roff
Created March 12, 2019 13:42
Show Gist options
  • Save elena-roff/972a0bf0c463e2e842760f9fa7f696c4 to your computer and use it in GitHub Desktop.
Save elena-roff/972a0bf0c463e2e842760f9fa7f696c4 to your computer and use it in GitHub Desktop.
Test example with client
headers = {
'Content-Type': 'application/json',
'Authorization': ('...'),
'accept': 'application/json'
}
def test_post(client):
response = client.post('/endpoint',
data=json.dumps(payload),
headers=headers)
assert response.status_code == 200
assert response.json == [{<data>}]
def test_post_400(client):
response = client.post('/endpoint',
data=json.dumps({'payload': 'invalid'}),
headers=headers)
assert response.status_code == 400
def test_post_auth_before_validation_401(client):
headers_ = deepcopy(headers)
headers_['Authorization'] = 'not'
response = client.post('/endpoint',
data=json.dumps({'payload': 'invalid'}),
headers=headers_)
assert response.status_code == 401
def test_post_500(client, mocker):
def fail(*args, **kwargs):
raise Exception('failed')
with mocker.patch('<service>.app.function', side_effect=fail):
response = client.post('/endpoint',
data=json.dumps(payload),
headers=headers)
assert response.status_code == 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment