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'))
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)
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
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
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
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
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
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
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)
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
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
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
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()
""" 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()
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'))
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'))
def test_normal_domain(self): r = DomainCriteria('google.com') assert r(FakeRequest('http://google.com')) assert not r(FakeRequest('http://google.com.au'))
def test_AllOfCriteria(): c = DomainCriteria('google.com') & RegexCriteria('https://') assert not c(FakeRequest('http://google.com')) assert c(FakeRequest('https://google.com'))
#!/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)