def test_cookies_jar(): class Foo: def on_get(self, req, resp): # NOTE(myuz): In the future we shouldn't change the cookie # a test depends on the input. # NOTE(kgriffs): This is the only test that uses a single # cookie (vs. multiple) as input; if this input ever changes, # a separate test will need to be added to explicitly verify # this use case. resp.set_cookie('has_permission', 'true') def on_post(self, req, resp): if req.cookies['has_permission'] == 'true': resp.status = falcon.HTTP_200 else: resp.status = falcon.HTTP_403 app = App() app.add_route('/jars', Foo()) client = testing.TestClient(app) response_one = client.simulate_get('/jars') response_two = client.simulate_post('/jars', cookies=response_one.cookies) assert response_two.status == falcon.HTTP_200
def test_simulate_request_content_type(): class Foo: def on_post(self, req, resp): resp.text = req.content_type app = App() app.add_route('/', Foo()) headers = {'Content-Type': falcon.MEDIA_TEXT} result = testing.simulate_post(app, '/', headers=headers) assert result.text == falcon.MEDIA_TEXT result = testing.simulate_post(app, '/', content_type=falcon.MEDIA_HTML) assert result.text == falcon.MEDIA_HTML result = testing.simulate_post( app, '/', content_type=falcon.MEDIA_HTML, headers=headers ) assert result.text == falcon.MEDIA_HTML result = testing.simulate_post(app, '/', json={}) assert result.text == falcon.MEDIA_JSON result = testing.simulate_post(app, '/', json={}, content_type=falcon.MEDIA_HTML) assert result.text == falcon.MEDIA_JSON result = testing.simulate_post(app, '/', json={}, headers=headers) assert result.text == falcon.MEDIA_JSON result = testing.simulate_post( app, '/', json={}, headers=headers, content_type=falcon.MEDIA_HTML ) assert result.text == falcon.MEDIA_JSON
def get_app(asgi, cors=True, **kw): if asgi: from falcon.asgi import App as AsyncApp return AsyncApp(cors_enable=cors, **kw) else: from falcon import App return App(cors_enable=cors, **kw)
def test_simulate_request_http_version(version, valid): app = App() if valid: testing.simulate_request(app, http_version=version) else: with pytest.raises(ValueError): testing.simulate_request(app, http_version=version)
def api_server(): """ spawn a run forever api server instance and add routing information """ app = App() app.add_route("/gateway", GatewayDepositServer()) print(it("red", "INITIALIZING DEPOSIT SERVER")) print("serving http at") call(["hostname", "-I"]) with make_server("", 8000, app) as httpd: httpd.serve_forever()
def app(self, middleware): return App(middleware=[middleware])
from falcon import App from falconproject.openapi import openapi_middleware from falconproject.pets.resources import PetDetailResource from falconproject.pets.resources import PetListResource app = App(middleware=[openapi_middleware]) pet_list_resource = PetListResource() pet_detail_resource = PetDetailResource() app.add_route("/v1/pets", pet_list_resource) app.add_route("/v1/pets/{petId}", pet_detail_resource)
resp.media = {"name": name} @api.validate( resp=Response(HTTP_200=Resp, HTTP_401=None), tags=[api_tag, "test"], ) def on_post(self, req, resp, name, query: Query, json: JSON, cookies: Cookies): score = [randint(0, req.context.json.limit) for _ in range(5)] score.sort(reverse=req.context.query.order) assert req.context.cookies.pub == "abcdefg" assert req.cookies["pub"] == "abcdefg" resp.media = {"name": req.context.json.name, "score": score} app = App() app.add_route("/ping", Ping()) app.add_route("/api/user/{name}", UserScore()) app.add_route("/api/user_annotated/{name}", UserScoreAnnotated()) api.register(app) @pytest.fixture def client(): return testing.TestClient(app) def test_falcon_validate(client): resp = client.simulate_request("GET", "/ping", headers={"Content-Type": "text/plain"})
from falcon.cmd import print_routes from falcon.testing import redirected try: import cython except ImportError: cython = None class DummyResource: def on_get(self, req, resp): resp.body = 'Test\n' resp.status = '200 OK' _api = App() _api.add_route('/test', DummyResource()) def test_traverse_with_verbose(): """Ensure traverse() finds the proper routes and outputs verbose info.""" output = io.StringIO() with redirected(stdout=output): print_routes.traverse(_api._router._roots, verbose=True) route, get_info, options_info = output.getvalue().strip().split('\n') assert '-> /test' == route # NOTE(kgriffs) We might receive these in either order, since the # method map is not ordered, so check and swap if necessary.