def test_translate_request_with_large_body(self): receive = AsyncMock(side_effect=[ {'type': 'http.request', 'body': b'hello ', 'more_body': True}, {'type': 'http.request', 'body': b'world', 'more_body': True}, {'type': 'foo.bar'}, # should stop parsing here {'type': 'http.request', 'body': b'!!!'}, ]) send = AsyncMock() environ = _run(async_asgi.translate_request({ 'type': 'http', 'method': 'PUT', 'headers': [(b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'), (b'content-type', b'application/json'), (b'content-length', b'123')], 'path': '/foo/bar', 'query_string': b'baz=1'}, receive, send)) expected_environ = { 'REQUEST_METHOD': 'PUT', 'PATH_INFO': '/foo/bar', 'QUERY_STRING': 'baz=1', 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': '123', 'HTTP_A': 'b', # 'HTTP_C_C': 'd,e', 'RAW_URI': '/foo/bar?baz=1', 'SERVER_PROTOCOL': 'HTTP/1.1', 'asgi.receive': receive, 'asgi.send': send } for k, v in expected_environ.items(): self.assertEqual(v, environ[k]) self.assertTrue( environ['HTTP_C_C'] == 'd,e' or environ['HTTP_C_C'] == 'e,d') body = _run(environ['wsgi.input'].read()) self.assertEqual(body, b'hello world')
def test_translate_unknown_request(self): receive = AsyncMock(return_value={'type': 'http.foo'}) send = AsyncMock() environ = _run(async_asgi.translate_request({ 'type': 'http', 'path': '/foo/bar', 'query_string': b'baz=1'}, receive, send)) self.assertEqual(environ, {})
def test_translate_websocket_request(self): receive = AsyncMock(return_value={'type': 'websocket.connect'}) send = AsyncMock() _run(async_asgi.translate_request({ 'type': 'websocket', 'headers': [(b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'), (b'content-type', b'application/json'), (b'content-length', b'123')], 'path': '/foo/bar', 'query_string': b'baz=1'}, receive, send)) send.mock.assert_called_once_with({'type': 'websocket.accept'})
def test_translate_request(self): receive = AsyncMock(return_value={ 'type': 'http.request', 'body': b'hello world' }) send = AsyncMock() environ = _run( async_asgi.translate_request( { 'type': 'http', 'method': 'PUT', 'headers': [ (b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'), (b'content-type', b'application/json'), (b'content-length', b'123'), ], 'path': '/foo/bar', 'query_string': b'baz=1', }, receive, send, )) expected_environ = { 'REQUEST_METHOD': 'PUT', 'PATH_INFO': '/foo/bar', 'QUERY_STRING': 'baz=1', 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': '123', 'HTTP_A': 'b', # 'HTTP_C_C': 'd,e', 'RAW_URI': '/foo/bar?baz=1', 'SERVER_PROTOCOL': 'HTTP/1.1', 'asgi.receive': receive, 'asgi.send': send, } for k, v in expected_environ.items(): assert v == environ[k] assert environ['HTTP_C_C'] == 'd,e' or environ['HTTP_C_C'] == 'e,d' body = _run(environ['wsgi.input'].read()) assert body == b'hello world'