Пример #1
0
    def test_start(self, ResponseImpl):
        req = self.make_request('GET', '/')
        resp = StreamResponse()
        msg = resp.start(req)

        self.assertTrue(msg.send_headers.called)
        self.assertIs(msg, resp.start(req))
Пример #2
0
    def test_reset_charset_after_setting(self):
        resp = StreamResponse()

        resp.content_type = 'text/html'
        resp.charset = 'koi8-r'
        resp.charset = None
        self.assertIsNone(resp.charset)
Пример #3
0
    def test_set_content_length_to_None_on_non_set(self):
        resp = StreamResponse()

        resp.content_length = None
        self.assertNotIn('Content-Length', resp.headers)
        resp.content_length = None
        self.assertNotIn('Content-Length', resp.headers)
Пример #4
0
    async def prepare_download(self, disposition=None, filename=None,
                               content_type=None, size=None, **kwargs):
        if disposition is None:
            disposition = self.request.query.get('disposition', 'attachment')

        try:
            file = self.field.get(self.field.context or self.context)
        except AttributeError:
            file = None

        if file is None and filename is None:
            raise HTTPNotFound(content={
                'message': 'File or custom filename required to download'
            })
        cors_renderer = app_settings['cors_renderer'](self.request)
        headers = await cors_renderer.get_headers()
        headers.update({
            'Content-Disposition': '{}; filename="{}"'.format(
                disposition, filename or file.filename)
        })

        download_resp = StreamResponse(headers=headers)
        download_resp.content_type = content_type or file.guess_content_type()
        if size or file.size:
            download_resp.content_length = size or file.size

        await download_resp.prepare(self.request)
        return download_resp
Пример #5
0
    def test_force_close(self):
        req = self.make_request('GET', '/')
        resp = StreamResponse(req)

        self.assertTrue(resp.keep_alive)
        resp.force_close()
        self.assertFalse(resp.keep_alive)
Пример #6
0
    def test_setting_content_type(self):

        req = self.make_request('GET', '/')
        resp = StreamResponse(req)

        resp.content_type = 'text/html'
        self.assertEqual('text/html', resp.headers['content-type'])
Пример #7
0
    def test_set_status_with_reason(self):
        req = self.make_request('GET', '/')
        resp = StreamResponse(req)

        resp.set_status(200, "Everithing is fine!")
        self.assertEqual(200, resp.status)
        self.assertEqual("Everithing is fine!", resp.reason)
Пример #8
0
    def test_drop_content_length_header_on_setting_len_to_None(self):
        resp = StreamResponse()

        resp.content_length = 1
        self.assertEqual("1", resp.headers['Content-Length'])
        resp.content_length = None
        self.assertNotIn('Content-Length', resp.headers)
Пример #9
0
    def test_reset_charset(self):
        req = self.make_request('GET', '/')
        resp = StreamResponse(req)

        resp.content_type = 'text/html'
        resp.charset = None
        self.assertIsNone(resp.charset)
Пример #10
0
 def test_keep_alive_http09(self):
     headers = CIMultiDict(Connection="keep-alive")
     message = RawRequestMessage("GET", "/", HttpVersion(0, 9), headers, False, False)
     req = self.request_from_message(message)
     resp = StreamResponse()
     self.loop.run_until_complete(resp.prepare(req))
     self.assertFalse(resp.keep_alive)
Пример #11
0
    def test_chunked_encoding_forbidden_for_http_10(self):
        req = self.make_request("GET", "/", version=HttpVersion10)
        resp = StreamResponse()
        resp.enable_chunked_encoding()

        with self.assertRaisesRegex(RuntimeError, "Using chunked encoding is forbidden for HTTP/1.0"):
            self.loop.run_until_complete(resp.prepare(req))
Пример #12
0
    def test_cookie_set_after_del(self):
        resp = StreamResponse()

        resp.del_cookie('name')
        resp.set_cookie('name', 'val')
        # check for Max-Age dropped
        self.assertEqual(str(resp.cookies), 'Set-Cookie: name=val')
Пример #13
0
def test_prepare_twice():
    req = make_request('GET', '/')
    resp = StreamResponse()

    impl1 = yield from resp.prepare(req)
    impl2 = yield from resp.prepare(req)
    assert impl1 is impl2
Пример #14
0
def test_reset_charset_after_setting():
    resp = StreamResponse()

    resp.content_type = 'text/html'
    resp.charset = 'koi8-r'
    resp.charset = None
    assert resp.charset is None
