Beispiel #1
0
    def test_gzip_encoding(self):
        input_bytes = data_string('reddit.com.request')

        server = ICAPProtocolFactory()
        protocol = server()
        protocol.connection_made(BytesIOTransport())

        called = False

        @handler(raw=True)
        def respmod(request):
            nonlocal called
            assert len(request.http.body)
            assert '<!doctype html>' in request.http.body
            assert b'<!doctype html>' in request.http.body_bytes
            called = True

        for b in input_bytes:
            f = protocol.data_received(bytes([b]))

            if f is not None:
                asyncio.get_event_loop().run_until_complete(f)

        t = protocol.transport.getvalue()

        assert protocol.parser.sline is None
        assert not protocol.parser.headers
        assert not protocol.parser.complete()
        print(t)
        assert t
        assert t.count(b'ICAP') == 1
        assert b'<!doctype html>' not in t
        assert called
Beispiel #2
0
    def test_handle_request__options_request_no_handlers(self):
        input_bytes = data_string('options_request.request')

        s = self.run_test(ICAPProtocolFactory(), input_bytes)

        print(s)

        assert b'ICAP/1.0 404 ICAP Service Not Found' in s
        assert b'ISTag: ' in s
        assert b'Date: ' in s
        assert b'Encapsulated: ' in s
Beispiel #3
0
    def test_handle_request__string_return(self):
        input_bytes = data_string('icap_request_with_two_header_sets.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def respmod(request):
            return b"fooooooooooooooo"

        transaction = self.run_test(server, input_bytes, assert_mutated=True)

        assert b"fooooooooooooooo" in transaction
Beispiel #4
0
    def test_handle_request__handles_exceptions(self, exception):
        input_bytes = data_string('icap_request_with_two_header_sets.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def respmod(request):
            raise exception

        transaction = self.run_test(server, input_bytes)

        assert b'500 Internal Server Error' in transaction
Beispiel #5
0
    def dummy_server(self):
        server = ICAPProtocolFactory()

        @handler()
        def reqmod(request):
            pass

        @handler()
        def respmod(request):
            pass

        return server
Beispiel #6
0
    def test_handle_request__correct_length_for_binary_data(self):
        input_bytes = data_string(
            'request_with_http_request_no_payload.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def reqmod(request):
            return os.urandom(16)

        transaction = self.run_test(server, input_bytes)

        assert b"Content-Length: 16\r\n" in transaction
Beispiel #7
0
    def test_handle_request__no_content_length_on_zero_length_body(self):
        input_bytes = data_string(
            'request_with_http_request_no_payload.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def reqmod(request):
            return HTTPRequest(body=b'', headers=request.headers)

        transaction = self.run_test(server, input_bytes)

        assert b"Content-Length" not in transaction
Beispiel #8
0
    def test_poor_matching_uris_returns_405(self, is_reqmod):
        if is_reqmod:
            path = 'request_with_bad_resource.request'
        else:
            path = 'icap_request_with_two_header_sets_bad_resource.request'
        input_bytes = data_string(path)

        server = ICAPProtocolFactory()

        s = self.run_test(server, input_bytes)

        print(s)
        assert b'405 Method Not Allowed For Service' in s
Beispiel #9
0
    def test_handle_request__request_for_respmod(self):
        input_bytes = data_string('icap_request_with_two_header_sets.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def respmod(request):
            return HTTPRequest()

        transaction = self.run_test(server, input_bytes)

        assert b"500 Internal Server Error" in transaction
        assert transaction.count(
            b"This is data that was returned by an origin server") == 0
Beispiel #10
0
    def test_handle_request__response_for_reqmod(self):
        input_bytes = data_string(
            'request_with_http_request_no_payload.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def reqmod(request):
            return HTTPResponse(body=b'cool body')

        transaction = self.run_test(server, input_bytes)

        assert b"HTTP/1.1 200 OK" in transaction
        assert b"cool body" in transaction
Beispiel #11
0
    def test_handle_request__empty_return_forces_reserialization(
            self, force_204):
        input_bytes = data_string('icap_request_with_two_header_sets.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def respmod(request):
            return

        transaction = self.run_test(server, input_bytes, force_204=force_204)

        assert b'200 OK' in transaction
        assert transaction.count(b'33; lamps') == 0
        assert transaction.count(b'33\r\n') == 1
Beispiel #12
0
    def test_handle_request__no_match_204(self, force_204):
        input_bytes = data_string(
            'request_with_http_request_no_payload.request')

        server = ICAPProtocolFactory()

        @handler(lambda req: False)
        def reqmod(request):
            return

        transaction = self.run_test(server, input_bytes, force_204=force_204)

        if force_204:
            assert b"ICAP/1.0 204 No Modifications Needed" in transaction
        else:
            assert b"ICAP/1.0 200 OK" in transaction
Beispiel #13
0
    def test_handle_request__options_request_handler_with_http_specific_handler(
            self):
        input_bytes = data_string('options_request.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('google.com'))
        def respmod(request):
            pass

        s = self.run_test(server, input_bytes)

        print(s)

        assert b'ICAP/1.0 200 OK' in s
        assert b'ISTag: ' in s
        assert b'Date: ' in s
        assert b'Encapsulated: ' in s
Beispiel #14
0
    def test_handle_request__response_for_respmod(self):
        input_bytes = data_string('icap_request_with_two_header_sets.request')

        server = ICAPProtocolFactory()

        @handler(DomainCriteria('www.origin-server.com'))
        def respmod(request):
            headers = HeadersDict([
                ('Foo', 'bar'),
                ('Bar', 'baz'),
            ])
            return HTTPResponse(headers=headers, body=b"cool data")

        transaction = self.run_test(server, input_bytes)

        assert b"cool data" in transaction
        assert b"Foo: bar" in transaction
        assert b"Bar: baz" in transaction
        assert transaction.count(
            b"This is data that was returned by an origin server") == 0
Beispiel #15
0
    def test_multiple_data_received_calls(self):
        expected = HeadersDict([('Host', 'icap.example.org'),
                                ('Encapsulated',
                                 'req-hdr=0, res-hdr=137, res-body=296')])

        input_bytes = data_string('icap_request_with_two_header_sets.request')

        server = ICAPProtocolFactory()

        protocol = server()
        protocol.connection_made(BytesIOTransport())

        called = False

        @handler(raw=True)
        def respmod(request):
            nonlocal called
            assert request.is_respmod
            assert request.request_line is not None
            assert request.headers == expected
            assert len(request.http.body) == 51
            called = True

        for b in input_bytes:
            f = protocol.data_received(bytes([b]))

            if f is not None:
                asyncio.get_event_loop().run_until_complete(f)

        t = protocol.transport.getvalue()

        assert protocol.parser.sline is None
        assert not protocol.parser.headers
        assert not protocol.parser.complete()
        print(t)
        assert t
        assert t.count(b'ICAP') == 1
        assert called
Beispiel #16
0
 def test_as_factory(self):
     f = ICAPProtocolFactory()
     t = f()
     assert isinstance(t, ICAPProtocol)
     assert t.factory == f