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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
import pytest
import falcon
from falcon import testing
def test_custom_router_add_route_should_be_used(asgi, util):
check = []
class CustomRouter:
def add_route(self, uri_template, *args, **kwargs):
check.append(uri_template)
def find(self, uri):
pass
app = util.create_app(asgi=asgi, router=CustomRouter())
app.add_route('/test', 'resource')
assert len(check) == 1
assert '/test' in check
def test_custom_router_find_should_be_used(asgi, util):
if asgi:
async def resource(req, resp, **kwargs):
resp.text = '{{"uri_template": "{0}"}}'.format(req.uri_template)
else:
def resource(req, resp, **kwargs):
resp.text = '{{"uri_template": "{0}"}}'.format(req.uri_template)
class CustomRouter:
def __init__(self):
self.reached_backwards_compat = False
def find(self, uri, req=None):
if uri == '/test/42':
return resource, {'GET': resource}, {}, '/test/{id}'
if uri == '/test/42/no-uri-template':
return resource, {'GET': resource}, {}, None
if uri == '/test/42/uri-template/backwards-compat':
return resource, {'GET': resource}, {}
if uri == '/404/backwards-compat':
self.reached_backwards_compat = True
return (None, None, None)
return None
router = CustomRouter()
app = util.create_app(asgi=asgi, router=router)
client = testing.TestClient(app)
response = client.simulate_request(path='/test/42')
assert response.content == b'{"uri_template": "/test/{id}"}'
response = client.simulate_request(path='/test/42/no-uri-template')
assert response.content == b'{"uri_template": "None"}'
response = client.simulate_request(path='/test/42/uri-template/backwards-compat')
assert response.content == b'{"uri_template": "None"}'
for uri in ('/404', '/404/backwards-compat'):
response = client.simulate_request(path=uri)
assert response.content == falcon.HTTPNotFound().to_json()
assert response.status == falcon.HTTP_404
assert router.reached_backwards_compat
def test_can_pass_additional_params_to_add_route(asgi, util):
check = []
class CustomRouter:
def add_route(self, uri_template, resource, **kwargs):
name = kwargs['name']
self._index = {name: uri_template}
check.append(name)
def find(self, uri):
pass
app = util.create_app(asgi=asgi, router=CustomRouter())
app.add_route('/test', 'resource', name='my-url-name')
assert len(check) == 1
assert 'my-url-name' in check
# NOTE(kgriffs): Extra values must be passed as kwargs, since that makes
# it a lot easier for overridden methods to simply ignore options they
# don't care about.
with pytest.raises(TypeError):
app.add_route('/test', 'resource', 'xarg1', 'xarg2')
def test_custom_router_takes_req_positional_argument(asgi, util):
if asgi:
async def responder(req, resp):
resp.text = 'OK'
else:
def responder(req, resp):
resp.text = 'OK'
class CustomRouter:
def find(self, uri, req):
if uri == '/test' and isinstance(req, falcon.Request):
return responder, {'GET': responder}, {}, None
router = CustomRouter()
app = util.create_app(asgi=asgi, router=router)
client = testing.TestClient(app)
response = client.simulate_request(path='/test')
assert response.content == b'OK'
def test_custom_router_takes_req_keyword_argument(asgi, util):
if asgi:
async def responder(req, resp):
resp.text = 'OK'
else:
def responder(req, resp):
resp.text = 'OK'
class CustomRouter:
def find(self, uri, req=None):
if uri == '/test' and isinstance(req, falcon.Request):
return responder, {'GET': responder}, {}, None
router = CustomRouter()
app = util.create_app(asgi=asgi, router=router)
client = testing.TestClient(app)
response = client.simulate_request(path='/test')
assert response.content == b'OK'
|