Skip to content

Commit

Permalink
Merge branch 'master' into feature_add_param_as_datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
kgriffs committed May 26, 2017
2 parents dd4e778 + 7e31c65 commit 1943044
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 472 deletions.
168 changes: 85 additions & 83 deletions tests/test_custom_router.py
Expand Up @@ -2,119 +2,121 @@
from falcon import testing


class TestCustomRouter(object):
def test_custom_router_add_route_should_be_used():
check = []

def test_custom_router_add_route_should_be_used(self):
check = []
class CustomRouter(object):
def add_route(self, uri_template, *args, **kwargs):
check.append(uri_template)

class CustomRouter(object):
def add_route(self, uri_template, *args, **kwargs):
check.append(uri_template)
def find(self, uri):
pass

def find(self, uri):
pass
app = falcon.API(router=CustomRouter())
app.add_route('/test', 'resource')

app = falcon.API(router=CustomRouter())
app.add_route('/test', 'resource')
assert len(check) == 1
assert '/test' in check

assert len(check) == 1
assert '/test' in check

def test_custom_router_find_should_be_used(self):
def test_custom_router_find_should_be_used():

def resource(req, resp, **kwargs):
resp.body = '{{"uri_template": "{0}"}}'.format(req.uri_template)
def resource(req, resp, **kwargs):
resp.body = '{{"uri_template": "{0}"}}'.format(req.uri_template)

class CustomRouter(object):
def __init__(self):
self.reached_backwards_compat = False
class CustomRouter(object):
def __init__(self):
self.reached_backwards_compat = False

def find(self, uri):
if uri == '/test/42':
return resource, {'GET': resource}, {}, '/test/{id}'
def find(self, uri):
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/no-uri-template':
return resource, {'GET': resource}, {}, None

if uri == '/test/42/uri-template/backwards-compat':
return resource, {'GET': resource}, {}
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)
if uri == '/404/backwards-compat':
self.reached_backwards_compat = True
return (None, None, None)

return None
return None

router = CustomRouter()
app = falcon.API(router=router)
client = testing.TestClient(app)
router = CustomRouter()
app = falcon.API(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')
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/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"}'
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 not response.content
assert response.status == falcon.HTTP_404
for uri in ('/404', '/404/backwards-compat'):
response = client.simulate_request(path=uri)
assert not response.content
assert response.status == falcon.HTTP_404

assert router.reached_backwards_compat
assert router.reached_backwards_compat

def test_can_pass_additional_params_to_add_route(self):

check = []
def test_can_pass_additional_params_to_add_route():

class CustomRouter(object):
def add_route(self, uri_template, method_map, resource, name):
self._index = {name: uri_template}
check.append(name)
check = []

def find(self, uri):
pass
class CustomRouter(object):
def add_route(self, uri_template, method_map, resource, name):
self._index = {name: uri_template}
check.append(name)

app = falcon.API(router=CustomRouter())
app.add_route('/test', 'resource', name='my-url-name')
def find(self, uri):
pass

assert len(check) == 1
assert 'my-url-name' in check
app = falcon.API(router=CustomRouter())
app.add_route('/test', 'resource', name='my-url-name')

# Also as arg.
app.add_route('/test', 'resource', 'my-url-name-arg')
assert len(check) == 1
assert 'my-url-name' in check

assert len(check) == 2
assert 'my-url-name-arg' in check
# Also as arg.
app.add_route('/test', 'resource', 'my-url-name-arg')

def test_custom_router_takes_req_positional_argument(self):
def responder(req, resp):
resp.body = 'OK'
assert len(check) == 2
assert 'my-url-name-arg' in check

class CustomRouter(object):
def find(self, uri, req):
if uri == '/test' and isinstance(req, falcon.Request):
return responder, {'GET': responder}, {}, None

router = CustomRouter()
app = falcon.API(router=router)
client = testing.TestClient(app)
response = client.simulate_request(path='/test')
assert response.content == b'OK'
def test_custom_router_takes_req_positional_argument():
def responder(req, resp):
resp.body = 'OK'

def test_custom_router_takes_req_keyword_argument(self):
def responder(req, resp):
resp.body = 'OK'
class CustomRouter(object):
def find(self, uri, req):
if uri == '/test' and isinstance(req, falcon.Request):
return responder, {'GET': responder}, {}, None

class CustomRouter(object):
def find(self, uri, req=None):
if uri == '/test' and isinstance(req, falcon.Request):
return responder, {'GET': responder}, {}, None
router = CustomRouter()
app = falcon.API(router=router)
client = testing.TestClient(app)
response = client.simulate_request(path='/test')
assert response.content == b'OK'

router = CustomRouter()
app = falcon.API(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():
def responder(req, resp):
resp.body = 'OK'

class CustomRouter(object):
def find(self, uri, req=None):
if uri == '/test' and isinstance(req, falcon.Request):
return responder, {'GET': responder}, {}, None

router = CustomRouter()
app = falcon.API(router=router)
client = testing.TestClient(app)
response = client.simulate_request(path='/test')
assert response.content == b'OK'
10 changes: 8 additions & 2 deletions tests/test_default_router.py
Expand Up @@ -132,9 +132,15 @@ def test_user_regression_versioned_url():
def test_user_regression_recipes():
router = DefaultRouter()
router.add_route(
'/recipes/{activity}/{type_id}', {}, ResourceWithId(1))
'/recipes/{activity}/{type_id}',
{},
ResourceWithId(1)
)
router.add_route(
'/recipes/baking', {}, ResourceWithId(2))
'/recipes/baking',
{},
ResourceWithId(2)
)

resource, __, __, __ = router.find('/recipes/baking/4242')
assert resource.resource_id == 1
Expand Down
24 changes: 12 additions & 12 deletions tests/test_deps.py
@@ -1,17 +1,17 @@
import mimeparse


class TestDeps(object):
def test_deps_mimeparse_correct_package():
"""Ensure we are dealing with python-mimeparse, not mimeparse."""

def test_mimeparse_correct_package(self):
"""Ensure we are dealing with python-mimeparse, not mimeparse."""
tokens = mimeparse.__version__.split('.')
msg = (
'Incorrect dependency detected. Please install the '
'"python-mimeparse" package instead of the "mimeparse" '
'package.'
)

tokens = mimeparse.__version__.split('.')
msg = ('Incorrect dependency detected. Please install the '
'"python-mimeparse" package instead of the "mimeparse" '
'package.')

# NOTE(kgriffs): python-mimeparse starts at version 1.5.2,
# whereas the mimeparse package is at version 0.1.4 at the time
# of this writing.
assert int(tokens[0]) > 0, msg
# NOTE(kgriffs): python-mimeparse starts at version 1.5.2,
# whereas the mimeparse package is at version 0.1.4 at the time
# of this writing.
assert int(tokens[0]) > 0, msg

0 comments on commit 1943044

Please sign in to comment.