def test_bounded_stream_alias(self): scope = testing.create_scope() req_event_emitter = testing.ASGIRequestEventEmitter(b'', disconnect_at=0) req = falcon.asgi.Request(scope, req_event_emitter) assert req.bounded_stream is req.stream
def test_request_repr(self): scope = testing.create_scope() req_event_emitter = testing.ASGIRequestEventEmitter(b'', disconnect_at=0) req = falcon.asgi.Request(scope, req_event_emitter) _repr = '<%s: %s %r>' % (req.__class__.__name__, req.method, req.url) assert req.__repr__() == _repr
def test_create_scope_default_ua_override(): ua = 'curl/7.64.1' scope = testing.create_scope(headers={'user-agent': ua}) assert dict(scope['headers'])[b'user-agent'] == ua.encode() req = testing.create_asgi_req(headers={'user-agent': ua}) assert req.user_agent == ua
def test_create_scope_default_ua(): default_ua = 'falcon-client/' + falcon.__version__ scope = testing.create_scope() assert dict(scope['headers'])[b'user-agent'] == default_ua.encode() req = testing.create_asgi_req() assert req.user_agent == default_ua
def test_supported_http_spec(spec_version, supported): scope = testing.create_scope() scope['asgi']['spec_version'] = spec_version if supported: _call_with_scope(scope) else: with pytest.raises(UnsupportedScopeError): _call_with_scope(scope)
def test_missing_asgi_version(): scope = testing.create_scope() del scope['asgi'] resource = _call_with_scope(scope) # NOTE(kgriffs): According to the ASGI spec, the version should # default to "2.0". assert resource.captured_req.scope['asgi']['version'] == '2.0'
def test_supported_asgi_version(version, supported): scope = testing.create_scope() if version: scope['asgi']['version'] = version else: del scope['asgi']['version'] if supported: _call_with_scope(scope) else: with pytest.raises(UnsupportedScopeError): _call_with_scope(scope)
async def test_content_length_smaller_than_body(self, body_length, content_length): body_in = os.urandom(body_length) scope = testing.create_scope(content_length=content_length) req_event_emitter = testing.ASGIRequestEventEmitter(body=body_in) req_event_emitter._emit_empty_chunks = False first_event = await req_event_emitter.emit() req = falcon.asgi.Request(scope, req_event_emitter, first_event=first_event) body_out = await req.bounded_stream.read() assert body_out == body_in[:content_length]
def test_query_string_values(): with pytest.raises(ValueError): testing.create_scope(query_string='?catsup=y') with pytest.raises(ValueError): testing.create_scope(query_string='?') for qs in ('', None): scope = testing.create_scope(query_string=qs) assert scope['query_string'] == b'' resource = _call_with_scope(scope) assert resource.captured_req.query_string == '' qs = 'a=1&b=2&c=%3E%20%3C' scope = testing.create_scope(query_string=qs) assert scope['query_string'] == qs.encode() resource = _call_with_scope(scope) assert resource.captured_req.query_string == qs
def test_unsupported_scope_type(scope_type): scope = testing.create_scope() scope['type'] = scope_type with pytest.raises(UnsupportedScopeError): _call_with_scope(scope)
def test_scheme(scheme, valid): if valid: testing.create_scope(scheme=scheme) else: with pytest.raises(ValueError): testing.create_scope(scheme=scheme)
def test_cookies_options_meathod(): scope = testing.create_scope(method='OPTIONS', cookies={'foo': 'bar'}) assert not any(header == b'cookie' for header, _ in scope['headers'])
def test_cookies(cookies): scope = testing.create_scope(cookies=cookies) assert any(header == b'cookie' for header, _ in scope['headers'])
def test_unsupported_http_version(http_version): scope = testing.create_scope() scope['http_version'] = http_version with pytest.raises(UnsupportedError): _call_with_scope(scope)