diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-06-24 17:01:04 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-06-24 17:01:04 +1200 |
commit | d4ad3f0b2cc5ae878108e13e86679fac2abaedb2 (patch) | |
tree | 5341869d10a041fb785f576418a5bc032341730b /test/test_rparse.py | |
parent | 05f5e772c3f59c9be40132eb7afd4f049ced140a (diff) | |
download | mitmproxy-d4ad3f0b2cc5ae878108e13e86679fac2abaedb2.tar.gz mitmproxy-d4ad3f0b2cc5ae878108e13e86679fac2abaedb2.tar.bz2 mitmproxy-d4ad3f0b2cc5ae878108e13e86679fac2abaedb2.zip |
Refactor to extract ready_actions and write_values.
Diffstat (limited to 'test/test_rparse.py')
-rw-r--r-- | test/test_rparse.py | 116 |
1 files changed, 56 insertions, 60 deletions
diff --git a/test/test_rparse.py b/test/test_rparse.py index 0813f22e..727a89f6 100644 --- a/test/test_rparse.py +++ b/test/test_rparse.py @@ -137,9 +137,9 @@ class TestMisc: class TestDisconnects: - def test_parse(self): - assert (0, "disconnect") in rparse.parse({}, "400:d0").actions - assert ("r", "disconnect") in rparse.parse({}, "400:dr").actions + def test_parse_response(self): + assert (0, "disconnect") in rparse.parse_response({}, "400:d0").actions + assert ("r", "disconnect") in rparse.parse_response({}, "400:dr").actions def test_at(self): e = rparse.DisconnectAt.expr() @@ -156,13 +156,13 @@ class TestDisconnects: class TestShortcuts: - def test_parse(self): - assert rparse.parse({}, "400:c'foo'").headers[0][0][:] == "Content-Type" - assert rparse.parse({}, "400:l'foo'").headers[0][0][:] == "Location" + def test_parse_response(self): + assert rparse.parse_response({}, "400:c'foo'").headers[0][0][:] == "Content-Type" + assert rparse.parse_response({}, "400:l'foo'").headers[0][0][:] == "Location" class TestPauses: - def test_parse(self): + def test_parse_response(self): e = rparse.PauseAt.expr() v = e.parseString("p10,10")[0] assert v.seconds == 10 @@ -178,109 +178,105 @@ class TestPauses: assert v.offset == "a" def test_request(self): - r = rparse.parse({}, '400:p10,10') + r = rparse.parse_response({}, '400:p10,10') assert r.actions[0] == (10, "pause", 10) class TestParse: def test_parse_err(self): - tutils.raises(rparse.ParseException, rparse.parse, {}, "400:msg,b:") + tutils.raises(rparse.ParseException, rparse.parse_response, {}, "400:msg,b:") try: - rparse.parse({}, "400'msg':b:") + rparse.parse_response({}, "400'msg':b:") except rparse.ParseException, v: assert v.marked() assert str(v) def test_parse_header(self): - r = rparse.parse({}, '400:h"foo"="bar"') + r = rparse.parse_response({}, '400:h"foo"="bar"') assert r.get_header("foo") == "bar" def test_parse_pause_before(self): - r = rparse.parse({}, "400:p10,0") + r = rparse.parse_response({}, "400:p10,0") assert (0, "pause", 10) in r.actions def test_parse_pause_after(self): - r = rparse.parse({}, "400:p10,a") + r = rparse.parse_response({}, "400:p10,a") assert ("a", "pause", 10) in r.actions def test_parse_pause_random(self): - r = rparse.parse({}, "400:p10,r") + r = rparse.parse_response({}, "400:p10,r") assert ("r", "pause", 10) in r.actions def test_parse_stress(self): - r = rparse.parse({}, "400:b@100g") + r = rparse.parse_response({}, "400:b@100g") assert r.length() -class TestResponse: - def dummy_response(self): - return rparse.parse({}, "400'msg'") - - def test_response(self): - r = rparse.parse({}, "400'msg'") - assert r.code == 400 - assert r.msg == "msg" - - r = rparse.parse({}, "400'msg':b@100b") - assert r.msg == "msg" - assert r.body[:] - assert str(r) - - def test_ready_actions(self): - r = rparse.parse({}, "400'msg'") - - x = [(0, 5)] - assert r.ready_actions(100, x) == x - - x = [("r", 5)] - ret = r.ready_actions(100, x) - assert 0 <= ret[0][0] < 100 - - x = [("a", "pause", 5)] - ret = r.ready_actions(100, x) - assert ret[0][0] > 100 - - x = [(1, 5), (0, 5)] - assert r.ready_actions(100, x) == sorted(x) - +class TestWriteValues: def test_write_values_disconnects(self): - r = self.dummy_response() s = cStringIO.StringIO() tst = "foo"*100 - r.write_values(s, [tst], [(0, "disconnect")], blocksize=5) + rparse.write_values(s, [tst], [(0, "disconnect")], blocksize=5) assert not s.getvalue() def test_write_values(self): tst = "foo"*1025 - r = rparse.parse({}, "400'msg'") - s = cStringIO.StringIO() - r.write_values(s, [tst], []) + rparse.write_values(s, [tst], []) assert s.getvalue() == tst def test_write_values_pauses(self): tst = "".join(str(i) for i in range(10)) - r = rparse.parse({}, "400'msg'") - for i in range(2, 10): s = cStringIO.StringIO() - r.write_values(s, [tst], [(2, "pause", 0), (1, "pause", 0)], blocksize=i) + rparse.write_values(s, [tst], [(2, "pause", 0), (1, "pause", 0)], blocksize=i) assert s.getvalue() == tst for i in range(2, 10): s = cStringIO.StringIO() - r.write_values(s, [tst], [(1, "pause", 0)], blocksize=i) + rparse.write_values(s, [tst], [(1, "pause", 0)], blocksize=i) assert s.getvalue() == tst tst = ["".join(str(i) for i in range(10))]*5 for i in range(2, 10): s = cStringIO.StringIO() - r.write_values(s, tst[:], [(1, "pause", 0)], blocksize=i) + rparse.write_values(s, tst[:], [(1, "pause", 0)], blocksize=i) assert s.getvalue() == "".join(tst) + +def test_ready_actions(): + x = [(0, 5)] + assert rparse.ready_actions(100, x) == x + + x = [("r", 5)] + ret = rparse.ready_actions(100, x) + assert 0 <= ret[0][0] < 100 + + x = [("a", "pause", 5)] + ret = rparse.ready_actions(100, x) + assert ret[0][0] > 100 + + x = [(1, 5), (0, 5)] + assert rparse.ready_actions(100, x) == sorted(x) + + +class TestResponse: + def dummy_response(self): + return rparse.parse_response({}, "400'msg'") + + def test_response(self): + r = rparse.parse_response({}, "400'msg'") + assert r.code == 400 + assert r.msg == "msg" + + r = rparse.parse_response({}, "400'msg':b@100b") + assert r.msg == "msg" + assert r.body[:] + assert str(r) + def test_render(self): s = cStringIO.StringIO() - r = rparse.parse({}, "400'msg'") + r = rparse.parse_response({}, "400'msg'") assert r.serve(s) def test_length(self): @@ -288,6 +284,6 @@ class TestResponse: s = cStringIO.StringIO() x.serve(s) assert x.length() == len(s.getvalue()) - testlen(rparse.parse({}, "400'msg'")) - testlen(rparse.parse({}, "400'msg':h'foo'='bar'")) - testlen(rparse.parse({}, "400'msg':h'foo'='bar':b@100b")) + testlen(rparse.parse_response({}, "400'msg'")) + testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'")) + testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b")) |