2012-07-08 22:18:37 +00:00
|
|
|
import Queue, time, os.path
|
2011-02-16 01:33:04 +00:00
|
|
|
from cStringIO import StringIO
|
2011-08-03 10:38:23 +00:00
|
|
|
import email.utils
|
2012-04-10 22:10:53 +00:00
|
|
|
from libmproxy import filt, flow, controller, utils, tnetstring
|
2011-03-05 02:58:48 +00:00
|
|
|
import tutils
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2011-02-20 19:47:19 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestStickyCookieState:
|
2011-02-24 02:15:51 +00:00
|
|
|
def _response(self, cookie, host):
|
|
|
|
s = flow.StickyCookieState(filt.parse(".*"))
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow_full()
|
2012-02-09 03:40:31 +00:00
|
|
|
f.request.host = host
|
2011-02-24 02:15:51 +00:00
|
|
|
f.response.headers["Set-Cookie"] = [cookie]
|
|
|
|
s.handle_response(f)
|
|
|
|
return s, f
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
def test_domain_match(self):
|
|
|
|
s = flow.StickyCookieState(filt.parse(".*"))
|
|
|
|
assert s.domain_match("www.google.com", ".google.com")
|
|
|
|
assert s.domain_match("google.com", ".google.com")
|
|
|
|
|
2011-02-24 02:15:51 +00:00
|
|
|
def test_handle_response(self):
|
|
|
|
c = "SSID=mooo, FOO=bar; Domain=.google.com; Path=/; "\
|
|
|
|
"Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; "
|
|
|
|
|
|
|
|
s, f = self._response(c, "host")
|
|
|
|
assert not s.jar.keys()
|
|
|
|
|
|
|
|
s, f = self._response(c, "www.google.com")
|
|
|
|
assert s.jar.keys()
|
|
|
|
|
|
|
|
s, f = self._response("SSID=mooo", "www.google.com")
|
|
|
|
assert s.jar.keys()[0] == ('www.google.com', 80, '/')
|
|
|
|
|
|
|
|
def test_handle_request(self):
|
|
|
|
s, f = self._response("SSID=mooo", "www.google.com")
|
|
|
|
assert "cookie" not in f.request.headers
|
|
|
|
s.handle_request(f)
|
|
|
|
assert "cookie" in f.request.headers
|
|
|
|
|
2011-02-23 21:33:39 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestStickyAuthState:
|
2011-03-20 04:31:54 +00:00
|
|
|
def test_handle_response(self):
|
|
|
|
s = flow.StickyAuthState(filt.parse(".*"))
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.request.headers["authorization"] = ["foo"]
|
|
|
|
s.handle_request(f)
|
|
|
|
assert "host" in s.hosts
|
|
|
|
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
s.handle_request(f)
|
|
|
|
assert f.request.headers["authorization"] == ["foo"]
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestClientPlaybackState:
|
2011-03-04 00:08:43 +00:00
|
|
|
def test_tick(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
first = tutils.tflow()
|
2011-03-04 00:08:43 +00:00
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2011-03-06 03:54:49 +00:00
|
|
|
fm.start_client_playback([first, tutils.tflow()], True)
|
|
|
|
c = fm.client_playback
|
|
|
|
|
|
|
|
assert not c.done()
|
2011-03-06 03:11:45 +00:00
|
|
|
assert not s.flow_count()
|
2011-03-04 00:08:43 +00:00
|
|
|
assert c.count() == 2
|
|
|
|
c.tick(fm, testing=True)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert s.flow_count()
|
2011-03-04 00:08:43 +00:00
|
|
|
assert c.count() == 1
|
|
|
|
|
|
|
|
c.tick(fm, testing=True)
|
|
|
|
assert c.count() == 1
|
|
|
|
|
2011-03-05 22:21:31 +00:00
|
|
|
c.clear(c.current)
|
2011-03-04 00:08:43 +00:00
|
|
|
c.tick(fm, testing=True)
|
|
|
|
assert c.count() == 0
|
2011-03-06 03:54:49 +00:00
|
|
|
c.clear(c.current)
|
|
|
|
assert c.done()
|
2011-03-04 00:08:43 +00:00
|
|
|
|
2011-03-06 03:54:49 +00:00
|
|
|
q = Queue.Queue()
|
|
|
|
fm.state.clear()
|
|
|
|
fm.tick(q)
|
2011-03-04 00:08:43 +00:00
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
fm.stop_client_playback()
|
|
|
|
assert not fm.client_playback
|
|
|
|
|
2011-02-23 21:33:39 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestServerPlaybackState:
|
2011-02-20 19:47:19 +00:00
|
|
|
def test_hash(self):
|
2012-03-05 11:40:18 +00:00
|
|
|
s = flow.ServerPlaybackState(None, [], False, False)
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tflow()
|
|
|
|
r2 = tutils.tflow()
|
2011-02-20 19:47:19 +00:00
|
|
|
|
|
|
|
assert s._hash(r)
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r.request.headers["foo"] = ["bar"]
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r.request.path = "voing"
|
|
|
|
assert s._hash(r) != s._hash(r2)
|
|
|
|
|
2011-02-22 21:54:51 +00:00
|
|
|
def test_headers(self):
|
2012-03-05 11:40:18 +00:00
|
|
|
s = flow.ServerPlaybackState(["foo"], [], False, False)
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tflow_full()
|
2011-02-22 21:54:51 +00:00
|
|
|
r.request.headers["foo"] = ["bar"]
|
2011-03-05 02:58:48 +00:00
|
|
|
r2 = tutils.tflow_full()
|
2011-02-22 21:54:51 +00:00
|
|
|
assert not s._hash(r) == s._hash(r2)
|
|
|
|
r2.request.headers["foo"] = ["bar"]
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r2.request.headers["oink"] = ["bar"]
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tflow_full()
|
|
|
|
r2 = tutils.tflow_full()
|
2011-02-22 21:54:51 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
|
2011-02-20 19:47:19 +00:00
|
|
|
def test_load(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tflow_full()
|
2011-02-20 19:47:19 +00:00
|
|
|
r.request.headers["key"] = ["one"]
|
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
r2 = tutils.tflow_full()
|
2011-02-20 19:47:19 +00:00
|
|
|
r2.request.headers["key"] = ["two"]
|
|
|
|
|
2012-03-05 11:40:18 +00:00
|
|
|
s = flow.ServerPlaybackState(None, [r, r2], False, False)
|
2011-02-20 22:40:49 +00:00
|
|
|
assert s.count() == 2
|
2011-02-20 19:47:19 +00:00
|
|
|
assert len(s.fmap.keys()) == 1
|
|
|
|
|
|
|
|
n = s.next_flow(r)
|
|
|
|
assert n.request.headers["key"] == ["one"]
|
2011-02-20 22:40:49 +00:00
|
|
|
assert s.count() == 1
|
2011-02-20 19:47:19 +00:00
|
|
|
|
|
|
|
n = s.next_flow(r)
|
|
|
|
assert n.request.headers["key"] == ["two"]
|
2011-02-20 22:40:49 +00:00
|
|
|
assert s.count() == 0
|
2011-02-20 19:47:19 +00:00
|
|
|
|
|
|
|
assert not s.next_flow(r)
|
|
|
|
|
2012-03-05 11:57:57 +00:00
|
|
|
def test_load_with_nopop(self):
|
|
|
|
r = tutils.tflow_full()
|
|
|
|
r.request.headers["key"] = ["one"]
|
|
|
|
|
|
|
|
r2 = tutils.tflow_full()
|
|
|
|
r2.request.headers["key"] = ["two"]
|
|
|
|
|
|
|
|
s = flow.ServerPlaybackState(None, [r, r2], False, True)
|
|
|
|
|
|
|
|
assert s.count() == 2
|
2012-07-15 20:42:59 +00:00
|
|
|
s.next_flow(r)
|
2012-03-05 11:57:57 +00:00
|
|
|
assert s.count() == 2
|
2011-02-20 19:47:19 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
|
|
|
|
class TestFlow:
|
2012-02-18 10:56:40 +00:00
|
|
|
def test_copy(self):
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f2 = f.copy()
|
|
|
|
assert not f is f2
|
|
|
|
assert not f.request is f2.request
|
|
|
|
assert f.request.headers == f2.request.headers
|
|
|
|
assert not f.request.headers is f2.request.headers
|
2012-02-24 23:19:54 +00:00
|
|
|
assert f.response == f2.response
|
|
|
|
assert not f.response is f2.response
|
|
|
|
|
|
|
|
f = tutils.tflow_err()
|
|
|
|
f2 = f.copy()
|
|
|
|
assert not f is f2
|
|
|
|
assert not f.request is f2.request
|
|
|
|
assert f.request.headers == f2.request.headers
|
|
|
|
assert not f.request.headers is f2.request.headers
|
|
|
|
assert f.error == f2.error
|
|
|
|
assert not f.error is f2.error
|
2012-02-18 10:56:40 +00:00
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_match(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
f.response = tutils.tresp()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.request = f.response.request
|
|
|
|
assert not f.match(filt.parse("~b test"))
|
2011-07-31 23:17:01 +00:00
|
|
|
assert f.match(None)
|
2012-02-10 02:31:45 +00:00
|
|
|
assert not f.match(filt.parse("~b test"))
|
|
|
|
|
|
|
|
f = tutils.tflow_err()
|
|
|
|
assert f.match(filt.parse("~e"))
|
2011-01-30 22:48:53 +00:00
|
|
|
|
|
|
|
def test_backup(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
f.response = tutils.tresp()
|
2011-02-05 21:28:43 +00:00
|
|
|
f.request = f.response.request
|
|
|
|
f.request.content = "foo"
|
2011-02-01 22:44:28 +00:00
|
|
|
assert not f.modified()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.backup()
|
2011-02-05 21:28:43 +00:00
|
|
|
f.request.content = "bar"
|
2011-02-01 22:44:28 +00:00
|
|
|
assert f.modified()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.revert()
|
2011-02-05 21:28:43 +00:00
|
|
|
assert f.request.content == "foo"
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2012-07-04 23:52:56 +00:00
|
|
|
def test_backup_idempotence(self):
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.backup()
|
|
|
|
f.revert()
|
|
|
|
f.backup()
|
|
|
|
f.revert()
|
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_getset_state(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
f.response = tutils.tresp(f.request)
|
2012-02-09 03:40:31 +00:00
|
|
|
state = f._get_state()
|
2011-08-03 10:48:40 +00:00
|
|
|
assert f._get_state() == flow.Flow._from_state(state)._get_state()
|
2011-02-19 04:21:08 +00:00
|
|
|
|
|
|
|
f.response = None
|
2011-08-03 10:38:23 +00:00
|
|
|
f.error = flow.Error(f.request, "error")
|
2012-02-09 03:40:31 +00:00
|
|
|
state = f._get_state()
|
2011-08-03 10:48:40 +00:00
|
|
|
assert f._get_state() == flow.Flow._from_state(state)._get_state()
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
f2 = tutils.tflow()
|
2011-08-03 10:38:23 +00:00
|
|
|
f2.error = flow.Error(f.request, "e2")
|
2011-02-19 20:55:42 +00:00
|
|
|
assert not f == f2
|
2011-08-03 10:48:40 +00:00
|
|
|
f._load_state(f2._get_state())
|
|
|
|
assert f._get_state() == f2._get_state()
|
2011-02-19 20:55:42 +00:00
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_kill(self):
|
2011-03-13 03:50:11 +00:00
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
f.request = tutils.treq()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.intercept()
|
|
|
|
assert not f.request.acked
|
2011-03-13 03:50:11 +00:00
|
|
|
f.kill(fm)
|
2011-01-30 22:48:53 +00:00
|
|
|
assert f.request.acked
|
|
|
|
f.intercept()
|
2011-03-05 02:58:48 +00:00
|
|
|
f.response = tutils.tresp()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.request = f.response.request
|
2011-08-03 11:02:33 +00:00
|
|
|
f.request._ack()
|
2011-01-30 22:48:53 +00:00
|
|
|
assert not f.response.acked
|
2011-03-13 03:50:11 +00:00
|
|
|
f.kill(fm)
|
2011-01-30 22:48:53 +00:00
|
|
|
assert f.response.acked
|
|
|
|
|
2011-08-02 04:52:47 +00:00
|
|
|
def test_killall(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
|
|
|
|
r = tutils.treq()
|
|
|
|
fm.handle_request(r)
|
|
|
|
|
|
|
|
r = tutils.treq()
|
|
|
|
fm.handle_request(r)
|
|
|
|
|
|
|
|
for i in s.view:
|
|
|
|
assert not i.request.acked
|
|
|
|
s.killall(fm)
|
|
|
|
for i in s.view:
|
|
|
|
assert i.request.acked
|
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_accept_intercept(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
f.request = tutils.treq()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.intercept()
|
|
|
|
assert not f.request.acked
|
|
|
|
f.accept_intercept()
|
|
|
|
assert f.request.acked
|
2011-03-05 02:58:48 +00:00
|
|
|
f.response = tutils.tresp()
|
2011-01-30 22:48:53 +00:00
|
|
|
f.request = f.response.request
|
|
|
|
f.intercept()
|
2011-08-03 11:02:33 +00:00
|
|
|
f.request._ack()
|
2011-01-30 22:48:53 +00:00
|
|
|
assert not f.response.acked
|
|
|
|
f.accept_intercept()
|
|
|
|
assert f.response.acked
|
|
|
|
|
|
|
|
def test_serialization(self):
|
|
|
|
f = flow.Flow(None)
|
2011-03-05 02:58:48 +00:00
|
|
|
f.request = tutils.treq()
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2012-05-26 01:10:31 +00:00
|
|
|
def test_replace_unicode(self):
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.response.content = "\xc2foo"
|
|
|
|
f.replace("foo", u"bar")
|
|
|
|
|
2011-07-22 05:48:42 +00:00
|
|
|
def test_replace(self):
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.request.headers["foo"] = ["foo"]
|
|
|
|
f.request.content = "afoob"
|
|
|
|
|
|
|
|
f.response.headers["foo"] = ["foo"]
|
|
|
|
f.response.content = "afoob"
|
|
|
|
|
|
|
|
assert f.replace("foo", "bar") == 6
|
|
|
|
|
|
|
|
assert f.request.headers["bar"] == ["bar"]
|
|
|
|
assert f.request.content == "abarb"
|
|
|
|
assert f.response.headers["bar"] == ["bar"]
|
|
|
|
assert f.response.content == "abarb"
|
|
|
|
|
2011-08-02 04:52:47 +00:00
|
|
|
f = tutils.tflow_err()
|
|
|
|
f.replace("error", "bar")
|
|
|
|
assert f.error.msg == "bar"
|
|
|
|
|
2012-03-15 22:24:18 +00:00
|
|
|
def test_replace_encoded(self):
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.request.content = "afoob"
|
|
|
|
f.request.encode("gzip")
|
|
|
|
f.response.content = "afoob"
|
|
|
|
f.response.encode("gzip")
|
|
|
|
|
|
|
|
f.replace("foo", "bar")
|
|
|
|
|
|
|
|
assert f.request.content != "abarb"
|
|
|
|
f.request.decode()
|
|
|
|
assert f.request.content == "abarb"
|
|
|
|
|
|
|
|
assert f.response.content != "abarb"
|
|
|
|
f.response.decode()
|
|
|
|
assert f.response.content == "abarb"
|
|
|
|
|
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestState:
|
2011-02-02 23:16:03 +00:00
|
|
|
def test_backup(self):
|
|
|
|
c = flow.State()
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = c.add_request(req)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
f.backup()
|
|
|
|
c.revert(f)
|
|
|
|
|
|
|
|
def test_flow(self):
|
|
|
|
"""
|
|
|
|
normal flow:
|
|
|
|
|
|
|
|
connect -> request -> response
|
|
|
|
"""
|
2011-08-03 10:38:23 +00:00
|
|
|
bc = flow.ClientConnect(("address", 22))
|
2011-02-02 23:16:03 +00:00
|
|
|
c = flow.State()
|
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq(bc)
|
2011-02-19 04:00:24 +00:00
|
|
|
f = c.add_request(req)
|
|
|
|
assert f
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.flow_count() == 1
|
2011-07-31 23:17:01 +00:00
|
|
|
assert c._flow_map.get(req)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.active_flow_count() == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
newreq = tutils.treq()
|
2011-02-16 21:18:38 +00:00
|
|
|
assert c.add_request(newreq)
|
2011-07-31 23:17:01 +00:00
|
|
|
assert c._flow_map.get(newreq)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.active_flow_count() == 2
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
resp = tutils.tresp(req)
|
2011-02-02 23:16:03 +00:00
|
|
|
assert c.add_response(resp)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.flow_count() == 2
|
2011-07-31 23:17:01 +00:00
|
|
|
assert c._flow_map.get(resp.request)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.active_flow_count() == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-03-06 03:11:45 +00:00
|
|
|
unseen_resp = tutils.tresp()
|
|
|
|
assert not c.add_response(unseen_resp)
|
2011-07-31 23:17:01 +00:00
|
|
|
assert not c._flow_map.get(unseen_resp.request)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.active_flow_count() == 1
|
|
|
|
|
|
|
|
resp = tutils.tresp(newreq)
|
|
|
|
assert c.add_response(resp)
|
|
|
|
assert c.active_flow_count() == 0
|
2011-02-19 04:00:24 +00:00
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def test_err(self):
|
|
|
|
c = flow.State()
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = c.add_request(req)
|
2011-08-03 10:38:23 +00:00
|
|
|
e = flow.Error(f.request, "message")
|
2011-02-02 23:16:03 +00:00
|
|
|
assert c.add_error(e)
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
e = flow.Error(tutils.tflow().request, "message")
|
2011-02-02 23:16:03 +00:00
|
|
|
assert not c.add_error(e)
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
c = flow.State()
|
|
|
|
req = tutils.treq()
|
|
|
|
f = c.add_request(req)
|
|
|
|
e = flow.Error(f.request, "message")
|
2012-02-10 02:31:45 +00:00
|
|
|
c.set_limit("~e")
|
|
|
|
assert not c.view
|
2012-02-10 02:04:20 +00:00
|
|
|
assert not c.view
|
|
|
|
assert c.add_error(e)
|
2012-02-10 02:31:45 +00:00
|
|
|
assert c.view
|
2012-02-10 02:04:20 +00:00
|
|
|
|
2011-03-13 01:51:25 +00:00
|
|
|
def test_set_limit(self):
|
2011-02-02 23:16:03 +00:00
|
|
|
c = flow.State()
|
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 04:00:24 +00:00
|
|
|
assert len(c.view) == 0
|
|
|
|
|
2011-08-03 22:34:34 +00:00
|
|
|
c.add_request(req)
|
2011-02-02 23:16:03 +00:00
|
|
|
assert len(c.view) == 1
|
2011-02-19 04:00:24 +00:00
|
|
|
|
2011-03-12 22:24:49 +00:00
|
|
|
c.set_limit("~s")
|
2011-08-02 04:52:47 +00:00
|
|
|
assert c.limit_txt == "~s"
|
2011-02-02 23:16:03 +00:00
|
|
|
assert len(c.view) == 0
|
2011-03-05 02:58:48 +00:00
|
|
|
resp = tutils.tresp(req)
|
2011-02-19 04:00:24 +00:00
|
|
|
c.add_response(resp)
|
|
|
|
assert len(c.view) == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
c.set_limit(None)
|
2011-02-19 04:00:24 +00:00
|
|
|
assert len(c.view) == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-02 23:16:03 +00:00
|
|
|
c.add_request(req)
|
|
|
|
assert len(c.view) == 2
|
2011-03-12 22:24:49 +00:00
|
|
|
c.set_limit("~q")
|
2012-02-24 00:03:24 +00:00
|
|
|
assert len(c.view) == 1
|
2011-03-12 22:24:49 +00:00
|
|
|
c.set_limit("~s")
|
2011-02-19 04:00:24 +00:00
|
|
|
assert len(c.view) == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-03-13 01:51:25 +00:00
|
|
|
assert "Invalid" in c.set_limit("~")
|
|
|
|
|
|
|
|
def test_set_intercept(self):
|
|
|
|
c = flow.State()
|
|
|
|
assert not c.set_intercept("~q")
|
|
|
|
assert c.intercept_txt == "~q"
|
|
|
|
assert "Invalid" in c.set_intercept("~")
|
|
|
|
assert not c.set_intercept(None)
|
|
|
|
assert c.intercept_txt == None
|
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def _add_request(self, state):
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = state.add_request(req)
|
2011-02-02 23:16:03 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def _add_response(self, state):
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-08-03 22:34:34 +00:00
|
|
|
state.add_request(req)
|
2011-03-05 02:58:48 +00:00
|
|
|
resp = tutils.tresp(req)
|
2011-02-19 04:00:24 +00:00
|
|
|
state.add_response(resp)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
def _add_error(self, state):
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = state.add_request(req)
|
2011-08-03 10:38:23 +00:00
|
|
|
f.error = flow.Error(f.request, "msg")
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
def test_clear(self):
|
|
|
|
c = flow.State()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = self._add_request(c)
|
2011-02-02 23:16:03 +00:00
|
|
|
f.intercepting = True
|
|
|
|
|
|
|
|
c.clear()
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.flow_count() == 0
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
def test_dump_flows(self):
|
|
|
|
c = flow.State()
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
|
|
|
self._add_error(c)
|
|
|
|
|
2011-02-16 02:10:00 +00:00
|
|
|
flows = c.view[:]
|
2011-02-02 23:16:03 +00:00
|
|
|
c.clear()
|
2012-02-09 03:40:31 +00:00
|
|
|
|
2011-02-16 02:10:00 +00:00
|
|
|
c.load_flows(flows)
|
2011-07-31 23:17:01 +00:00
|
|
|
assert isinstance(c._flow_list[0], flow.Flow)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
def test_accept_all(self):
|
|
|
|
c = flow.State()
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
|
|
|
self._add_request(c)
|
|
|
|
c.accept_all()
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestSerialize:
|
2011-05-14 23:22:35 +00:00
|
|
|
def _treader(self):
|
|
|
|
sio = StringIO()
|
|
|
|
w = flow.FlowWriter(sio)
|
|
|
|
for i in range(3):
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
w.add(f)
|
|
|
|
for i in range(3):
|
|
|
|
f = tutils.tflow_err()
|
|
|
|
w.add(f)
|
|
|
|
|
|
|
|
sio.seek(0)
|
|
|
|
return flow.FlowReader(sio)
|
|
|
|
|
2011-02-16 01:33:04 +00:00
|
|
|
def test_roundtrip(self):
|
|
|
|
sio = StringIO()
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
2011-07-01 01:07:09 +00:00
|
|
|
f.request.content = "".join(chr(i) for i in range(255))
|
2011-02-16 01:33:04 +00:00
|
|
|
w = flow.FlowWriter(sio)
|
|
|
|
w.add(f)
|
|
|
|
|
|
|
|
sio.seek(0)
|
|
|
|
r = flow.FlowReader(sio)
|
|
|
|
l = list(r.stream())
|
|
|
|
assert len(l) == 1
|
2011-07-01 01:07:09 +00:00
|
|
|
|
|
|
|
f2 = l[0]
|
2011-08-03 10:48:40 +00:00
|
|
|
assert f2._get_state() == f._get_state()
|
2011-08-03 11:02:33 +00:00
|
|
|
assert f2.request._assemble() == f.request._assemble()
|
2011-02-16 01:33:04 +00:00
|
|
|
|
2011-05-14 23:22:35 +00:00
|
|
|
def test_load_flows(self):
|
|
|
|
r = self._treader()
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
fm.load_flows(r)
|
2011-07-31 23:17:01 +00:00
|
|
|
assert len(s._flow_list) == 6
|
2011-05-14 23:22:35 +00:00
|
|
|
|
|
|
|
|
2011-03-11 02:16:31 +00:00
|
|
|
def test_error(self):
|
|
|
|
sio = StringIO()
|
|
|
|
sio.write("bogus")
|
|
|
|
sio.seek(0)
|
|
|
|
r = flow.FlowReader(sio)
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(flow.FlowReadError, list, r.stream())
|
2011-03-11 02:16:31 +00:00
|
|
|
|
|
|
|
f = flow.FlowReadError("foo")
|
|
|
|
assert f.strerror == "foo"
|
|
|
|
|
2012-04-10 22:10:53 +00:00
|
|
|
def test_versioncheck(self):
|
|
|
|
f = tutils.tflow()
|
|
|
|
d = f._get_state()
|
|
|
|
d["version"] = (0, 0)
|
|
|
|
sio = StringIO()
|
|
|
|
tnetstring.dump(d, sio)
|
|
|
|
sio.seek(0)
|
|
|
|
|
|
|
|
r = flow.FlowReader(sio)
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises("version", list, r.stream())
|
2012-04-10 22:10:53 +00:00
|
|
|
|
2011-02-16 01:33:04 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestFlowMaster:
|
2011-08-03 04:36:20 +00:00
|
|
|
def test_load_script(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2012-06-09 01:42:43 +00:00
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/a.py"))
|
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/a.py"))
|
2012-02-10 02:55:58 +00:00
|
|
|
assert not fm.load_script(None)
|
2011-08-03 04:36:20 +00:00
|
|
|
assert fm.load_script("nonexistent")
|
2012-06-09 01:42:43 +00:00
|
|
|
assert "ValueError" in fm.load_script(tutils.test_data.path("scripts/starterr.py"))
|
2011-08-03 04:36:20 +00:00
|
|
|
|
2012-05-16 06:24:32 +00:00
|
|
|
def test_replay(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.request.content = flow.CONTENT_MISSING
|
|
|
|
assert "missing" in fm.replay_request(f)
|
|
|
|
|
|
|
|
f.intercepting = True
|
|
|
|
assert "intercepting" in fm.replay_request(f)
|
|
|
|
|
2012-02-10 02:55:58 +00:00
|
|
|
def test_script_reqerr(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2012-06-09 01:42:43 +00:00
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py"))
|
2012-02-10 02:55:58 +00:00
|
|
|
req = tutils.treq()
|
|
|
|
fm.handle_clientconnect(req.client_conn)
|
2012-02-19 22:04:07 +00:00
|
|
|
assert fm.handle_request(req)
|
2012-02-10 02:55:58 +00:00
|
|
|
|
2011-08-03 04:36:20 +00:00
|
|
|
def test_script(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2012-06-09 01:42:43 +00:00
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
|
2011-08-03 04:36:20 +00:00
|
|
|
req = tutils.treq()
|
|
|
|
fm.handle_clientconnect(req.client_conn)
|
|
|
|
assert fm.script.ns["log"][-1] == "clientconnect"
|
|
|
|
f = fm.handle_request(req)
|
|
|
|
assert fm.script.ns["log"][-1] == "request"
|
|
|
|
resp = tutils.tresp(req)
|
|
|
|
fm.handle_response(resp)
|
|
|
|
assert fm.script.ns["log"][-1] == "response"
|
2011-08-03 10:38:23 +00:00
|
|
|
dc = flow.ClientDisconnect(req.client_conn)
|
2011-08-03 04:36:20 +00:00
|
|
|
fm.handle_clientdisconnect(dc)
|
|
|
|
assert fm.script.ns["log"][-1] == "clientdisconnect"
|
2011-08-03 10:38:23 +00:00
|
|
|
err = flow.Error(f.request, "msg")
|
2011-08-03 04:36:20 +00:00
|
|
|
fm.handle_error(err)
|
|
|
|
assert fm.script.ns["log"][-1] == "error"
|
|
|
|
|
2012-02-18 10:56:40 +00:00
|
|
|
def test_duplicate_flow(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
fm.load_flow(f)
|
|
|
|
assert s.flow_count() == 1
|
|
|
|
f2 = fm.duplicate_flow(f)
|
2012-02-18 11:17:47 +00:00
|
|
|
assert f2.response
|
2012-02-18 10:56:40 +00:00
|
|
|
assert s.flow_count() == 2
|
2012-02-18 11:17:47 +00:00
|
|
|
assert s.index(f2)
|
2012-02-18 10:56:40 +00:00
|
|
|
|
2011-02-19 04:21:08 +00:00
|
|
|
def test_all(self):
|
2011-02-16 03:43:35 +00:00
|
|
|
s = flow.State()
|
2011-02-19 04:21:08 +00:00
|
|
|
fm = flow.FlowMaster(None, s)
|
2011-03-09 00:15:31 +00:00
|
|
|
fm.anticache = True
|
2011-07-15 03:21:04 +00:00
|
|
|
fm.anticomp = True
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 04:21:08 +00:00
|
|
|
fm.handle_clientconnect(req.client_conn)
|
|
|
|
|
|
|
|
f = fm.handle_request(req)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert s.flow_count() == 1
|
2011-02-19 04:00:24 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
resp = tutils.tresp(req)
|
2011-02-19 04:21:08 +00:00
|
|
|
fm.handle_response(resp)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert s.flow_count() == 1
|
2011-02-19 04:21:08 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
rx = tutils.tresp()
|
2011-02-19 04:21:08 +00:00
|
|
|
assert not fm.handle_response(rx)
|
2012-02-09 03:40:31 +00:00
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
dc = flow.ClientDisconnect(req.client_conn)
|
2012-02-10 02:55:58 +00:00
|
|
|
req.client_conn.requestcount = 1
|
2011-08-02 04:52:47 +00:00
|
|
|
fm.handle_clientdisconnect(dc)
|
2011-02-19 04:21:08 +00:00
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
err = flow.Error(f.request, "msg")
|
2011-02-19 04:21:08 +00:00
|
|
|
fm.handle_error(err)
|
|
|
|
|
2012-06-09 01:55:55 +00:00
|
|
|
fm.load_script(tutils.test_data.path("scripts/a.py"))
|
2012-02-10 02:55:58 +00:00
|
|
|
fm.shutdown()
|
|
|
|
|
2011-03-06 03:02:28 +00:00
|
|
|
def test_client_playback(self):
|
|
|
|
s = flow.State()
|
|
|
|
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
pb = [tutils.tflow_full(), f]
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2012-03-05 11:40:18 +00:00
|
|
|
assert not fm.start_server_playback(pb, False, [], False, False)
|
2011-03-06 03:54:49 +00:00
|
|
|
assert not fm.start_client_playback(pb, False)
|
2011-03-06 03:02:28 +00:00
|
|
|
|
|
|
|
q = Queue.Queue()
|
2011-03-06 03:11:45 +00:00
|
|
|
assert not fm.state.flow_count()
|
2011-03-06 03:02:28 +00:00
|
|
|
fm.tick(q)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert fm.state.flow_count()
|
2011-03-06 03:02:28 +00:00
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
fm.handle_error(flow.Error(f.request, "error"))
|
2011-03-06 03:02:28 +00:00
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
def test_server_playback(self):
|
2011-02-20 20:54:39 +00:00
|
|
|
s = flow.State()
|
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
f.response = tutils.tresp(f.request)
|
2011-02-20 20:54:39 +00:00
|
|
|
pb = [f]
|
|
|
|
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2011-03-10 22:56:10 +00:00
|
|
|
fm.refresh_server_playback = True
|
2011-03-05 02:58:48 +00:00
|
|
|
assert not fm.do_server_playback(tutils.tflow())
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2012-03-05 11:40:18 +00:00
|
|
|
fm.start_server_playback(pb, False, [], False, False)
|
2011-03-05 02:58:48 +00:00
|
|
|
assert fm.do_server_playback(tutils.tflow())
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2012-03-05 11:40:18 +00:00
|
|
|
fm.start_server_playback(pb, False, [], True, False)
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tflow()
|
2011-02-20 20:54:39 +00:00
|
|
|
r.request.content = "gibble"
|
2011-03-05 00:03:26 +00:00
|
|
|
assert not fm.do_server_playback(r)
|
|
|
|
|
2011-03-06 04:08:56 +00:00
|
|
|
assert fm.do_server_playback(tutils.tflow())
|
|
|
|
q = Queue.Queue()
|
|
|
|
fm.tick(q)
|
2011-08-02 04:14:33 +00:00
|
|
|
assert controller.should_exit
|
2011-03-06 04:08:56 +00:00
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
fm.stop_server_playback()
|
|
|
|
assert not fm.server_playback
|
|
|
|
|
2011-02-24 02:15:51 +00:00
|
|
|
def test_stickycookie(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
assert "Invalid" in fm.set_stickycookie("~h")
|
|
|
|
fm.set_stickycookie(".*")
|
|
|
|
assert fm.stickycookie_state
|
|
|
|
fm.set_stickycookie(None)
|
|
|
|
assert not fm.stickycookie_state
|
|
|
|
|
|
|
|
fm.set_stickycookie(".*")
|
2011-03-05 02:58:48 +00:00
|
|
|
tf = tutils.tflow_full()
|
2011-02-24 02:15:51 +00:00
|
|
|
tf.response.headers["set-cookie"] = ["foo=bar"]
|
|
|
|
fm.handle_request(tf.request)
|
2011-08-03 22:34:34 +00:00
|
|
|
fm.handle_response(tf.response)
|
2011-02-24 02:15:51 +00:00
|
|
|
assert fm.stickycookie_state.jar
|
|
|
|
assert not "cookie" in tf.request.headers
|
2012-02-18 10:56:40 +00:00
|
|
|
tf = tf.copy()
|
2011-02-24 02:15:51 +00:00
|
|
|
fm.handle_request(tf.request)
|
|
|
|
assert tf.request.headers["cookie"] == ["foo=bar"]
|
|
|
|
|
2011-03-20 04:31:54 +00:00
|
|
|
def test_stickyauth(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
assert "Invalid" in fm.set_stickyauth("~h")
|
|
|
|
fm.set_stickyauth(".*")
|
|
|
|
assert fm.stickyauth_state
|
|
|
|
fm.set_stickyauth(None)
|
|
|
|
assert not fm.stickyauth_state
|
|
|
|
|
|
|
|
fm.set_stickyauth(".*")
|
|
|
|
tf = tutils.tflow_full()
|
|
|
|
tf.request.headers["authorization"] = ["foo"]
|
|
|
|
fm.handle_request(tf.request)
|
|
|
|
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
assert fm.stickyauth_state.hosts
|
|
|
|
assert not "authorization" in f.request.headers
|
|
|
|
fm.handle_request(f.request)
|
|
|
|
assert f.request.headers["authorization"] == ["foo"]
|
|
|
|
|
2012-07-08 22:18:37 +00:00
|
|
|
def test_stream(self):
|
|
|
|
with tutils.tmpdir() as tdir:
|
|
|
|
p = os.path.join(tdir, "foo")
|
|
|
|
def r():
|
|
|
|
r = flow.FlowReader(open(p))
|
|
|
|
return list(r.stream())
|
|
|
|
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
tf = tutils.tflow_full()
|
|
|
|
|
|
|
|
fm.start_stream(file(p, "ab"))
|
|
|
|
fm.handle_request(tf.request)
|
|
|
|
fm.handle_response(tf.response)
|
|
|
|
fm.stop_stream()
|
|
|
|
|
|
|
|
assert r()[0].response
|
|
|
|
|
|
|
|
tf = tutils.tflow_full()
|
|
|
|
fm.start_stream(file(p, "ab"))
|
|
|
|
fm.handle_request(tf.request)
|
|
|
|
fm.shutdown()
|
|
|
|
|
|
|
|
assert not r()[1].response
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestRequest:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_simple(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2011-08-03 10:38:23 +00:00
|
|
|
h["test"] = ["test"]
|
|
|
|
c = flow.ClientConnect(("addr", 2222))
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
2011-08-03 21:26:26 +00:00
|
|
|
u = r.get_url()
|
2011-08-03 10:38:23 +00:00
|
|
|
assert r.set_url(u)
|
|
|
|
assert not r.set_url("")
|
2011-08-03 21:26:26 +00:00
|
|
|
assert r.get_url() == u
|
2011-08-03 11:02:33 +00:00
|
|
|
assert r._assemble()
|
2012-08-04 01:18:05 +00:00
|
|
|
assert r.size() == len(r._assemble())
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
r2 = r.copy()
|
|
|
|
assert r == r2
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
r.content = None
|
|
|
|
assert r._assemble()
|
2012-08-04 01:18:05 +00:00
|
|
|
assert r.size() == len(r._assemble())
|
2012-02-10 02:04:20 +00:00
|
|
|
|
|
|
|
r.close = True
|
|
|
|
assert "connection: close" in r._assemble()
|
|
|
|
|
|
|
|
assert r._assemble(True)
|
|
|
|
|
2012-05-16 03:42:58 +00:00
|
|
|
r.content = flow.CONTENT_MISSING
|
|
|
|
assert not r._assemble()
|
|
|
|
|
2012-08-19 01:03:21 +00:00
|
|
|
def test_path_components(self):
|
|
|
|
h = flow.ODictCaseless()
|
|
|
|
c = flow.ClientConnect(("addr", 2222))
|
|
|
|
r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
|
|
|
assert r.get_path_components() == []
|
|
|
|
r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/foo/bar", h, "content")
|
|
|
|
assert r.get_path_components() == ["foo", "bar"]
|
|
|
|
q = flow.ODict()
|
|
|
|
q["test"] = ["123"]
|
|
|
|
r.set_query(q)
|
|
|
|
assert r.get_path_components() == ["foo", "bar"]
|
|
|
|
|
|
|
|
r.set_path_components([])
|
|
|
|
assert r.get_path_components() == []
|
|
|
|
r.set_path_components(["foo"])
|
|
|
|
assert r.get_path_components() == ["foo"]
|
|
|
|
r.set_path_components(["/oo"])
|
|
|
|
assert r.get_path_components() == ["/oo"]
|
|
|
|
assert "%2F" in r.path
|
2012-08-04 01:18:05 +00:00
|
|
|
|
2012-02-10 01:27:39 +00:00
|
|
|
def test_getset_form_urlencoded(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2012-02-10 01:27:39 +00:00
|
|
|
h["content-type"] = [flow.HDR_FORM_URLENCODED]
|
2012-02-19 22:13:35 +00:00
|
|
|
d = flow.ODict([("one", "two"), ("three", "four")])
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/", h, utils.urlencode(d.lst))
|
2012-02-10 01:27:39 +00:00
|
|
|
assert r.get_form_urlencoded() == d
|
|
|
|
|
2012-02-19 22:13:35 +00:00
|
|
|
d = flow.ODict([("x", "y")])
|
2012-02-10 01:27:39 +00:00
|
|
|
r.set_form_urlencoded(d)
|
|
|
|
assert r.get_form_urlencoded() == d
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
r.headers["content-type"] = ["foo"]
|
|
|
|
assert not r.get_form_urlencoded()
|
2012-02-10 01:27:39 +00:00
|
|
|
|
2012-02-09 03:40:31 +00:00
|
|
|
def test_getset_query(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2012-02-09 03:40:31 +00:00
|
|
|
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/foo?x=y&a=b", h, "content")
|
2012-02-09 03:40:31 +00:00
|
|
|
q = r.get_query()
|
2012-02-19 21:44:47 +00:00
|
|
|
assert q.lst == [("x", "y"), ("a", "b")]
|
2012-02-09 03:40:31 +00:00
|
|
|
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
2012-02-09 03:40:31 +00:00
|
|
|
q = r.get_query()
|
|
|
|
assert not q
|
|
|
|
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/?adsfa", h, "content")
|
2012-02-09 03:40:31 +00:00
|
|
|
q = r.get_query()
|
|
|
|
assert not q
|
|
|
|
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/foo?x=y&a=b", h, "content")
|
2012-02-09 03:40:31 +00:00
|
|
|
assert r.get_query()
|
2012-02-19 21:44:47 +00:00
|
|
|
r.set_query(flow.ODict([]))
|
2012-02-09 03:40:31 +00:00
|
|
|
assert not r.get_query()
|
2012-02-19 21:44:47 +00:00
|
|
|
qv = flow.ODict([("a", "b"), ("c", "d")])
|
2012-02-09 03:40:31 +00:00
|
|
|
r.set_query(qv)
|
|
|
|
assert r.get_query() == qv
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_anticache(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
2011-08-03 10:38:23 +00:00
|
|
|
h["if-modified-since"] = ["test"]
|
|
|
|
h["if-none-match"] = ["test"]
|
|
|
|
r.anticache()
|
|
|
|
assert not "if-modified-since" in r.headers
|
|
|
|
assert not "if-none-match" in r.headers
|
|
|
|
|
|
|
|
def test_getset_state(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2011-08-03 10:38:23 +00:00
|
|
|
h["test"] = ["test"]
|
|
|
|
c = flow.ClientConnect(("addr", 2222))
|
2012-06-09 22:46:22 +00:00
|
|
|
r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
2011-08-03 10:48:40 +00:00
|
|
|
state = r._get_state()
|
|
|
|
assert flow.Request._from_state(state) == r
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
r.client_conn = None
|
2011-08-03 10:48:40 +00:00
|
|
|
state = r._get_state()
|
|
|
|
assert flow.Request._from_state(state) == r
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2012-06-09 22:46:22 +00:00
|
|
|
r2 = flow.Request(c, (1, 1), "testing", 20, "http", "PUT", "/foo", h, "test")
|
2011-08-03 10:38:23 +00:00
|
|
|
assert not r == r2
|
2011-08-03 10:48:40 +00:00
|
|
|
r._load_state(r2._get_state())
|
2011-08-03 10:38:23 +00:00
|
|
|
assert r == r2
|
|
|
|
|
|
|
|
r2.client_conn = None
|
2011-08-03 10:48:40 +00:00
|
|
|
r._load_state(r2._get_state())
|
2011-08-03 10:38:23 +00:00
|
|
|
assert not r.client_conn
|
|
|
|
|
|
|
|
def test_replace(self):
|
|
|
|
r = tutils.treq()
|
|
|
|
r.path = "path/foo"
|
|
|
|
r.headers["Foo"] = ["fOo"]
|
|
|
|
r.content = "afoob"
|
|
|
|
assert r.replace("foo(?i)", "boo") == 4
|
|
|
|
assert r.path == "path/boo"
|
|
|
|
assert not "foo" in r.content
|
|
|
|
assert r.headers["boo"] == ["boo"]
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
def test_constrain_encoding(self):
|
|
|
|
r = tutils.treq()
|
|
|
|
r.headers["accept-encoding"] = ["gzip", "oink"]
|
|
|
|
r.constrain_encoding()
|
|
|
|
assert "oink" not in r.headers["accept-encoding"]
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_decodeencode(self):
|
|
|
|
r = tutils.treq()
|
|
|
|
r.headers["content-encoding"] = ["identity"]
|
|
|
|
r.content = "falafel"
|
|
|
|
r.decode()
|
|
|
|
assert not r.headers["content-encoding"]
|
|
|
|
assert r.content == "falafel"
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
r = tutils.treq()
|
|
|
|
r.content = "falafel"
|
|
|
|
assert not r.decode()
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
r = tutils.treq()
|
|
|
|
r.headers["content-encoding"] = ["identity"]
|
|
|
|
r.content = "falafel"
|
|
|
|
r.encode("identity")
|
|
|
|
assert r.headers["content-encoding"] == ["identity"]
|
|
|
|
assert r.content == "falafel"
|
|
|
|
|
|
|
|
r = tutils.treq()
|
|
|
|
r.headers["content-encoding"] = ["identity"]
|
|
|
|
r.content = "falafel"
|
|
|
|
r.encode("gzip")
|
|
|
|
assert r.headers["content-encoding"] == ["gzip"]
|
|
|
|
assert r.content != "falafel"
|
|
|
|
r.decode()
|
|
|
|
assert not r.headers["content-encoding"]
|
|
|
|
assert r.content == "falafel"
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestResponse:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_simple(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2011-08-03 10:38:23 +00:00
|
|
|
h["test"] = ["test"]
|
|
|
|
c = flow.ClientConnect(("addr", 2222))
|
2012-06-09 22:46:22 +00:00
|
|
|
req = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
2012-06-10 01:27:43 +00:00
|
|
|
resp = flow.Response(req, (1, 1), 200, "msg", h.copy(), "content", None)
|
2011-08-03 11:02:33 +00:00
|
|
|
assert resp._assemble()
|
2012-08-04 01:18:05 +00:00
|
|
|
assert resp.size() == len(resp._assemble())
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
resp2 = resp.copy()
|
|
|
|
assert resp2 == resp
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
resp.content = None
|
|
|
|
assert resp._assemble()
|
2012-08-04 01:18:05 +00:00
|
|
|
assert resp.size() == len(resp._assemble())
|
2012-02-10 02:04:20 +00:00
|
|
|
|
2012-05-16 03:42:58 +00:00
|
|
|
resp.content = flow.CONTENT_MISSING
|
|
|
|
assert not resp._assemble()
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_refresh(self):
|
|
|
|
r = tutils.tresp()
|
|
|
|
n = time.time()
|
|
|
|
r.headers["date"] = [email.utils.formatdate(n)]
|
|
|
|
pre = r.headers["date"]
|
|
|
|
r.refresh(n)
|
|
|
|
assert pre == r.headers["date"]
|
|
|
|
r.refresh(n+60)
|
|
|
|
|
|
|
|
d = email.utils.parsedate_tz(r.headers["date"][0])
|
|
|
|
d = email.utils.mktime_tz(d)
|
|
|
|
# Weird that this is not exact...
|
|
|
|
assert abs(60-(d-n)) <= 1
|
|
|
|
|
|
|
|
r.headers["set-cookie"] = ["MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"]
|
|
|
|
r.refresh()
|
|
|
|
|
|
|
|
def test_refresh_cookie(self):
|
|
|
|
r = tutils.tresp()
|
|
|
|
|
|
|
|
# Invalid expires format, sent to us by Reddit.
|
|
|
|
c = "rfoo=bar; Domain=reddit.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/"
|
|
|
|
assert r._refresh_cookie(c, 60)
|
|
|
|
|
|
|
|
c = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
|
|
|
|
assert "00:21:38" in r._refresh_cookie(c, 60)
|
|
|
|
|
|
|
|
def test_getset_state(self):
|
2012-02-19 22:29:36 +00:00
|
|
|
h = flow.ODictCaseless()
|
2011-08-03 10:38:23 +00:00
|
|
|
h["test"] = ["test"]
|
|
|
|
c = flow.ClientConnect(("addr", 2222))
|
2012-06-09 22:46:22 +00:00
|
|
|
req = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
|
2012-06-10 01:27:43 +00:00
|
|
|
resp = flow.Response(req, (1, 1), 200, "msg", h.copy(), "content", None)
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2011-08-03 10:48:40 +00:00
|
|
|
state = resp._get_state()
|
|
|
|
assert flow.Response._from_state(req, state) == resp
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2012-06-10 01:27:43 +00:00
|
|
|
resp2 = flow.Response(req, (1, 1), 220, "foo", h.copy(), "test", None)
|
2011-08-03 10:38:23 +00:00
|
|
|
assert not resp == resp2
|
2011-08-03 10:48:40 +00:00
|
|
|
resp._load_state(resp2._get_state())
|
2011-08-03 10:38:23 +00:00
|
|
|
assert resp == resp2
|
|
|
|
|
|
|
|
def test_replace(self):
|
|
|
|
r = tutils.tresp()
|
|
|
|
r.headers["Foo"] = ["fOo"]
|
|
|
|
r.content = "afoob"
|
|
|
|
assert r.replace("foo(?i)", "boo") == 3
|
|
|
|
assert not "foo" in r.content
|
|
|
|
assert r.headers["boo"] == ["boo"]
|
|
|
|
|
|
|
|
def test_decodeencode(self):
|
|
|
|
r = tutils.tresp()
|
|
|
|
r.headers["content-encoding"] = ["identity"]
|
|
|
|
r.content = "falafel"
|
|
|
|
r.decode()
|
|
|
|
assert not r.headers["content-encoding"]
|
|
|
|
assert r.content == "falafel"
|
|
|
|
|
|
|
|
r = tutils.tresp()
|
|
|
|
r.headers["content-encoding"] = ["identity"]
|
|
|
|
r.content = "falafel"
|
|
|
|
r.encode("identity")
|
|
|
|
assert r.headers["content-encoding"] == ["identity"]
|
|
|
|
assert r.content == "falafel"
|
|
|
|
|
|
|
|
r = tutils.tresp()
|
|
|
|
r.headers["content-encoding"] = ["identity"]
|
|
|
|
r.content = "falafel"
|
|
|
|
r.encode("gzip")
|
|
|
|
assert r.headers["content-encoding"] == ["gzip"]
|
|
|
|
assert r.content != "falafel"
|
|
|
|
r.decode()
|
|
|
|
assert not r.headers["content-encoding"]
|
|
|
|
assert r.content == "falafel"
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestError:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_getset_state(self):
|
|
|
|
e = flow.Error(None, "Error")
|
2011-08-03 10:48:40 +00:00
|
|
|
state = e._get_state()
|
2012-02-17 23:27:59 +00:00
|
|
|
assert flow.Error._from_state(None, state) == e
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
assert e.copy()
|
|
|
|
|
|
|
|
e2 = flow.Error(None, "bar")
|
|
|
|
assert not e == e2
|
2011-08-03 10:48:40 +00:00
|
|
|
e._load_state(e2._get_state())
|
2011-08-03 10:38:23 +00:00
|
|
|
assert e == e2
|
|
|
|
|
|
|
|
|
|
|
|
e3 = e.copy()
|
|
|
|
assert e3 == e
|
|
|
|
|
|
|
|
def test_replace(self):
|
|
|
|
e = flow.Error(None, "amoop")
|
|
|
|
e.replace("moo", "bar")
|
|
|
|
assert e.msg == "abarp"
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestClientConnect:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_state(self):
|
|
|
|
c = flow.ClientConnect(("a", 22))
|
2011-08-03 10:48:40 +00:00
|
|
|
assert flow.ClientConnect._from_state(c._get_state()) == c
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
c2 = flow.ClientConnect(("a", 25))
|
|
|
|
assert not c == c2
|
|
|
|
|
2012-04-03 10:37:24 +00:00
|
|
|
c2.requestcount = 99
|
2011-08-03 10:48:40 +00:00
|
|
|
c._load_state(c2._get_state())
|
2012-04-03 10:37:24 +00:00
|
|
|
assert c.requestcount == 99
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
c3 = c.copy()
|
|
|
|
assert c3 == c
|
|
|
|
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
def test_decoded():
|
|
|
|
r = tutils.treq()
|
|
|
|
assert r.content == "content"
|
|
|
|
assert not r.headers["content-encoding"]
|
|
|
|
r.encode("gzip")
|
|
|
|
assert r.headers["content-encoding"]
|
|
|
|
assert r.content != "content"
|
|
|
|
with flow.decoded(r):
|
2012-03-15 22:12:56 +00:00
|
|
|
assert not r.headers["content-encoding"]
|
2012-06-09 01:42:43 +00:00
|
|
|
assert r.content == "content"
|
|
|
|
assert r.headers["content-encoding"]
|
|
|
|
assert r.content != "content"
|
|
|
|
|
|
|
|
with flow.decoded(r):
|
|
|
|
r.content = "foo"
|
|
|
|
|
|
|
|
assert r.content != "foo"
|
|
|
|
r.decode()
|
|
|
|
assert r.content == "foo"
|
|
|
|
|
|
|
|
|
|
|
|
def test_replacehooks():
|
|
|
|
h = flow.ReplaceHooks()
|
|
|
|
h.add("~q", "foo", "bar")
|
|
|
|
assert h.lst
|
2012-08-18 11:39:52 +00:00
|
|
|
|
|
|
|
h.set(
|
|
|
|
[
|
|
|
|
(".*", "one", "two"),
|
|
|
|
(".*", "three", "four"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert h.count() == 2
|
|
|
|
|
|
|
|
h.clear()
|
2012-06-09 01:42:43 +00:00
|
|
|
assert not h.lst
|
|
|
|
|
|
|
|
h.add("~q", "foo", "bar")
|
|
|
|
h.add("~s", "foo", "bar")
|
|
|
|
|
|
|
|
v = h.get_specs()
|
|
|
|
assert v == [('~q', 'foo', 'bar'), ('~s', 'foo', 'bar')]
|
|
|
|
assert h.count() == 2
|
|
|
|
h.clear()
|
|
|
|
assert h.count() == 0
|
|
|
|
|
|
|
|
f = tutils.tflow()
|
|
|
|
f.request.content = "foo"
|
|
|
|
h.add("~s", "foo", "bar")
|
|
|
|
h.run(f)
|
|
|
|
assert f.request.content == "foo"
|
|
|
|
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.request.content = "foo"
|
|
|
|
f.response.content = "foo"
|
|
|
|
h.run(f)
|
|
|
|
assert f.response.content == "bar"
|
|
|
|
assert f.request.content == "foo"
|
|
|
|
|
|
|
|
f = tutils.tflow()
|
|
|
|
h.clear()
|
|
|
|
h.add("~q", "foo", "bar")
|
|
|
|
f.request.content = "foo"
|
|
|
|
h.run(f)
|
|
|
|
assert f.request.content == "bar"
|
|
|
|
|
|
|
|
assert not h.add("~", "foo", "bar")
|
|
|
|
assert not h.add("foo", "*", "bar")
|
2012-03-16 04:13:11 +00:00
|
|
|
|
2012-08-18 11:39:52 +00:00
|
|
|
|
|
|
|
def test_setheaders():
|
|
|
|
h = flow.SetHeaders()
|
|
|
|
h.add("~q", "foo", "bar")
|
|
|
|
assert h.lst
|
|
|
|
|
|
|
|
h.set(
|
|
|
|
[
|
|
|
|
(".*", "one", "two"),
|
|
|
|
(".*", "three", "four"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert h.count() == 2
|
|
|
|
|
|
|
|
h.clear()
|
|
|
|
assert not h.lst
|
|
|
|
|
|
|
|
h.add("~q", "foo", "bar")
|
|
|
|
h.add("~s", "foo", "bar")
|
|
|
|
|
|
|
|
v = h.get_specs()
|
|
|
|
assert v == [('~q', 'foo', 'bar'), ('~s', 'foo', 'bar')]
|
|
|
|
assert h.count() == 2
|
|
|
|
h.clear()
|
|
|
|
assert h.count() == 0
|
|
|
|
|
|
|
|
f = tutils.tflow()
|
|
|
|
f.request.content = "foo"
|
|
|
|
h.add("~s", "foo", "bar")
|
|
|
|
h.run(f)
|
|
|
|
assert f.request.content == "foo"
|
|
|
|
|
|
|
|
|
|
|
|
h.clear()
|
|
|
|
h.add("~s", "one", "two")
|
|
|
|
h.add("~s", "one", "three")
|
|
|
|
f = tutils.tflow_full()
|
|
|
|
f.request.headers["one"] = ["xxx"]
|
|
|
|
f.response.headers["one"] = ["xxx"]
|
|
|
|
h.run(f)
|
|
|
|
assert f.request.headers["one"] == ["xxx"]
|
|
|
|
assert f.response.headers["one"] == ["two", "three"]
|
|
|
|
|
|
|
|
h.clear()
|
|
|
|
h.add("~q", "one", "two")
|
|
|
|
h.add("~q", "one", "three")
|
|
|
|
f = tutils.tflow()
|
|
|
|
f.request.headers["one"] = ["xxx"]
|
|
|
|
h.run(f)
|
|
|
|
assert f.request.headers["one"] == ["two", "three"]
|
|
|
|
|
|
|
|
assert not h.add("~", "foo", "bar")
|