Пример #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_wsgi_response(self):
        srv = wsgi.WSGIServerHttpProtocol(self.wsgi, loop=self.loop)
        srv.stream = self.stream
        srv.transport = self.transport

        resp = srv.create_wsgi_response(self.message)
        self.assertIsInstance(resp, wsgi.WsgiResponse)
Пример #3
0
    def test_wsgi_response_start_response_exc(self):
        srv = wsgi.WSGIServerHttpProtocol(self.wsgi, loop=self.loop)
        srv.stream = self.stream
        srv.transport = self.transport

        resp = srv.create_wsgi_response(self.message)
        resp.start_response(
            '200 OK', [('CONTENT-TYPE', 'text/plain')], ['', ValueError()])
        self.assertEqual(resp.status, '200 OK')
        self.assertIsInstance(resp.response, protocol.Response)
Пример #4
0
    def test_wsgi_response_101_upgrade_to_websocket(self, m_tulip):
        srv = wsgi.WSGIServerHttpProtocol(self.wsgi, loop=self.loop)
        srv.stream = self.stream
        srv.transport = self.transport

        resp = srv.create_wsgi_response(self.message)
        resp.start_response(
            '101 Switching Protocols', (('UPGRADE', 'websocket'),
                                        ('CONNECTION', 'upgrade')))
        self.assertEqual(resp.status, '101 Switching Protocols')
        self.assertTrue(m_tulip.http.Response.return_value.send_headers.called)
Пример #5
0
    def test_wsgi_response_start_response_exc_status(self):
        srv = wsgi.WSGIServerHttpProtocol(self.wsgi, loop=self.loop)
        srv.stream = self.stream
        srv.transport = self.transport

        resp = srv.create_wsgi_response(self.message)
        resp.start_response('200 OK', [('CONTENT-TYPE', 'text/plain')])

        self.assertRaises(
            ValueError,
            resp.start_response,
            '500 Err', [('CONTENT-TYPE', 'text/plain')], ['', ValueError()])
Пример #6
0
    def test_handle_request_io(self):

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

        srv = wsgi.WSGIServerHttpProtocol(wsgi_app, 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.0 200 OK'))
        self.assertTrue(content.endswith(b'data'))
Пример #7
0
 def _make_one(self, **kw):
     srv = wsgi.WSGIServerHttpProtocol(self.wsgi, loop=self.loop, **kw)
     srv.stream = self.stream
     srv.transport = self.transport
     return srv.create_wsgi_environ(self.message, self.payload)
Пример #8
0
 def test_ctor(self):
     srv = wsgi.WSGIServerHttpProtocol(self.wsgi, loop=self.loop)
     self.assertIs(srv.wsgi, self.wsgi)
     self.assertFalse(srv.readpayload)