Example #1
0
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'
Example #2
0
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"}'
Example #3
0
 async def method_not_allowed(request, methods):
     return Response(status=405,
                     json={
                         'code': 'method_not_allowed',
                         'message': 'Method Not Allowed',
                     },
                     allow=', '.join(sorted(methods)))
Example #4
0
 async def not_found(request):
     return Response(
         status=404,
         json={
             'code': 'not_found',
             'message': 'Not Found',
         },
     )
Example #5
0
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'
Example #6
0
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'
Example #7
0
 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
Example #8
0
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"',
    ]
Example #9
0
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',
    ]
Example #10
0
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'
    ))
Example #11
0
 async def delete_view(request):
     return Response(text='delete')
Example #12
0
 async def bar(request, param):
     return Response(text=param)
Example #13
0
 async def foo(request):
     return Response(text='foo')
Example #14
0
async def user_list(request):
    return Response(text='user list')
Example #15
0
 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)])
Example #16
0
 async def app(request):
     return Response(body=request.chunks())
Example #17
0
async def user_update(request, user_id):
    return Response(text=f'user update {user_id}')
Example #18
0
async def user_create(request):
    return Response(text='user create')
Example #19
0
 async def put_view(request):
     return Response(text='put')
Example #20
0
async def post_update(request, post_id):
    return Response(text=f'post update {post_id}')
Example #21
0
async def user_detail(request, user_id):
    return Response(text=f'user detail {user_id}')
Example #22
0
 async def connect_view(request):
     return Response(text='connect')
Example #23
0
async def user_delete(request, user_id):
    return Response(text=f'user delete {user_id}')
Example #24
0
 async def options_view(request):
     return Response(text='options')
Example #25
0
 async def app(request):
     name = request.query.get('name', 'World')
     return Response(text=f'Hello, {name}!')
Example #26
0
 async def trace_view(request):
     return Response(text='trace')
Example #27
0
async def file_upload(request):
    form = await request.form()
    return Response(
        body=form['file'].content,
        content_type=form['file'].content_type,
    )
Example #28
0
 async def patch_view(request):
     return Response(text='patch')
Example #29
0
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))],
    )
Example #30
0
async def post_delete(request, post_id):
    return Response(text=f'post delete {post_id}')