コード例 #1
0
    def test_handle_request_keep_alive(self):

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

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

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

        srv = wsgi.WSGIServerHttpProtocol(
            wsgi_app, readpayload=True, loop=self.loop)
        srv.stream = self.stream
        srv.transport = self.transport

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

        content = b''.join(
            [c[1][0] for c in self.transport.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._keep_alive)
コード例 #2
0
 def test_http_payload_parser_length_zero(self):
     msg = protocol.RawRequestMessage('GET', '/', (1, 1),
                                      [('CONTENT-LENGTH', '0')], None, None)
     p = protocol.http_payload_parser(msg)
     next(p)
     out = tulip.DataBuffer()
     buf = tulip.ParserBuffer()
     self.assertRaises(StopIteration, p.send, (out, buf))
     self.assertEqual(b'', b''.join(out._buffer))
コード例 #3
0
    def test_environ_host_port_header(self):
        self.message = protocol.RawRequestMessage(
            'GET', '/path', (1, 1), self.headers, True, 'deflate')
        self.headers.append(('HOST', 'python.org:443'))
        environ = self._make_one()

        self.assertEqual(environ['HTTP_HOST'], 'python.org:443')
        self.assertEqual(environ['SERVER_NAME'], 'python.org')
        self.assertEqual(environ['SERVER_PORT'], '443')
        self.assertEqual(environ['SERVER_PROTOCOL'], 'HTTP/1.1')
コード例 #4
0
    def test_http_payload_parser_no_length(self):
        msg = protocol.RawRequestMessage('GET', '/', (1, 1), [], None, None)
        p = protocol.http_payload_parser(msg, readall=False)
        next(p)

        out = tulip.DataBuffer()
        buf = tulip.ParserBuffer()
        self.assertRaises(StopIteration, p.send, (out, buf))
        self.assertEqual(b'', b''.join(out._buffer))
        self.assertTrue(out._eof)
コード例 #5
0
    def test_http_payload_parser_length_wrong(self):
        msg = protocol.RawRequestMessage('GET', '/', (1, 1),
                                         [('CONTENT-LENGTH', '-1')], None,
                                         None)
        p = protocol.http_payload_parser(msg)
        next(p)

        out = tulip.DataBuffer()
        buf = tulip.ParserBuffer()
        self.assertRaises(errors.InvalidHeader, p.send, (out, buf))
コード例 #6
0
 def test_http_payload_parser_eof(self):
     msg = protocol.RawRequestMessage('GET', '/', (1, 1), [], None, None)
     p = protocol.http_payload_parser(msg, readall=True)
     next(p)
     out = tulip.DataBuffer()
     buf = tulip.ParserBuffer()
     p.send((out, buf))
     p.send(b'data')
     p.send(b'line')
     self.assertRaises(tulip.EofStream, p.throw, tulip.EofStream())
     self.assertEqual(b'dataline', b''.join(out._buffer))
コード例 #7
0
 def test_http_payload_parser_websocket(self):
     msg = protocol.RawRequestMessage('GET', '/', (1, 1),
                                      [('SEC-WEBSOCKET-KEY1', '13')], None,
                                      None)
     p = protocol.http_payload_parser(msg)
     next(p)
     out = tulip.DataBuffer()
     buf = tulip.ParserBuffer()
     p.send((out, buf))
     self.assertRaises(StopIteration, p.send, b'1234567890')
     self.assertEqual(b'12345678', b''.join(out._buffer))
コード例 #8
0
 def test_http_payload_parser_chunked(self):
     msg = protocol.RawRequestMessage('GET', '/', (1, 1),
                                      [('TRANSFER-ENCODING', 'chunked')],
                                      None, None)
     p = protocol.http_payload_parser(msg)
     next(p)
     out = tulip.DataBuffer()
     buf = tulip.ParserBuffer()
     p.send((out, buf))
     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(out._buffer))
コード例 #9
0
    def test_http_payload_parser_deflate_disabled(self):
        msg = protocol.RawRequestMessage(
            'GET', '/', (1, 1), [('CONTENT-LENGTH', len(self._COMPRESSED))],
            None, 'deflate')
        p = protocol.http_payload_parser(msg, compression=False)
        next(p)

        out = tulip.DataBuffer()
        buf = tulip.ParserBuffer()
        p.send((out, buf))
        self.assertRaises(StopIteration, p.send, self._COMPRESSED)
        self.assertEqual(self._COMPRESSED, b''.join(out._buffer))
コード例 #10
0
    def setUp(self):
        self.loop = tulip.new_event_loop()
        tulip.set_event_loop(None)

        self.wsgi = unittest.mock.Mock()
        self.stream = unittest.mock.Mock()
        self.transport = unittest.mock.Mock()
        self.transport.get_extra_info.return_value = '127.0.0.1'

        self.headers = []
        self.message = protocol.RawRequestMessage(
            'GET', '/path', (1, 0), self.headers, True, 'deflate')
        self.payload = tulip.DataBuffer()
        self.payload.feed_data(b'data')
        self.payload.feed_data(b'data')
        self.payload.feed_eof()
コード例 #11
0
    def test_http_payload_parser_length(self):
        msg = protocol.RawRequestMessage('GET', '/', (1, 1),
                                         [('CONTENT-LENGTH', '2')], None, None)
        p = protocol.http_payload_parser(msg)
        next(p)

        out = tulip.DataBuffer()
        buf = tulip.ParserBuffer()
        p.send((out, buf))
        try:
            p.send(b'1245')
        except StopIteration:
            pass

        self.assertEqual(b'12', b''.join(out._buffer))
        self.assertEqual(b'45', bytes(buf))
コード例 #12
0
 def setUp(self):
     self.transport = unittest.mock.Mock()
     self.headers = []
     self.message = protocol.RawRequestMessage(
         'GET', '/path', (1, 0), self.headers, True, None)