コード例 #1
0
 def test_connection_reuse(self):
     connection = Connection('localhost', self._port)
     request = Request.new(self.get_url('/'))
     request.version = 'HTTP/1.0'
     response = yield connection.fetch(request)
     self.assertEqual(200, response.status_code)
     response = yield connection.fetch(request)
     self.assertEqual(200, response.status_code)
コード例 #2
0
 def test_connection_refused(self):
     connection = Connection(('localhost', 1))
     try:
         yield connection.fetch(Request.new('http://localhost:1/'))
     except ConnectionRefused:
         pass
     else:
         self.fail()
コード例 #3
0
 def test_ssl_fail(self):
     connection = Connection('localhost', self.get_http_port())
     try:
         yield connection.fetch(Request.new(self.get_url('/')))
     except SSLVerficationError:
         pass
     else:
         self.fail()
コード例 #4
0
 def test_connection_timeout(self):
     connection = Connection('1.0.0.0', 1, connect_timeout=0.1)
     try:
         yield connection.fetch(
             Request.new('http://1.0.0.0:1/'))
     except NetworkError:
         pass
     else:
         self.fail()
コード例 #5
0
 def test_read_timeout(self):
     connection = Connection('localhost', self._port, read_timeout=0.1)
     request = Request.new(self.get_url('/sleep_long'))
     try:
         yield connection.fetch(request)
     except NetworkError:
         pass
     else:
         self.fail()
コード例 #6
0
 def setUp(self):
     super().setUp()
     self.http_server = Server()
     self.http_server.start()
     self.http_server.started_event.wait(timeout=5.0)
     self._port = self.http_server.port
     self.connection = Connection(
         ('localhost', self._port),
         params=ConnectionParams(connect_timeout=2.0, read_timeout=60.0))
コード例 #7
0
 def test_no_such_host(self):
     connection = Connection('wpull-no-exist.invalid', 80)
     try:
         yield connection.fetch(
             Request.new('http://wpull-no-exist.invalid'))
     except NetworkError:
         pass
     else:
         self.fail()
コード例 #8
0
ファイル: http_test.py プロジェクト: DanielOaks/wpull
 def test_connection_refused(self):
     connection = Connection('localhost', 1)
     try:
         yield connection.fetch(
             Request.new('http://localhost:1/'))
     except ConnectionRefused:
         pass
     else:
         self.fail()
コード例 #9
0
 def test_buffer_overflow(self):
     connection = Connection('localhost', self._port,
         connect_timeout=2.0, read_timeout=5.0, buffer_size=1000)
     request = Request.new(self.get_url('/buffer_overflow'))
     try:
         yield connection.fetch(request)
     except (ProtocolError, NetworkError):
         pass
     else:
         self.fail()
コード例 #10
0
    def test_ignore_length(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(keep_alive=False, ignore_length=True))

        response = yield self.connection.fetch(Request.new(
            self.get_url('/underrun')),
                                               recorder=DebugPrintRecorder())

        self.assertEqual(50, response.body.content_size)
コード例 #11
0
 def test_ssl_fail(self):
     connection = Connection(
         ('localhost', self.get_http_port()),
         ssl_enable=True,
         params=ConnectionParams(
             ssl_options=dict(cert_reqs=ssl.CERT_REQUIRED,
                              ca_certs=self.get_ssl_options()['certfile'])))
     try:
         yield connection.fetch(Request.new(self.get_url('/')))
     except SSLVerficationError:
         pass
     else:
         self.fail()
コード例 #12
0
    def test_underrun(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(connect_timeout=2.0, read_timeout=1.0))

        for counter in range(3):
            print(counter)
            try:
                yield self.connection.fetch(
                    Request.new(self.get_url('/underrun')))
            except NetworkTimedOut:
                pass
            else:
                self.fail()
コード例 #13
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
        def mock_func():
            @tornado.gen.coroutine
            def mock_connect(*dummy, **dummy1):
                raise ssl.SSLError(123, 'Mock error')

            yield Connection._make_socket(self.connection)
            self.connection._io_stream.connect = mock_connect
