Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def client():
app = falcon.App()
resource = RedirectingResource()
app.add_route('/', resource)
return testing.TestClient(app)
def create_client(handlers=None):
res = testing.SimpleTestResource()
app = falcon.App()
app.add_route('/', res)
if handlers:
app.req_options.media_handlers.update(handlers)
client = testing.TestClient(app)
client.resource = res
return client
def test_raise_status_survives_after_hooks(self):
""" Make sure after hook doesn't overwrite our status """
app = falcon.App()
app.add_route('/status', TestStatusResource())
client = testing.TestClient(app)
response = client.simulate_request(path='/status', method='DELETE')
assert response.status == falcon.HTTP_200
assert response.headers['x-failed'] == 'False'
assert response.text == 'Pass'
def test_resources_can_read_request_stream_during_tests(self):
"""Make sure we can perform a simple request during testing.
Originally, testing would fail after performing a request because no
size was specified when calling `wsgiref.validate.InputWrapper.read()`
via `req.stream.read()`"""
app = falcon.App()
type_route = '/type'
app.add_route(type_route, TypeResource())
client = testing.TestClient(app)
result = client.simulate_post(path=type_route, body='hello')
assert result.status == falcon.HTTP_200
assert result.json == {'data': 'hello'}
def test_simulate_hostname(self):
app = falcon.App()
resource = testing.SimpleTestResource()
app.add_route('/', resource)
client = testing.TestClient(app)
client.simulate_get('/', protocol='https',
host='falcon.readthedocs.io')
assert resource.captured_req.uri == 'https://falcon.readthedocs.io/'
def client():
app = falcon.App()
tehlogger = LoggerResource()
app.add_route('/logger', tehlogger)
return testing.TestClient(app)
def test_path_must_start_with_slash(self):
app = falcon.App()
app.add_route('/', testing.SimpleTestResource())
client = testing.TestClient(app)
with pytest.raises(ValueError):
client.simulate_get('foo')
def create_client(handlers=None):
res = testing.SimpleTestResource()
app = falcon.App()
app.add_route('/', res)
if handlers:
app.resp_options.media_handlers.update(handlers)
client = testing.TestClient(app)
client.resource = res
return client
def client():
app = falcon.App()
return testing.TestClient(app)
def on_post(self, req, resp, user_id):
try:
doc = req.context.doc
except AttributeError:
raise falcon.HTTPBadRequest(
'Missing thing',
'A thing must be submitted in the request body.')
proper_thing = self.db.add_thing(doc)
resp.status = falcon.HTTP_201
resp.location = '/%s/things/%s' % (user_id, proper_thing['id'])
# Configure your WSGI server to load "things.app" (app is a WSGI callable)
app = falcon.App(middleware=[
AuthMiddleware(),
RequireJSON(),
JSONTranslator(),
])
db = StorageEngine()
things = ThingsResource(db)
app.add_route('/{user_id}/things', things)
# If a responder ever raised an instance of StorageError, pass control to
# the given handler.
app.add_error_handler(StorageError, StorageError.handle)
# Proxy some things to another service; this example shows how you might
# send parts of an API off to a legacy system that hasn't been upgraded
# yet, or perhaps is a single cluster that all data centers have to share.