aiohttp.protocol.RawRequestMessage

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

20 Examples 7

Example 1

Project: sockjs
License: View license
Source File: test_base.py
def make_raw_request_message(method, path, headers, version=HttpVersion11,
                             should_close=False, compression=False):
    raw_headers = [(k.encode('utf-8'), v.encode('utf-8'))
                   for k, v in headers.items()]
    try:
        message = RawRequestMessage(method=method, path=path, headers=headers,
                                    raw_headers=raw_headers,
                                    version=version, should_close=should_close,
                                    compression=compression)
    except TypeError:  # aiohttp < 0.21.x
        message = RawRequestMessage(method=method, path=path, headers=headers,
                                    version=version, should_close=should_close,
                                    compression=compression)
    return message

Example 2

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 3

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 4

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 5

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 6

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 7

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 8

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 9

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 10

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 11

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 12

Project: aiohttp
License: View license
Source File: test_wsgi.py
    def test_dont_unquote_environ_path_info(self):
        path = '/path/some%20text'
        self.message = protocol.RawRequestMessage(
            'GET', path, (1, 0), self.headers, self.raw_headers,
            True, 'deflate')
        environ = self._make_one()
        self.assertEqual(environ['PATH_INFO'], path)

Example 13

Project: aiohttp
License: View license
Source File: test_wsgi.py
Function: test_authorization
    def test_authorization(self):
        # This header should be removed according to CGI/1.1 and WSGI but
        # in our case basic auth is not handled by server, so should
        # not be removed
        self.headers.extend({'AUTHORIZATION': 'spam'})
        self.message = protocol.RawRequestMessage(
            'GET', '/', (1, 1), self.headers, self.raw_headers,
            True, 'deflate')
        environ = self._make_one()
        self.assertEqual('spam', environ['HTTP_AUTHORIZATION'])

Example 14

Project: aiohttp
License: View license
Source File: test_wsgi.py
    def test_http_1_0_no_host(self):
        headers = multidict.MultiDict({})
        self.message = protocol.RawRequestMessage(
            'GET', '/', (1, 0), headers, [], True, 'deflate')
        environ = self._make_one()
        self.assertEqual(environ['SERVER_NAME'], '2.3.4.5')
        self.assertEqual(environ['SERVER_PORT'], '80')

Example 15

Project: aiohttp
License: View license
Source File: test_wsgi.py
    def test_family_inet6(self):
        self.transport.get_extra_info.side_effect = [
            mock.Mock(family=socket.AF_INET6),
            ("::", 1122, 0, 0),
            ('2.3.4.5', 80)]
        self.message = protocol.RawRequestMessage(
            'GET', '/', (1, 0), self.headers, self.raw_headers,
            True, 'deflate')
        environ = self._make_one()
        self.assertEqual(environ['SERVER_NAME'], 'python.org')
        self.assertEqual(environ['SERVER_PORT'], '80')
        self.assertEqual(environ['REMOTE_ADDR'], '::')
        self.assertEqual(environ['REMOTE_PORT'], '1122')

Example 16

Project: aiohttp
License: View license
Source File: test_wsgi.py
    def test_family_unix(self):
        if not hasattr(socket, "AF_UNIX"):
            self.skipTest("No UNIX address family. (Windows?)")
        self.transport.get_extra_info.side_effect = [
            mock.Mock(family=socket.AF_UNIX)]
        headers = multidict.MultiDict({
            'SERVER_NAME': '1.2.3.4', 'SERVER_PORT': '5678',
            'REMOTE_ADDR': '4.3.2.1', 'REMOTE_PORT': '8765'})
        self.message = protocol.RawRequestMessage(
            'GET', '/', (1, 0), headers, self.raw_headers, True, 'deflate')
        environ = self._make_one()
        self.assertEqual(environ['SERVER_NAME'], '1.2.3.4')
        self.assertEqual(environ['SERVER_PORT'], '5678')
        self.assertEqual(environ['REMOTE_ADDR'], '4.3.2.1')
        self.assertEqual(environ['REMOTE_PORT'], '8765')

Example 17

Project: aiohttp
License: View license
Source File: test_websocket_handshake.py
Function: message
@pytest.fixture()
def message():
    headers = multidict.MultiDict()
    return protocol.RawRequestMessage(
        'GET', '/path', (1, 0), headers, [], True, None)

Example 18

Project: aiohttp
License: View license
Source File: test_wsgi.py
Function: set_up
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        self.wsgi = mock.Mock()
        self.reader = mock.Mock()
        self.writer = mock.Mock()
        self.writer.drain.return_value = ()
        self.transport = mock.Mock()
        self.transport.get_extra_info.side_effect = [
            mock.Mock(family=socket.AF_INET),
            ('1.2.3.4', 1234),
            ('2.3.4.5', 80)]

        self.headers = multidict.CIMultiDict({"HOST": "python.org"})
        self.raw_headers = [(b"HOST", b"python.org")]
        self.message = protocol.RawRequestMessage(
            'GET', '/path', (1, 0), self.headers, self.raw_headers,
            True, 'deflate')
        self.payload = aiohttp.FlowControlDataQueue(self.reader)
        self.payload.feed_data(b'data', 4)
        self.payload.feed_data(b'data', 4)
        self.payload.feed_eof()

Example 19

Project: aiohttp
License: View license
Source File: test_wsgi.py
    def test_handle_request_simple(self):

        def wsgi_app(env, start):
            start('200 OK', [('Content-Type', 'text/plain')])
            return [b'data']

        stream = asyncio.StreamReader(loop=self.loop)
        stream.feed_data(b'data')
        stream.feed_eof()

        self.message = protocol.RawRequestMessage(
            'GET', '/path', (1, 1), self.headers, self.raw_headers,
            True, 'deflate')

        srv = self._make_srv(wsgi_app, readpayload=True)
        self.loop.run_until_complete(
            srv.handle_request(self.message, self.payload))

        content = b''.join(
            [c[1][0] for c in self.writer.write.mock_calls])
        self.assertTrue(content.startswith(b'HTTP/1.1 200 OK'))
        self.assertTrue(content.endswith(b'data\r\n0\r\n\r\n'))
        self.assertFalse(srv._keepalive)

Example 20

Project: aiohttp
License: View license
Source File: test_wsgi.py
    def test_handle_request_keep_alive(self):

        def wsgi_app(env, start):
            start('200 OK', [('Content-Type', 'text/plain')])
            return [b'data']

        stream = asyncio.StreamReader(loop=self.loop)
        stream.feed_data(b'data')
        stream.feed_eof()

        self.message = protocol.RawRequestMessage(
            'GET', '/path', (1, 1), self.headers, self.raw_headers,
            False, 'deflate')

        srv = self._make_srv(wsgi_app, readpayload=True)

        self.loop.run_until_complete(
            srv.handle_request(self.message, self.payload))

        content = b''.join(
            [c[1][0] for c in self.writer.write.mock_calls])
        self.assertTrue(content.startswith(b'HTTP/1.1 200 OK'))
        self.assertTrue(content.endswith(b'data\r\n0\r\n\r\n'))
        self.assertTrue(srv._keepalive)