コード例 #14
0
ファイル: badapp.py プロジェクト: lowks/wpull
class BadAppTestCase(AsyncTestCase):
    def setUp(self):
        super().setUp()
        self.http_server = Server()
        self.http_server.start()
        self.http_server.started_event.wait(timeout=5.0)
        self._port = self.http_server.port
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(connect_timeout=2.0, read_timeout=60.0)
        )

    @tornado.gen.coroutine
    def fetch(self, path):
        response = yield self.connection.fetch(Request.new(self.get_url(path)),
                                               DebugPrintRecorder())
        raise tornado.gen.Return(response)

    def get_http_port(self):
        return self._port

    def get_protocol(self):
        return 'http'

    def get_url(self, path):
        # from tornado.testing
        return '%s://localhost:%s%s' % (self.get_protocol(),
                                        self.get_http_port(), path)

    def tearDown(self):
        self.http_server.stop()
        self.http_server.join(timeout=5)
        super().tearDown()
コード例 #15
0
class BadAppTestCase(AsyncTestCase):
    def setUp(self):
        super().setUp()
        self.http_server = Server()
        self.http_server.start()
        self.http_server.started_event.wait(timeout=5.0)
        self._port = self.http_server.port
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(connect_timeout=2.0, read_timeout=60.0))

    @tornado.gen.coroutine
    def fetch(self, path):
        response = yield self.connection.fetch(Request.new(self.get_url(path)),
                                               DebugPrintRecorder())
        raise tornado.gen.Return(response)

    def get_http_port(self):
        return self._port

    def get_protocol(self):
        return 'http'

    def get_url(self, path):
        # from tornado.testing
        return '%s://localhost:%s%s' % (self.get_protocol(),
                                        self.get_http_port(), path)

    def tearDown(self):
        self.http_server.stop()
        self.http_server.join(timeout=5)
        super().tearDown()
コード例 #16
0
        def mock_func():
            @tornado.gen.coroutine
            def mock_connect(*dummy, **dummy1):
                raise ssl.SSLError(123, 'Mock error')

            yield Connection._make_socket(self.connection)
            self.connection._io_stream.connect = mock_connect
コード例 #17
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
 def test_ssl_fail(self):
     connection = Connection(
         ('localhost', self.get_http_port()),
         ssl_enable=True,
         params=ConnectionParams(
             ssl_options=dict(
                 cert_reqs=ssl.CERT_REQUIRED,
                 ca_certs=self.get_ssl_options()['certfile']
             )
         )
     )
     try:
         yield connection.fetch(Request.new(self.get_url('/')))
     except SSLVerficationError:
         pass
     else:
         self.fail()
コード例 #18
0
ファイル: badapp.py プロジェクト: DanielOaks/wpull
 def setUp(self):
     super().setUp()
     self.http_server = Server()
     self.http_server.start()
     self.http_server.started_event.wait(timeout=5.0)
     self._port = self.http_server.port
     self.connection = Connection('localhost', self._port,
         connect_timeout=2.0, read_timeout=5.0)
コード例 #19
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
    def test_ignore_length(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(keep_alive=False, ignore_length=True)
        )

        response = yield self.connection.fetch(
            Request.new(self.get_url('/underrun')),
            recorder=DebugPrintRecorder()
        )

        self.assertEqual(50, response.body.content_size)
コード例 #20
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
    def test_underrun(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(connect_timeout=2.0, read_timeout=1.0)
        )

        for counter in range(3):
            print(counter)
            try:
                yield self.connection.fetch(
                    Request.new(self.get_url('/underrun'))
                )
            except NetworkTimedOut:
                pass
            else:
                self.fail()
