aiohttp.CIMultiDict

Here are the examples of the python api aiohttp.CIMultiDict taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

22 Examples 7

Example 1

Project: aiorest
License: View license
Source File: cookies_test.py
    def test_no_request_cookies(self):
        req = Request('host', aiohttp.RawRequestMessage(
            'GET', '/some/path', '1.1', CIMultiDict(),
            True, None),
            None, loop=self.loop)

        self.assertEqual(req.cookies, {})

        cookies = req.cookies
        self.assertIs(cookies, req.cookies)

Example 2

Project: sockjs
License: View license
Source File: test_route.py
    def test_iframe_cache(self):
        route = self.make_route()
        request = self.make_request(
            'GET', '/sm/',
            headers=CIMultiDict({'IF-NONE-MATCH': 'test'}))
        response = route.iframe(request)

        self.assertEqual(response.status, 304)

Example 3

Project: sockjs
License: View license
Source File: test_route.py
    @mock.patch('sockjs.route.RawWebSocketTransport')
    def test_raw_websocket(self, ws):
        ws.return_value.process.return_value = asyncio.Future(loop=self.loop)
        ws.return_value.process.return_value.set_result(web.HTTPOk())

        route = self.make_route()
        request = self.make_request(
            'GET', '/sm/', headers=CIMultiDict({}))
        res = self.loop.run_until_complete(route.websocket(request))

        self.assertIsInstance(res, web.HTTPOk)
        self.assertTrue(ws.called)
        self.assertTrue(ws.return_value.process.called)

Example 4

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_length_broken(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('CONTENT-LENGTH', 'qwe')]),
            [(b'CONTENT-LENGTH', b'qwe')],
            None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        self.assertRaises(errors.InvalidHeader, next, p)

Example 5

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_length_wrong(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('CONTENT-LENGTH', '-1')]),
            [(b'CONTENT-LENGTH', b'-1')],
            None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        self.assertRaises(errors.InvalidHeader, next, p)

Example 6

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_length(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('CONTENT-LENGTH', '2')]),
            [(b'CONTENT-LENGTH', b'2')],
            None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        next(p)
        try:
            p.send(b'1245')
        except StopIteration:
            pass

        self.assertEqual(b'12', b''.join(d for d, _ in out._buffer))
        self.assertEqual(b'45', bytes(buf))

Example 7

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_no_length(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1), CIMultiDict(), [], None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg, readall=False)(out, buf)
        self.assertRaises(StopIteration, next, p)
        self.assertEqual(b'', b''.join(out._buffer))
        self.assertTrue(out._eof)

Example 8

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_deflate(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('CONTENT-LENGTH', str(len(self._COMPRESSED)))]),
            [(b'CONTENT-LENGTH', str(len(self._COMPRESSED)).encode('ascii'))],
            None, 'deflate')

        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        next(p)
        self.assertRaises(StopIteration, p.send, self._COMPRESSED)
        self.assertEqual(b'data', b''.join(d for d, _ in out._buffer))

Example 9

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_deflate_disabled(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('CONTENT-LENGTH', len(self._COMPRESSED))]),
            [(b'CONTENT-LENGTH', str(len(self._COMPRESSED)).encode('ascii'))],
            None, 'deflate')

        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg, compression=False)(out, buf)
        next(p)
        self.assertRaises(StopIteration, p.send, self._COMPRESSED)
        self.assertEqual(self._COMPRESSED, b''.join(d for d, _ in out._buffer))

Example 10

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_websocket(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('SEC-WEBSOCKET-KEY1', '13')]),
            [(b'SEC-WEBSOCKET-KEY1', b'13')],
            None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        next(p)
        self.assertRaises(StopIteration, p.send, b'1234567890')
        self.assertEqual(b'12345678', b''.join(d for d, _ in out._buffer))

Example 11

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_chunked(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('TRANSFER-ENCODING', 'chunked')]),
            [(b'TRANSFER-ENCODING', b'chunked')],
            None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        next(p)
        self.assertRaises(StopIteration, p.send,
                          b'4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n')
        self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer))

Example 12

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_eof(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1), CIMultiDict(), [], None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg, readall=True)(out, buf)
        next(p)
        p.send(b'data')
        p.send(b'line')
        self.assertRaises(StopIteration, p.throw, aiohttp.EofStream())
        self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer))

Example 13

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_payload_parser_length_zero(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1),
            CIMultiDict([('CONTENT-LENGTH', '0')]),
            [(b'CONTENT-LENGTH', b'0')],
            None, None)
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpPayloadParser(msg)(out, buf)
        self.assertRaises(StopIteration, next, p)
        self.assertEqual(b'', b''.join(out._buffer))

Example 14

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_request_parser(self):
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpRequestParser()(out, buf)
        next(p)
        try:
            p.send(b'get /path HTTP/1.1\r\n\r\n')
        except StopIteration:
            pass
        result = out._buffer[0][0]
        self.assertEqual(
            ('GET', '/path', (1, 1), CIMultiDict(), [], False, None),
            result)

