Пример #1
0
def test_AnyOfCriteria():
    c = DomainCriteria('google.com') | RegexCriteria('.*gbogle.c[ao]m.*')

    assert c(FakeRequest('http://google.com'))
    assert c(FakeRequest('http://gbogle.cam'))
    assert c(FakeRequest('http://gbogle.com'))
    assert not c(FakeRequest('http://twitter.com'))
Пример #2
0
    def test_missing_host_header(self):
        request = MagicMock()

        def get(key, default):
            return default

        request.http.headers.get = get

        r = DomainCriteria('google.com')
        assert not r(request)
Пример #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
Пример #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
Пример #5
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
Пример #6
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
Пример #7
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
Пример #8
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
Пример #9
0
def test_sort_handlers():
    _HANDLERS.clear()

    _HANDLERS['foo'] = [
        (DomainCriteria('google.com'), None, None),
        (RegexCriteria('.*'), None, None),
    ]

    sort_handlers()

    first, second = _HANDLERS['foo']

    assert isinstance(first[0], RegexCriteria)
    assert isinstance(second[0], DomainCriteria)
Пример #10
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
Пример #11
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
Пример #12
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
Пример #13
0
from lxml import html

from icap import run, handler, DomainCriteria, ContentTypeCriteria


@handler(DomainCriteria('twitter.com') & ContentTypeCriteria('text/html'))
class Twitter:
    def respmod(request):
        doc = html.document_fromstring(request.body)

        for promoted_tweet in doc.cssselect('.promoted-tweet'):
            promoted_tweet.drop_tree()

        return html.tostring(doc)


if __name__ == '__main__':
    run()
Пример #14
0
"""
Example ICAP server that implements YouTube for Schools (http://www.youtube.com/schools)
"""
from icap import run, handler, DomainCriteria

# You must enter your school's YouTube for Schools ID here for this to work.
EDUCATION_ID = ''


@handler(DomainCriteria('youtube.com'))
class YouTubeForSchools:
    def reqmod(self, request):
        request.headers['X-YouTube-Edu-Filter'] = EDUCATION_ID


if __name__ == '__main__':
    run()
Пример #15
0
 def test_single_char(self):
     r = DomainCriteria('go?gle.com')
     assert r(FakeRequest('http://google.com'))
     assert r(FakeRequest('http://goggle.com'))
     assert not r(FakeRequest('http://giggle.com'))
Пример #16
0
 def test_both_glob(self):
     r = DomainCriteria('*google.com*')
     assert r(FakeRequest('http://google.com'))
     assert r(FakeRequest('http://sub.google.com'))
     assert r(FakeRequest('http://google.com.au'))
     assert not r(FakeRequest('http://googleg.com.au'))
Пример #17
0
    def test_normal_domain(self):
        r = DomainCriteria('google.com')

        assert r(FakeRequest('http://google.com'))
        assert not r(FakeRequest('http://google.com.au'))
Пример #18
0
def test_AllOfCriteria():
    c = DomainCriteria('google.com') & RegexCriteria('https://')

    assert not c(FakeRequest('http://google.com'))
    assert c(FakeRequest('https://google.com'))
Пример #19
0
#!/usr/bin/env python3

from icap import DomainCriteria, handler, run


@handler(DomainCriteria('*google.*'))
def reqmod(request):
    query = request.request_line.query

    if 'q' in query:
        request.request_line.query['q'][0] += ' without my pants'


if __name__ == '__main__':
    run(host='127.0.0.1', port=1334)