コード例 #21
0
 def test_connection_should_close(self):
     self.assertTrue(Connection.should_close('HTTP/1.0', None))
     self.assertTrue(Connection.should_close('HTTP/1.0', 'wolf'))
     self.assertTrue(Connection.should_close('HTTP/1.0', 'close'))
     self.assertTrue(Connection.should_close('HTTP/1.0', 'ClOse'))
     self.assertFalse(Connection.should_close('HTTP/1.0', 'keep-Alive'))
     self.assertFalse(Connection.should_close('HTTP/1.0', 'keepalive'))
     self.assertTrue(Connection.should_close('HTTP/1.1', 'close'))
     self.assertTrue(Connection.should_close('HTTP/1.1', 'ClOse'))
     self.assertFalse(Connection.should_close('HTTP/1.1', 'dragons'))
     self.assertFalse(Connection.should_close('HTTP/1.1', 'keep-alive'))
     self.assertTrue(Connection.should_close('HTTP/1.2', 'close'))
コード例 #22
0
 def test_ssl_no_check(self):
     connection = Connection(('localhost', self.get_http_port()),
                             ssl_enable=True)
     yield connection.fetch(Request.new(self.get_url('/')))
コード例 #23
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
class TestConnection(BadAppTestCase):
    def setUp(self):
        super().setUp()
        tornado.ioloop.IOLoop.current().set_blocking_log_threshold(0.5)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_no_such_host(self):
        connection = Connection(('wpull-no-exist.invalid', 80))
        try:
            yield connection.fetch(
                Request.new('http://wpull-no-exist.invalid'))
        except NetworkError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_refused(self):
        connection = Connection(('localhost', 1))
        try:
            yield connection.fetch(
                Request.new('http://localhost:1/'))
        except ConnectionRefused:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_timeout(self):
        connection = Connection(
            ('1.0.0.0', 1),
            params=ConnectionParams(connect_timeout=0.1)
        )

        try:
            yield connection.fetch(
                Request.new('http://1.0.0.0:1/'))
        except NetworkError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_reuse(self):
        connection = Connection(('localhost', self._port))
        request = Request.new(self.get_url('/'))
        request.version = 'HTTP/1.0'
        response = yield connection.fetch(request)
        self.assertEqual(200, response.status_code)
        response = yield connection.fetch(request)
        self.assertEqual(200, response.status_code)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_reuse_with_http_close(self):
        for dummy in range(5):
            response = yield self.fetch('/content_length_with_close')
            self.assertEqual(200, response.status_code)
            self.assertEqual('100', response.fields['Content-Length'])
            self.assertEqual(b'a' * 100, response.body.content)
            self.assertEqual(100, response.body.content_size)

    @unittest.skip("This case is too difficult to solve.")
    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_reuse_without_http_close(self):
        for dummy in range(5):
            response = yield self.fetch('/content_length_without_close')
            self.assertEqual(200, response.status_code)
            self.assertEqual('100', response.fields['Content-Length'])
            self.assertEqual(b'a' * 100, response.body.content)
            self.assertEqual(100, response.body.content_size)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_read_timeout(self):
        connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(read_timeout=0.1)
        )
        request = Request.new(self.get_url('/sleep_long'))
        try:
            yield connection.fetch(request)
        except NetworkError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic(self):
        response = yield self.fetch('/')
        self.assertEqual(200, response.status_code)
        self.assertEqual(b'hello world!', response.body.content)
        self.assertTrue(response.url_info)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_content_length(self):
        response = yield self.fetch('/content_length')
        self.assertEqual(200, response.status_code)
        self.assertEqual('100', response.fields['Content-Length'])
        self.assertEqual(b'a' * 100, response.body.content)
        self.assertEqual(100, response.body.content_size)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_chunked(self):
        response = yield self.fetch('/chunked')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_chunked_trailer(self):
        response = yield self.fetch('/chunked_trailer')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual('dolphin', response.fields['Animal'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_chunked_trailer_2(self):
        response = yield self.fetch('/chunked_trailer_2')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual('dolphin', response.fields['Animal'])
        self.assertEqual('delicious', response.fields['Cake'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_malformed_chunked(self):
        try:
            yield self.fetch('/malformed_chunked')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_non_standard_delim_chunked(self):
        response = yield self.fetch('/chunked_non_standard_delim')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_chunked_with_extension(self):
        response = yield self.fetch('/chunked_with_extension')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_buffer_overflow(self):
        connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(
                connect_timeout=2.0, read_timeout=5.0, buffer_size=1000
            )
        )
        request = Request.new(self.get_url('/buffer_overflow'))
        try:
            yield connection.fetch(request)
        except (ProtocolError, NetworkError):
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_bad_chunk_size(self):
        try:
            yield self.fetch('/bad_chunk_size')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_content_length_and_chunked(self):
        response = yield self.fetch('/content_length_and_chunked')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_bad_header_delminators(self):
        response = yield self.fetch('/bad_header_deliminators')
        self.assertEqual(200, response.status_code)
        self.assertEqual(b'hi\n', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_utf8_header(self):
        response = yield self.fetch('/utf8_header')
        self.assertEqual(200, response.status_code)
        self.assertEqual('🐱', response.fields['whoa'])

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_short_close(self):
        try:
            yield self.fetch('/short_close')
        except NetworkError:
            pass
        else:
            self.fail()

        yield self.fetch('/')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_header_early_close(self):
        try:
            yield self.fetch('/header_early_close')
        except NetworkError:
            pass
        else:
            self.fail()

        yield self.fetch('/')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_unclean_8bit_header(self):
        yield self.fetch('/unclean_8bit_header')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_no_colon_header(self):
        yield self.fetch('/no_colon_header')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_malformed_content_length(self):
        yield self.fetch('/malformed_content_length')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_negative_content_length(self):
        yield self.fetch('/negative_content_length')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_gzip_encoding(self):
        filename = os.path.join(
            os.path.dirname(__file__),
            '..', 'testing', 'samples', 'xkcd_1.html'
        )

        with open(filename, 'rb') as in_file:
            test_data = in_file.read()

        paths = ['/gzip_http_1_0', '/gzip_http_1_1', '/gzip_chunked']
        for path in paths:
            print('Fetching', path)
            response = yield self.fetch(path)

            self.assertEqual(len(test_data), len(response.body.content))
            self.assertEqual(test_data, response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_zlib_encoding(self):
        filename = os.path.join(
            os.path.dirname(__file__),
            '..', 'testing', 'samples', 'xkcd_1.html'
        )

        with open(filename, 'rb') as in_file:
            test_data = in_file.read()

        paths = [
            '/zlib_http_1_0', '/raw_deflate_http_1_0',
            '/zlib_chunked', '/raw_deflate_chunked',
        ]
        for path in paths:
            print('Fetching', path)
            response = yield self.fetch(path)

            self.assertEqual(len(test_data), len(response.body.content))
            self.assertEqual(test_data, response.body.content)

    @unittest.skip('zlib seems to not error on short content')
    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_gzip_corrupt_short(self):
        try:
            yield self.fetch('/gzip_corrupt_short')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_gzip_corrupt_footer(self):
        try:
            yield self.fetch('/gzip_corrupt_footer')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_no_content(self):
        yield self.fetch('/no_content')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_head_no_content(self):
        yield self.connection.fetch(
            Request.new(self.get_url('/no_content'), method='HEAD'),
            DebugPrintRecorder()
        )

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_big(self):
        yield self.connection.fetch(
            Request.new(self.get_url('/big')),
        )

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_underrun(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(connect_timeout=2.0, read_timeout=1.0)
        )

        for counter in range(3):
            print(counter)
            try:
                yield self.connection.fetch(
                    Request.new(self.get_url('/underrun'))
                )
            except NetworkTimedOut:
                pass
            else:
                self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_overrun(self):
        for dummy in range(3):
            try:
                yield self.fetch('/overrun')
            except ProtocolError:
                pass

        self.connection.close()
        yield self.fetch('/')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_connect_socket_error(self):
        @tornado.gen.coroutine
        def mock_func():
            @tornado.gen.coroutine
            def mock_connect(*dummy, **dummy1):
                raise socket.error(123, 'Mock error')

            yield Connection._make_socket(self.connection)
            self.connection._io_stream.connect = mock_connect

        self.connection._make_socket = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_connect_ssl_error(self):
        @tornado.gen.coroutine
        def mock_func():
            @tornado.gen.coroutine
            def mock_connect(*dummy, **dummy1):
                raise ssl.SSLError(123, 'Mock error')

            yield Connection._make_socket(self.connection)
            self.connection._io_stream.connect = mock_connect

        self.connection._make_socket = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_request_socket_error(self):
        @tornado.gen.coroutine
        def mock_func(dummy):
            if sys.version_info < (3, 3):
                raise socket.error(123, 'Mock error')
            else:
                raise ConnectionError(123, 'Mock error')

        self.connection._read_response_header = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_request_ssl_error(self):
        @tornado.gen.coroutine
        def mock_func(dummy):
            if sys.version_info < (3, 3):
                raise socket.error(123, 'Mock error')
            else:
                raise ConnectionError(123, 'Mock error')

        self.connection._read_response_header = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_ignore_length(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(keep_alive=False, ignore_length=True)
        )

        response = yield self.connection.fetch(
            Request.new(self.get_url('/underrun')),
            recorder=DebugPrintRecorder()
        )

        self.assertEqual(50, response.body.content_size)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_false_gzip(self):
        response = yield self.fetch('/false_gzip')

        self.assertEqual('gzip', response.fields['Content-Encoding'])
        self.assertEqual(b'a' * 100, response.body.content)
コード例 #24
0
class TestConnection(BadAppTestCase):
    def setUp(self):
        super().setUp()
        tornado.ioloop.IOLoop.current().set_blocking_log_threshold(0.5)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_no_such_host(self):
        connection = Connection(('wpull-no-exist.invalid', 80))
        try:
            yield connection.fetch(
                Request.new('http://wpull-no-exist.invalid'))
        except NetworkError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_refused(self):
        connection = Connection(('localhost', 1))
        try:
            yield connection.fetch(Request.new('http://localhost:1/'))
        except ConnectionRefused:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_timeout(self):
        connection = Connection(('1.0.0.0', 1),
                                params=ConnectionParams(connect_timeout=0.1))

        try:
            yield connection.fetch(Request.new('http://1.0.0.0:1/'))
        except NetworkError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_reuse(self):
        connection = Connection(('localhost', self._port))
        request = Request.new(self.get_url('/'))
        request.version = 'HTTP/1.0'
        response = yield connection.fetch(request)
        self.assertEqual(200, response.status_code)
        response = yield connection.fetch(request)
        self.assertEqual(200, response.status_code)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_reuse_with_http_close(self):
        for dummy in range(5):
            response = yield self.fetch('/content_length_with_close')
            self.assertEqual(200, response.status_code)
            self.assertEqual('100', response.fields['Content-Length'])
            self.assertEqual(b'a' * 100, response.body.content)
            self.assertEqual(100, response.body.content_size)

    @unittest.skip("This case is too difficult to solve.")
    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_connection_reuse_without_http_close(self):
        for dummy in range(5):
            response = yield self.fetch('/content_length_without_close')
            self.assertEqual(200, response.status_code)
            self.assertEqual('100', response.fields['Content-Length'])
            self.assertEqual(b'a' * 100, response.body.content)
            self.assertEqual(100, response.body.content_size)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_read_timeout(self):
        connection = Connection(('localhost', self._port),
                                params=ConnectionParams(read_timeout=0.1))
        request = Request.new(self.get_url('/sleep_long'))
        try:
            yield connection.fetch(request)
        except NetworkError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic(self):
        response = yield self.fetch('/')
        self.assertEqual(200, response.status_code)
        self.assertEqual(b'hello world!', response.body.content)
        self.assertTrue(response.url_info)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_content_length(self):
        response = yield self.fetch('/content_length')
        self.assertEqual(200, response.status_code)
        self.assertEqual('100', response.fields['Content-Length'])
        self.assertEqual(b'a' * 100, response.body.content)
        self.assertEqual(100, response.body.content_size)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_chunked(self):
        response = yield self.fetch('/chunked')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_chunked_trailer(self):
        response = yield self.fetch('/chunked_trailer')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual('dolphin', response.fields['Animal'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_basic_chunked_trailer_2(self):
        response = yield self.fetch('/chunked_trailer_2')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual('dolphin', response.fields['Animal'])
        self.assertEqual('delicious', response.fields['Cake'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_malformed_chunked(self):
        try:
            yield self.fetch('/malformed_chunked')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_non_standard_delim_chunked(self):
        response = yield self.fetch('/chunked_non_standard_delim')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_chunked_with_extension(self):
        response = yield self.fetch('/chunked_with_extension')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_buffer_overflow(self):
        connection = Connection(('localhost', self._port),
                                params=ConnectionParams(connect_timeout=2.0,
                                                        read_timeout=5.0,
                                                        buffer_size=1000))
        request = Request.new(self.get_url('/buffer_overflow'))
        try:
            yield connection.fetch(request)
        except (ProtocolError, NetworkError):
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_bad_chunk_size(self):
        try:
            yield self.fetch('/bad_chunk_size')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_content_length_and_chunked(self):
        response = yield self.fetch('/content_length_and_chunked')
        self.assertEqual(200, response.status_code)
        self.assertEqual('chunked', response.fields['Transfer-Encoding'])
        self.assertEqual(b'hello world!', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_bad_header_delminators(self):
        response = yield self.fetch('/bad_header_deliminators')
        self.assertEqual(200, response.status_code)
        self.assertEqual(b'hi\n', response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_utf8_header(self):
        response = yield self.fetch('/utf8_header')
        self.assertEqual(200, response.status_code)
        self.assertEqual('🐱', response.fields['whoa'])

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_short_close(self):
        try:
            yield self.fetch('/short_close')
        except NetworkError:
            pass
        else:
            self.fail()

        yield self.fetch('/')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_header_early_close(self):
        try:
            yield self.fetch('/header_early_close')
        except NetworkError:
            pass
        else:
            self.fail()

        yield self.fetch('/')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_unclean_8bit_header(self):
        yield self.fetch('/unclean_8bit_header')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_no_colon_header(self):
        yield self.fetch('/no_colon_header')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_malformed_content_length(self):
        yield self.fetch('/malformed_content_length')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_negative_content_length(self):
        yield self.fetch('/negative_content_length')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_gzip_encoding(self):
        filename = os.path.join(os.path.dirname(__file__), '..', 'testing',
                                'samples', 'xkcd_1.html')

        with open(filename, 'rb') as in_file:
            test_data = in_file.read()

        paths = ['/gzip_http_1_0', '/gzip_http_1_1', '/gzip_chunked']
        for path in paths:
            print('Fetching', path)
            response = yield self.fetch(path)

            self.assertEqual(len(test_data), len(response.body.content))
            self.assertEqual(test_data, response.body.content)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_zlib_encoding(self):
        filename = os.path.join(os.path.dirname(__file__), '..', 'testing',
                                'samples', 'xkcd_1.html')

        with open(filename, 'rb') as in_file:
            test_data = in_file.read()

        paths = [
            '/zlib_http_1_0',
            '/raw_deflate_http_1_0',
            '/zlib_chunked',
            '/raw_deflate_chunked',
        ]
        for path in paths:
            print('Fetching', path)
            response = yield self.fetch(path)

            self.assertEqual(len(test_data), len(response.body.content))
            self.assertEqual(test_data, response.body.content)

    @unittest.skip('zlib seems to not error on short content')
    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_gzip_corrupt_short(self):
        try:
            yield self.fetch('/gzip_corrupt_short')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_gzip_corrupt_footer(self):
        try:
            yield self.fetch('/gzip_corrupt_footer')
        except ProtocolError:
            pass
        else:
            self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_no_content(self):
        yield self.fetch('/no_content')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_head_no_content(self):
        yield self.connection.fetch(
            Request.new(self.get_url('/no_content'), method='HEAD'),
            DebugPrintRecorder())

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_big(self):
        yield self.connection.fetch(Request.new(self.get_url('/big')), )

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_underrun(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(connect_timeout=2.0, read_timeout=1.0))

        for counter in range(3):
            print(counter)
            try:
                yield self.connection.fetch(
                    Request.new(self.get_url('/underrun')))
            except NetworkTimedOut:
                pass
            else:
                self.fail()

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_overrun(self):
        for dummy in range(3):
            try:
                yield self.fetch('/overrun')
            except ProtocolError:
                pass

        self.connection.close()
        yield self.fetch('/')

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_connect_socket_error(self):
        @tornado.gen.coroutine
        def mock_func():
            @tornado.gen.coroutine
            def mock_connect(*dummy, **dummy1):
                raise socket.error(123, 'Mock error')

            yield Connection._make_socket(self.connection)
            self.connection._io_stream.connect = mock_connect

        self.connection._make_socket = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_connect_ssl_error(self):
        @tornado.gen.coroutine
        def mock_func():
            @tornado.gen.coroutine
            def mock_connect(*dummy, **dummy1):
                raise ssl.SSLError(123, 'Mock error')

            yield Connection._make_socket(self.connection)
            self.connection._io_stream.connect = mock_connect

        self.connection._make_socket = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_request_socket_error(self):
        @tornado.gen.coroutine
        def mock_func(dummy):
            if sys.version_info < (3, 3):
                raise socket.error(123, 'Mock error')
            else:
                raise ConnectionError(123, 'Mock error')

        self.connection._read_response_header = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_mock_request_ssl_error(self):
        @tornado.gen.coroutine
        def mock_func(dummy):
            if sys.version_info < (3, 3):
                raise socket.error(123, 'Mock error')
            else:
                raise ConnectionError(123, 'Mock error')

        self.connection._read_response_header = mock_func

        try:
            yield self.fetch('/')
        except NetworkError:
            pass

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_ignore_length(self):
        self.connection = Connection(
            ('localhost', self._port),
            params=ConnectionParams(keep_alive=False, ignore_length=True))

        response = yield self.connection.fetch(Request.new(
            self.get_url('/underrun')),
                                               recorder=DebugPrintRecorder())

        self.assertEqual(50, response.body.content_size)

    @tornado.testing.gen_test(timeout=DEFAULT_TIMEOUT)
    def test_false_gzip(self):
        response = yield self.fetch('/false_gzip')

        self.assertEqual('gzip', response.fields['Content-Encoding'])
        self.assertEqual(b'a' * 100, response.body.content)
コード例 #25
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
 def test_connection_should_close(self):
     self.assertTrue(Connection.should_close('HTTP/1.0', None))
     self.assertTrue(Connection.should_close('HTTP/1.0', 'wolf'))
     self.assertTrue(Connection.should_close('HTTP/1.0', 'close'))
     self.assertTrue(Connection.should_close('HTTP/1.0', 'ClOse'))
     self.assertFalse(Connection.should_close('HTTP/1.0', 'keep-Alive'))
     self.assertFalse(Connection.should_close('HTTP/1.0', 'keepalive'))
     self.assertTrue(Connection.should_close('HTTP/1.1', 'close'))
     self.assertTrue(Connection.should_close('HTTP/1.1', 'ClOse'))
     self.assertFalse(Connection.should_close('HTTP/1.1', 'dragons'))
     self.assertFalse(Connection.should_close('HTTP/1.1', 'keep-alive'))
     self.assertTrue(Connection.should_close('HTTP/1.2', 'close'))
コード例 #26
0
ファイル: http_test.py プロジェクト: imshashank/data-mining
 def test_ssl_no_check(self):
     connection = Connection(
         ('localhost', self.get_http_port()), ssl_enable=True
     )
     yield connection.fetch(Request.new(self.get_url('/')))