Example 15

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_request_parser_utf8(self):
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpRequestParser()(out, buf)
        next(p)
        msg = 'get /path HTTP/1.1\r\nx-test:тест\r\n\r\n'.encode('utf-8')
        try:
            p.send(msg)
        except StopIteration:
            pass
        result, length = out._buffer[0]
        self.assertEqual(len(msg), length)
        self.assertEqual(
            ('GET', '/path', (1, 1),
             CIMultiDict([('X-TEST', 'тест')]),
             [(b'X-TEST', 'тест'.encode('utf-8'))],
             False, None),
            result)

Example 16

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_request_parser_non_utf8(self):
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpRequestParser()(out, buf)
        next(p)
        msg = 'get /path HTTP/1.1\r\nx-test:тест\r\n\r\n'.encode('cp1251')
        try:
            p.send(msg)
        except StopIteration:
            pass
        result, length = out._buffer[0]
        self.assertEqual(len(msg), length)
        self.assertEqual(
            ('GET', '/path', (1, 1),
             CIMultiDict([('X-TEST', 'тест'.encode('cp1251').decode(
                 'utf-8', 'surrogateescape'))]),
             [(b'X-TEST', 'тест'.encode('cp1251'))],
             False, None),
            result)

Example 17

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_request_parser_two_slashes(self):
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpRequestParser()(out, buf)
        next(p)
        try:
            p.send(b'get //path HTTP/1.1\r\n\r\n')
        except StopIteration:
            pass
        self.assertEqual(
            ('GET', '//path', (1, 1), CIMultiDict(), [], False, None),
            out._buffer[0][0])

Example 18

Project: aiohttp
License: View license
Source File: test_http_parser.py
    def test_http_response_parser_utf8(self):
        out = aiohttp.FlowControlDataQueue(self.stream)
        buf = aiohttp.ParserBuffer()
        p = protocol.HttpResponseParser()(out, buf)
        next(p)
        msg = 'HTTP/1.1 200 Ok\r\nx-test:тест\r\n\r\n'.encode('utf-8')
        try:
            p.send(msg)
        except StopIteration:
            pass
        v, s, r, h = out._buffer[0][0][:4]
        self.assertEqual(v, (1, 1))
        self.assertEqual(s, 200)
        self.assertEqual(r, 'Ok')
        self.assertEqual(h, CIMultiDict([('X-TEST', 'тест')]))

Example 19

Project: aiohttp
License: View license
Source File: test_web_websocket.py
@pytest.fixture
def make_request(app, writer, reader):
    def maker(method, path, headers=None, protocols=False):
        if headers is None:
            headers = CIMultiDict(
                {'HOST': 'server.example.com',
                 'UPGRADE': 'websocket',
                 'CONNECTION': 'Upgrade',
                 'SEC-WEBSOCKET-KEY': 'dGhlIHNhbXBsZSBub25jZQ==',
                 'ORIGIN': 'http://example.com',
                 'SEC-WEBSOCKET-VERSION': '13'})
        if protocols:
            headers['SEC-WEBSOCKET-PROTOCOL'] = 'chat, superchat'

        return make_mocked_request(method, path, headers,
                                   app=app, writer=writer, reader=reader)

    return maker

Example 20

Project: aiohttp
License: View license
Source File: test_web_websocket.py
@asyncio.coroutine
def test_start_without_upgrade(make_request):
    req = make_request('GET', '/',
                       headers=CIMultiDict({}))
    ws = WebSocketResponse()
    with pytest.raises(HTTPBadRequest):
        yield from ws.prepare(req)

Example 21

Project: sockjs
License: View license
Source File: test_base.py
Function: make_request
    def make_request(self, method, path, query_params={}, headers=None,
                     match_info=None):
        if query_params:
            path = '%s?%s' % (path, urllib.parse.urlencode(query_params))

        # Ported from:
        # https://github.com/KeepSafe/aiohttp/blob/fa06acc2392c516491bdb25301ad3ef2b700ff5f/tests/test_web_websocket.py#L21-L45  # noqa
        self.app = mock.Mock()
        self.app._debug = False
        if headers is None:
            headers = CIMultiDict(
                {'HOST': 'server.example.com',
                 'UPGRADE': 'websocket',
                 'CONNECTION': 'Upgrade',
                 'SEC-WEBSOCKET-KEY': 'dGhlIHNhbXBsZSBub25jZQ==',
                 'ORIGIN': 'http://example.com',
                 'SEC-WEBSOCKET-PROTOCOL': 'chat, superchat',
                 'SEC-WEBSOCKET-VERSION': '13'})

        message = make_raw_request_message(method, path, headers)
        self.payload = mock.Mock()
        self.transport = mock.Mock()
        self.reader = mock.Mock()
        self.writer = mock.Mock()
        self.app.loop = self.loop
        self.app.on_response_prepare = Signal(self.app)
        req = Request(self.app, message, self.payload,
                      self.transport, self.reader, self.writer)
        req._match_info = match_info
        return req

Example 22

Project: aiohttp
License: View license
Source File: test_web_websocket.py
def test_can_prepare_without_upgrade(make_request):
    req = make_request('GET', '/',
                       headers=CIMultiDict({}))
    ws = WebSocketResponse()
    assert (False, None) == ws.can_prepare(req)