def test_query_string_in_path(self): app = falcon.API() resource = testing.SimpleTestResource() app.add_route('/thing', resource) client = testing.TestClient(app) with pytest.raises(ValueError): client.simulate_get(path='/thing?x=1', query_string='things=1,2,3') with pytest.raises(ValueError): client.simulate_get(path='/thing?x=1', params={'oid': 1978}) with pytest.raises(ValueError): client.simulate_get(path='/thing?x=1', query_string='things=1,2,3', params={'oid': 1978}) client.simulate_get(path='/thing?detailed=no&oid=1337') assert resource.captured_req.path == '/thing' assert resource.captured_req.query_string == 'detailed=no&oid=1337'
def test_headers_as_list(self): headers = [('Client-ID', '692ba466-74bb-11e3-bf3f-7567c531c7ca'), ('Accept', 'audio/*; q=0.2, audio/basic')] # Unit test environ = testing.create_environ(headers=headers) req = falcon.Request(environ) for name, value in headers: self.assertIn((name.upper(), value), req.headers.items()) # Functional test self.api.add_route('/', testing.SimpleTestResource(headers=headers)) result = self.simulate_get() for name, value in headers: self.assertEqual(result.headers[name], value)
def test_simulate_content_type_extra_handler(self, asgi, content_type): class TrackingJSONHandler(media.JSONHandler): def __init__(self): super().__init__() self.deserialize_count = 0 def deserialize(self, *args, **kwargs): result = super().deserialize(*args, **kwargs) self.deserialize_count += 1 return result async def deserialize_async(self, *args, **kwargs): result = await super().deserialize_async(*args, **kwargs) self.deserialize_count += 1 return result resource = testing.SimpleTestResourceAsync( ) if asgi else testing.SimpleTestResource() app = create_app(asgi) app.add_route('/', resource) handler = TrackingJSONHandler() extra_handlers = {'application/json': handler} app.req_options.media_handlers.update(extra_handlers) app.resp_options.media_handlers.update(extra_handlers) client = testing.TestClient(app) headers = { 'Content-Type': content_type, 'capture-req-media': 'y', } payload = b'{"hello": "world"}' resp = client.simulate_post('/', headers=headers, body=payload) if MEDIA_JSON in content_type: # Test that our custom deserializer was called assert handler.deserialize_count == 1 assert resource.captured_req_media == {'hello': 'world'} assert resp.status_code == 200 else: # YAML should not get handled assert handler.deserialize_count == 0 assert resource.captured_req_media is None assert resp.status_code == 415
def test_passthrough_request_headers(self, client): resource = testing.SimpleTestResource(body=SAMPLE_BODY) client.app.add_route('/', resource) request_headers = { 'X-Auth-Token': 'Setec Astronomy', 'Content-Type': 'text/plain; charset=utf-8' } client.simulate_get(headers=request_headers) for name, expected_value in request_headers.items(): actual_value = resource.captured_req.get_header(name) assert actual_value == expected_value client.simulate_get(headers=resource.captured_req.headers) # Compare the request HTTP headers with the original headers for name, expected_value in request_headers.items(): actual_value = resource.captured_req.get_header(name) assert actual_value == expected_value
def test_default_headers_with_override(self, 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
def simulate(): app = falcon.API(middleware=[RequireHTTPS()]) app.add_route(_TEST_PATH, testing.SimpleTestResource()) def simulate(protocol, headers=None): env = testing.create_environ( method='GET', scheme=protocol, path=_TEST_PATH, query_string='', headers=headers, ) srmock = testing.StartResponseMock() iterable = app(env, srmock) return testing.Result(iterable, srmock.status, srmock.headers) return simulate
def test_headers_as_list(self, client): headers = [ ('Client-ID', '692ba466-74bb-11e3-bf3f-7567c531c7ca'), ('Accept', 'audio/*; q=0.2, audio/basic'), ] # Unit test environ = testing.create_environ(headers=headers) req = falcon.Request(environ) for name, value in headers: assert (name.upper(), value) in req.headers.items() assert (name.lower(), value) in req.headers_lower.items() # Functional test client.app.add_route('/', testing.SimpleTestResource(headers=headers)) result = client.simulate_get() for name, value in headers: assert result.headers[name] == value
def test_simulate_json_body(self, asgi, document): resource = testing.SimpleTestResourceAsync() if asgi else testing.SimpleTestResource() app = create_app(asgi) app.add_route('/', resource) json_types = ('application/json', 'application/json; charset=UTF-8') client = testing.TestClient(app) client.simulate_post('/', json=document, headers={'capture-req-body-bytes': '-1'}) assert json.loads(resource.captured_req_body.decode()) == document assert resource.captured_req.content_type in json_types headers = { 'Content-Type': 'x-falcon/peregrine', 'X-Falcon-Type': 'peregrine', 'capture-req-media': 'y' } body = 'If provided, `json` parameter overrides `body`.' client.simulate_post('/', headers=headers, body=body, json=document) assert resource.captured_req_media == document assert resource.captured_req.content_type in json_types assert resource.captured_req.get_header('X-Falcon-Type') == 'peregrine'
def test_simulate_json_body(self, document): app = falcon.API() resource = testing.SimpleTestResource() app.add_route('/', resource) json_types = ('application/json', 'application/json; charset=UTF-8') client = testing.TestClient(app) client.simulate_post('/', json=document) captured_body = resource.captured_req.bounded_stream.read().decode('utf-8') assert json.loads(captured_body) == document assert resource.captured_req.content_type in json_types headers = { 'Content-Type': 'x-falcon/peregrine', 'X-Falcon-Type': 'peregrine', } body = 'If provided, `json` parameter overrides `body`.' client.simulate_post('/', headers=headers, body=body, json=document) assert resource.captured_req.media == document assert resource.captured_req.content_type in json_types assert resource.captured_req.get_header('X-Falcon-Type') == 'peregrine'
def test_path_must_start_with_slash(self, app): app.add_route('/', testing.SimpleTestResource()) client = testing.TestClient(app) with pytest.raises(ValueError): client.simulate_get('foo')
def setUp(self): super(TestSetupApi, self).setUp() with pytest.warns(UserWarning, match='API class may be removed in a future'): self.app = falcon.API() self.app.add_route('/', testing.SimpleTestResource(body='test'))
def resource(): return testing.SimpleTestResource()
def test_query_string_in_path(self): app = falcon.API() app.add_route('/', testing.SimpleTestResource()) client = testing.TestClient(app) with pytest.raises(ValueError): client.simulate_get(path='/thing?x=1')
def test_simple_resource_body_json_xor(self): with pytest.raises(ValueError): testing.SimpleTestResource(body='', json={})
def test_status(self): resource = testing.SimpleTestResource(status=falcon.HTTP_702) self.api.add_route('/', resource) result = self.simulate_get() self.assertEqual(result.status, falcon.HTTP_702)
def test_no_content_length(self, client, status): client.app.add_route('/xxx', testing.SimpleTestResource(status=status)) result = client.simulate_get('/xxx') assert 'Content-Length' not in result.headers assert not result.content
def test_default_media_type(self, client): resource = testing.SimpleTestResource(body='Hello world!') self._check_header(client, resource, 'Content-Type', falcon.DEFAULT_MEDIA_TYPE)
def setUp(self): super(TestHeaders, self).setUp() self.sample_body = testing.rand_string(0, 128 * 1024) self.resource = testing.SimpleTestResource(body=self.sample_body) self.api.add_route('/', self.resource)
def before(self): self.resource = testing.SimpleTestResource() self.api.add_route('/', self.resource)
def test_cached_text_in_result(self): self.api.add_route('/', testing.SimpleTestResource(body='test')) result = self.simulate_get() self.assertEqual(result.text, result.text)
def test_cached_text_in_result(self, app): app.add_route('/', testing.SimpleTestResource(body='test')) client = testing.TestClient(app) result = client.simulate_get() assert result.text == result.text
def test_query_string_no_question(self, app): app.add_route('/', testing.SimpleTestResource()) client = testing.TestClient(app) with pytest.raises(ValueError): client.simulate_get(query_string='?x=1')
def setUp(self): super(TestSetupApi, self).setUp() self.app = falcon.API() self.app.add_route('/', testing.SimpleTestResource(body='test'))
def test_no_content_length(self, status): self.api.add_route('/xxx', testing.SimpleTestResource(status=status)) result = self.simulate_get('/xxx') self.assertNotIn('Content-Length', result.headers) self.assertFalse(result.content)
def client_fixture(auth_middleware) -> testing.TestClient: api = API(middleware=auth_middleware) api.add_route('/', testing.SimpleTestResource()) return testing.TestClient(api)
def test_no_content_type(self, client, status): client.app.add_route('/', testing.SimpleTestResource(status=status)) result = client.simulate_get() assert 'Content-Type' not in result.headers
def test_no_content_type(self, status): self.api.add_route('/', testing.SimpleTestResource(status=status)) result = self.simulate_get() self.assertNotIn('Content-Type', result.headers)