async def test_form_request(): response = Response( form={ 'foo': '123', 'bar': 'multi\nline\nstring', 'baz': File('baz.png', 'image/png', b'pngcontent'), }) assert response.content_type == 'multipart/form-data' boundary = response.boundary.encode() assert await response.body() == ( b'--' + boundary + b'\n' b'Content-Disposition: form-data; name=foo\n' b'\n' b'123\n' b'--' + boundary + b'\n' b'Content-Disposition: form-data; name=bar\n' b'\n' b'multi\n' b'line\n' b'string\n' b'--' + boundary + b'\n' b'Content-Disposition: form-data; name=baz; filename=baz.png\n' b'Content-Type: image/png\n' b'\n' b'pngcontent\n' b'--' + boundary + b'--\n') data = await response.form() assert set(data) == {'foo', 'bar', 'baz'} assert data['foo'] == '123' assert data['bar'] == 'multi\nline\nstring' assert data['baz'].content_type == 'image/png' assert data['baz'].content == b'pngcontent'
async def test_json_response(): response = Response(json={'foo': 'bar'}) assert response.status == 200 assert response.headers['Content-Type'] == ( 'application/json; charset=UTF-8') assert await response.body() == b'{"foo": "bar"}' assert await response.text() == '{"foo": "bar"}'
async def method_not_allowed(request, methods): return Response(status=405, json={ 'code': 'method_not_allowed', 'message': 'Method Not Allowed', }, allow=', '.join(sorted(methods)))
async def not_found(request): return Response( status=404, json={ 'code': 'not_found', 'message': 'Not Found', }, )
async def test_file_response_unknown_type(): with tempfile.NamedTemporaryFile(suffix='.unknown') as f: f.write(b'foobar') f.flush() response = Response(file=f.name) assert response.content_type is None assert await response.body() == b'foobar' assert await response.text() == 'foobar'
async def test_file_response(): with tempfile.NamedTemporaryFile(suffix='.txt') as f: f.write(b'foobar') f.flush() response = Response(file=f.name) assert response.content_type == 'text/plain' assert await response.body() == b'foobar' assert await response.text() == 'foobar'
async def view(request): response = await get_response(request) try: prefix = request.query['prefix'] except KeyError: pass else: response = Response(text=prefix + await response.text()) return response
def test_set_cookies(): response = Response(set_cookies=[ Cookie('foo', 'bar'), Cookie('bar', 'baz"qux'), ]) assert response.headers.getlist('Set-Cookie') == [ 'foo=bar', 'bar="baz\\"qux"', ]
def test_unset_cookies(): response = Response( set_cookies=[Cookie('foo', 'bar')], unset_cookies=['foo'], ) assert response.headers.getlist('Set-Cookie') == [ 'foo=bar', 'foo=""; Expires=Thu, 1 Jan 1970 00:00:00 GMT', ]
async def form(request): return Response(html=( '<!doctype html>\n' '<html lang="en">\n' ' <head>\n' ' <title>File Upload</title>\n' ' </head>\n' ' <body>\n' ' <form method="POST" action="/file_upload" ' 'enctype="multipart/form-data">\n' ' <input type="file" name="file" />\n' ' <button>Upload</button>\n' ' </form>\n' ' </body>\n' '</html>\n' ))
async def delete_view(request): return Response(text='delete')
async def bar(request, param): return Response(text=param)
async def foo(request): return Response(text='foo')
async def user_list(request): return Response(text='user list')
async def app(request): offset = int(request.query.get('offset', '0')) size = int(request.query.get('size', '-1')) return Response(body=[SendFile(f.name, offset=offset, size=size)])
async def app(request): return Response(body=request.chunks())
async def user_update(request, user_id): return Response(text=f'user update {user_id}')
async def user_create(request): return Response(text='user create')
async def put_view(request): return Response(text='put')
async def post_update(request, post_id): return Response(text=f'post update {post_id}')
async def user_detail(request, user_id): return Response(text=f'user detail {user_id}')
async def connect_view(request): return Response(text='connect')
async def user_delete(request, user_id): return Response(text=f'user delete {user_id}')
async def options_view(request): return Response(text='options')
async def app(request): name = request.query.get('name', 'World') return Response(text=f'Hello, {name}!')
async def trace_view(request): return Response(text='trace')
async def file_upload(request): form = await request.form() return Response( body=form['file'].content, content_type=form['file'].content_type, )
async def patch_view(request): return Response(text='patch')
async def hello_world(request): count = int(request.cookies.get('count', '0')) + 1 return Response( text=f'count: {count}', set_cookies=[Cookie('count', str(count))], )
async def post_delete(request, post_id): return Response(text=f'post delete {post_id}')