Пример #15
0
def test_keep_alive_http10_switched_on():
    headers = CIMultiDict(Connection='keep-alive')
    req = make_request('GET', '/', version=HttpVersion10, headers=headers)
    req._message = req._message._replace(should_close=False)
    resp = StreamResponse()
    yield from resp.prepare(req)
    assert resp.keep_alive
Пример #16
0
def test_drop_content_length_header_on_setting_len_to_None():
    resp = StreamResponse()

    resp.content_length = 1
    assert "1" == resp.headers['Content-Length']
    resp.content_length = None
    assert 'Content-Length' not in resp.headers
Пример #17
0
def test_keep_alive_http10_default():
    message = RawRequestMessage('GET', '/', HttpVersion10, CIMultiDict(),
                                [], True, False)
    req = request_from_message(message)
    resp = StreamResponse()
    yield from resp.prepare(req)
    assert not resp.keep_alive
Пример #18
0
def test_set_content_length_to_None_on_non_set():
    resp = StreamResponse()

    resp.content_length = None
    assert 'Content-Length' not in resp.headers
    resp.content_length = None
    assert 'Content-Length' not in resp.headers
Пример #19
0
    def test_cannot_send_headers_twice(self):
        req = self.make_request('GET', '/')
        resp = StreamResponse(req)

        resp.send_headers()
        with self.assertRaises(RuntimeError):
            resp.send_headers()
Пример #20
0
    def test_setting_charset(self):
        resp = StreamResponse()

        resp.content_type = 'text/html'
        resp.charset = 'koi8-r'
        self.assertEqual('text/html; charset=koi8-r',
                         resp.headers['content-type'])
Пример #21
0
 def test_keep_alive_http09(self):
     headers = CIMultiDict(Connection='keep-alive')
     message = RawRequestMessage('GET', '/', HttpVersion(0, 9), headers,
                                 False, False)
     req = self.request_from_message(message)
     resp = StreamResponse()
     resp.start(req)
     self.assertFalse(resp.keep_alive)
Пример #22
0
    def test_response_cookie__issue_del_cookie(self):
        resp = StreamResponse()

        self.assertEqual(resp.cookies, {})
        self.assertEqual(str(resp.cookies), '')

        resp.del_cookie('name')
        self.assertEqual(str(resp.cookies), 'Set-Cookie: name=; Max-Age=0')
Пример #23
0
def test_set_tcp_nodelay_on_start():
    req = make_request('GET', '/')
    resp = StreamResponse()

    with mock.patch('aiohttp.web_reqrep.ResponseImpl'):
        resp_impl = yield from resp.prepare(req)
    resp_impl.transport.set_tcp_nodelay.assert_called_with(True)
    resp_impl.transport.set_tcp_cork.assert_called_with(False)
Пример #24
0
def test_set_nodelay_prepared():
    resp = StreamResponse()
    writer = mock.Mock()
    req = make_request('GET', '/', payload_writer=writer)

    yield from resp.prepare(req)
    resp.set_tcp_nodelay(True)
    writer.set_tcp_nodelay.assert_called_with(True)
Пример #25
0
def test_cookie_set_after_del():
    resp = StreamResponse()

    resp.del_cookie('name')
    resp.set_cookie('name', 'val')
    # check for Max-Age dropped
    expected = 'Set-Cookie: name=val; Path=/'
    assert str(resp.cookies) == expected
Пример #26
0
def test_set_cork_prepared():
    resp = StreamResponse()
    writer = mock.Mock()
    req = make_request('GET', '/', writer=writer)

    yield from resp.prepare(req)
    resp.set_tcp_cork(True)
    writer.set_tcp_cork.assert_called_with(True)
Пример #27
0
async def test_start_force_close():
    req = make_request('GET', '/')
    resp = StreamResponse()
    resp.force_close()
    assert not resp.keep_alive

    await resp.prepare(req)
    assert not resp.keep_alive
Пример #28
0
def test_start_twice(warning):
    req = make_request('GET', '/')
    resp = StreamResponse()

    with warning(DeprecationWarning):
        impl1 = resp.start(req)
        impl2 = resp.start(req)
        assert impl1 is impl2
Пример #29
0
def test_get_cork_prepared():
    resp = StreamResponse()
    writer = mock.Mock()
    writer.tcp_cork = False
    req = make_request('GET', '/', writer=writer)

    yield from resp.prepare(req)
    assert not resp.tcp_cork
