def test_filter_by_raw_headers_response(): f1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:') fn1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:', negate=True) f2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader') fn2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader', negate=True) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Header'] = 'Sexy' assert not f1(r) assert fn1(r) assert not f2(r) assert fn2(r) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Sexy'] = 'sexy' assert f1(r) assert not fn1(r) assert not f2(r) assert fn2(r) rsp.headers['OtherHeader'] = 'sexy' rsp.headers['Header'] = 'foo' assert f1(r) assert not fn1(r) assert f2(r) assert not fn2(r)
def test_filter_by_raw_headers_response(): f1 = context.gen_filter_by_raw_headers(['ct', 'Sexy:']) fn1 = context.gen_filter_by_raw_headers(['nct', 'Sexy:']) f2 = context.gen_filter_by_raw_headers(['ct', 'sexy\r\nHeader']) fn2 = context.gen_filter_by_raw_headers(['nct', 'sexy\r\nHeader']) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Header'] = 'Sexy' assert not f1(r) assert fn1(r) assert not f2(r) assert fn2(r) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Sexy'] = 'sexy' assert f1(r) assert not fn1(r) assert not f2(r) assert fn2(r) rsp.headers['OtherHeader'] = 'sexy' rsp.headers['Header'] = 'foo' assert f1(r) assert not fn1(r) assert f2(r) assert not fn2(r)
def test_filter_by_body(): f = context.gen_filter_by_body(context.cmp_contains, 'sexy') fn = context.gen_filter_by_body(context.cmp_contains, 'sexy', negate=True) # Test request bodies r = Request() r.status_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.raw_data = 'foo' assert not f(r) assert fn(r) r.raw_data = 'sexy' assert f(r) assert not fn(r) # Test response bodies r = Request() rsp = Response() rsp.status_line = 'HTTP/1.1 200 OK' rsp.headers['sexy'] = 'sexy' r.status_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.response = rsp assert not f(r) assert fn(r) rsp.raw_data = 'sexy' assert f(r) assert not fn(r)
def test_filter_by_body(): f = context.gen_filter_by_body(['ct', 'sexy']) fn = context.gen_filter_by_body(['nct', 'sexy']) # Test request bodies r = Request() r.start_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.body = 'foo' assert not f(r) assert fn(r) r.body = 'sexy' assert f(r) assert not fn(r) # Test response bodies r = Request() rsp = Response() rsp.start_line = 'HTTP/1.1 200 OK' rsp.headers['sexy'] = 'sexy' r.start_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.response = rsp assert not f(r) assert fn(r) rsp.body = 'sexy' assert f(r) assert not fn(r)
def http_request(): req = Request('GET / HTTP/1.1\r\n\r\n') req.host = 'www.foo.faketld' req.port = '1337' req.is_ssl = True req.reqid = 123 rsp = Response('HTTP/1.1 200 OK\r\n\r\n') req.response = rsp return req
def test_gen_filter_by_set_cookies(): f1 = context.gen_filter_by_set_cookies(context.cmp_contains, 'Session') f2 = context.gen_filter_by_set_cookies(context.cmp_contains, 'Cookie', context.cmp_contains, 'CookieVal') r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' '\r\n')) r.response = rsp assert not f1(r) assert not f2(r) r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' 'Set-Cookie: Session=Banana\r\n' '\r\n')) r.response = rsp assert f1(r) assert not f2(r) r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' 'Set-Cookie: Session=Banana\r\n' 'Set-Cookie: CookieThing=NoMatch\r\n' '\r\n')) r.response = rsp assert f1(r) assert not f2(r) r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' 'Set-Cookie: Session=Banana\r\n' 'Set-Cookie: CookieThing=CookieValue\r\n' '\r\n')) r.response = rsp assert f1(r) assert f2(r)