1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
import pytest
try:
import httpx
except ImportError:
httpx = None # type: ignore
try:
import requests
except ImportError:
requests = None # type: ignore
import falcon.testing as testing
def test_quote(util):
quote = util.load_module('examples/quote.py')
resp = testing.simulate_get(quote.app, '/quote')
assert resp.status_code == 200
assert resp.json == {
'author': 'Grace Hopper',
'quote': "I've always been more interested in the future than in the past.",
}
def test_things(asgi, util):
suffix = '_asgi' if asgi else ''
things = util.load_module(f'examples/things{suffix}.py')
resp = testing.simulate_get(things.app, '/things')
assert resp.status_code == 200
assert resp.text == (
'\nTwo things awe me most, the starry sky above me and the moral law within me.'
'\n\n ~ Immanuel Kant\n\n'
)
@pytest.mark.skipif(
httpx is None, reason='things_advanced_asgi.py requires httpx [not found]'
)
@pytest.mark.skipif(
requests is None, reason='things_advanced.py requires requests [not found]'
)
def test_things_advanced(asgi, util):
suffix = '_asgi' if asgi else ''
advanced = util.load_module(f'examples/things_advanced{suffix}.py')
# NOTE(vytas): The ASGI example explicitly requires Content-Length
# (its middleware errors out otherwise with 400).
# Should we change this?
resp1 = testing.simulate_get(
advanced.app, '/1337/things', headers={'Content-Length': '0'}
)
assert resp1.status_code == 401
resp2 = testing.simulate_get(
advanced.app,
'/1337/things',
headers={'Authorization': 'custom-token', 'Content-Length': '0'},
)
assert resp2.status_code == 200
assert len(resp2.json) == 1
assert resp2.json[0]['color'] == 'green'
|