Пример #30
0
def test_get_nodelay_prepared():
    resp = StreamResponse()
    writer = mock.Mock()
    writer.tcp_nodelay = False
    req = make_request('GET', '/', payload_writer=writer)

    yield from resp.prepare(req)
    assert not resp.tcp_nodelay
Пример #31
0
def test_etag_class(etag, expected_header) -> None:
    resp = StreamResponse()
    resp.etag = etag
    assert resp.etag == etag
    assert resp.headers[hdrs.ETAG] == expected_header
Пример #32
0
def test_etag_invalid_value_get(header) -> None:
    resp = StreamResponse()
    resp.headers["ETag"] = header
    assert resp.etag is None
Пример #33
0
def test_etag_invalid_value_class(invalid) -> None:
    resp = StreamResponse()
    with pytest.raises(ValueError, match="Unsupported etag type"):
        resp.etag = invalid
Пример #34
0
def test_force_close() -> None:
    resp = StreamResponse()

    assert resp.keep_alive is None
    resp.force_close()
    assert resp.keep_alive is False
Пример #35
0
def test_etag_invalid_value_set(invalid_value) -> None:
    resp = StreamResponse()
    with pytest.raises(ValueError, match="is not a valid etag"):
        resp.etag = invalid_value
Пример #36
0
def test_enable_chunked_encoding_with_content_length() -> None:
    resp = StreamResponse()

    resp.content_length = 234
    with pytest.raises(RuntimeError):
        resp.enable_chunked_encoding()
Пример #37
0
async def test_write_non_byteish() -> None:
    resp = StreamResponse()
    await resp.prepare(make_request("GET", "/"))

    with pytest.raises(AssertionError):
        await resp.write(123)
Пример #38
0
async def test_write_before_start() -> None:
    resp = StreamResponse()

    with pytest.raises(RuntimeError):
        await resp.write(b"data")
Пример #39
0
def test_etag_string() -> None:
    resp = StreamResponse()
    value = "0123-kotik"
    resp.etag = value
    assert resp.etag == ETag(value=value)
    assert resp.headers[hdrs.ETAG] == f'"{value}"'
Пример #40
0
async def test_cannot_write_eof_before_headers() -> None:
    resp = StreamResponse()

    with pytest.raises(AssertionError):
        await resp.write_eof()
Пример #41
0
def test_etag_any() -> None:
    resp = StreamResponse()
    resp.etag = "*"
    assert resp.etag == ETag(value="*")
    assert resp.headers[hdrs.ETAG] == "*"
Пример #42
0
async def test___repr__() -> None:
    req = make_request("GET", "/path/to")
    resp = StreamResponse(reason=301)
    await resp.prepare(req)
    assert "<StreamResponse 301 GET /path/to >" == repr(resp)
Пример #43
0
def test_etag_reset() -> None:
    resp = StreamResponse()
    resp.etag = "*"
    resp.etag = None
    assert resp.etag is None
Пример #44
0
def test_set_status_with_reason() -> None:
    resp = StreamResponse()

    resp.set_status(200, "Everithing is fine!")
    assert 200 == resp.status
    assert "Everithing is fine!" == resp.reason
Пример #45
0
def test_last_modified_string_invalid(header_val, expected) -> None:
    resp = StreamResponse(headers={"Last-Modified": header_val})
    assert resp.last_modified == expected
Пример #46
0
def test___repr___not_prepared() -> None:
    resp = StreamResponse(reason=301)
    assert "<StreamResponse 301 not prepared>" == repr(resp)
Пример #47
0
def test_last_modified_datetime() -> None:
    resp = StreamResponse()

    dt = datetime.datetime(2001, 2, 3, 4, 5, 6, 0, datetime.timezone.utc)
    resp.last_modified = dt
    assert resp.last_modified == dt
Пример #48
0
def test_etag_initial() -> None:
    resp = StreamResponse()
    assert resp.etag is None
Пример #49
0
def test_last_modified_string() -> None:
    resp = StreamResponse()

    dt = datetime.datetime(1990, 1, 2, 3, 4, 5, 0, datetime.timezone.utc)
    resp.last_modified = "Mon, 2 Jan 1990 03:04:05 GMT"
    assert resp.last_modified == dt
Пример #50
0
def test_last_modified_reset() -> None:
    resp = StreamResponse()

    resp.last_modified = 0
    resp.last_modified = None
    assert resp.last_modified is None