How to use the falcon.testing.TestClient function in falcon

To help you get started, we’ve selected a few falcon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github falconry / falcon / tests / test_hello.py View on Github external
def client():
    return testing.TestClient(falcon.App())
github falconry / falcon / tests / test_utils.py View on Github external
def test_default_headers_with_override(self):
        app = falcon.App()
        resource = testing.SimpleTestResource()
        app.add_route('/', resource)

        override_before = 'something-something'
        override_after = 'something-something'[::-1]

        headers = {
            'Authorization': 'Bearer XYZ',
            'Accept': 'application/vnd.siren+json',
            'X-Override-Me': override_before,
        }

        client = testing.TestClient(app, headers=headers)
        client.simulate_get(headers={'X-Override-Me': override_after})

        assert resource.captured_req.auth == headers['Authorization']
        assert resource.captured_req.accept == headers['Accept']
        assert resource.captured_req.get_header('X-Override-Me') == override_after
github falconry / falcon / tests / test_headers.py View on Github external
def cors_client():
    app = falcon.App(cors_enable=True)
    return testing.TestClient(app)
github falconry / falcon / tests / test_wsgi_errors.py View on Github external
def client():
    app = falcon.App()

    tehlogger = LoggerResource()
    app.add_route('/logger', tehlogger)
    return testing.TestClient(app)
github falconry / falcon / tests / test_middleware.py View on Github external
def test_middleware_execution_order(self):
        global context
        app = falcon.App(independent_middleware=False,
                         middleware=[ExecutedFirstMiddleware(),
                                     ExecutedLastMiddleware()])

        app.add_route(TEST_ROUTE, MiddlewareClassResource())
        client = testing.TestClient(app)

        response = client.simulate_request(path=TEST_ROUTE)
        assert _EXPECTED_BODY == response.json
        assert response.status == falcon.HTTP_200
        # as the method registration is in a list, the order also is
        # tested
        expectedExecutedMethods = [
            'ExecutedFirstMiddleware.process_request',
            'ExecutedLastMiddleware.process_request',
            'ExecutedFirstMiddleware.process_resource',
            'ExecutedLastMiddleware.process_resource',
            'ExecutedLastMiddleware.process_response',
            'ExecutedFirstMiddleware.process_response'
        ]
        assert expectedExecutedMethods == context['executed_methods']
github falconry / falcon / tests / test_static.py View on Github external
def client():
    app = falcon.App()
    return testing.TestClient(app)
github falconry / falcon / tests / test_utils.py View on Github external
def test_cached_text_in_result(self):
        app = falcon.App()
        app.add_route('/', testing.SimpleTestResource(body='test'))
        client = testing.TestClient(app)

        result = client.simulate_get()
        assert result.text == result.text
github falconry / falcon / tests / test_custom_router.py View on Github external
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 = falcon.App(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 not response.content
        assert response.status == falcon.HTTP_404

    assert router.reached_backwards_compat
github SAP / cf-python-logging-support / tests / test_falcon_logging.py View on Github external
def _user_logging(headers, extra, expected):
    app = falcon.API(middleware=[
        falcon_logging.LoggingMiddleware()
    ])
    app.add_route('/test/user/logging', UserResourceRoute(extra, expected))
    _set_up_falcon_logging(app)
    client = testing.TestClient(app)
    _check_expected_response(client.simulate_get('/test/user/logging',
                                                 headers=headers))