2015-05-30 00:03:28 +00:00
|
|
|
import Queue
|
|
|
|
import time
|
|
|
|
import 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
|
2015-08-30 13:27:29 +00:00
|
|
|
|
2015-08-01 08:40:19 +00:00
|
|
|
import mock
|
2015-07-29 09:39:53 +00:00
|
|
|
|
2015-08-01 08:40:19 +00:00
|
|
|
import netlib.utils
|
2015-04-13 23:58:10 +00:00
|
|
|
from netlib import odict
|
2015-09-26 15:41:14 +00:00
|
|
|
from netlib.http import CONTENT_MISSING, Headers
|
|
|
|
from libmproxy import filt, controller, tnetstring, flow
|
2016-01-27 09:10:14 +00:00
|
|
|
from libmproxy.models import Error
|
|
|
|
from libmproxy.models import Flow
|
|
|
|
from libmproxy.models import HTTPFlow
|
|
|
|
from libmproxy.models import HTTPRequest
|
|
|
|
from libmproxy.models import HTTPResponse
|
2014-10-18 16:29:35 +00:00
|
|
|
from libmproxy.proxy.config import HostMatcher
|
2014-09-08 12:43:05 +00:00
|
|
|
from libmproxy.proxy import ProxyConfig
|
|
|
|
from libmproxy.proxy.server import DummyServer
|
2015-08-30 13:27:29 +00:00
|
|
|
from libmproxy.models.connections import ClientConnection
|
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
|
|
|
|
2014-01-04 01:35:11 +00:00
|
|
|
def test_app_registry():
|
|
|
|
ar = flow.AppRegistry()
|
|
|
|
ar.add("foo", "domain", 80)
|
|
|
|
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2014-09-03 14:57:56 +00:00
|
|
|
r.host = "domain"
|
|
|
|
r.port = 80
|
2014-01-04 01:35:11 +00:00
|
|
|
assert ar.get(r)
|
|
|
|
|
|
|
|
r.port = 81
|
|
|
|
assert not ar.get(r)
|
|
|
|
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2014-01-04 01:35:11 +00:00
|
|
|
r.host = "domain2"
|
|
|
|
r.port = 80
|
|
|
|
assert not ar.get(r)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["host"] = "domain"
|
2014-01-04 01:35:11 +00:00
|
|
|
assert ar.get(r)
|
|
|
|
|
|
|
|
|
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(".*"))
|
2015-08-01 08:40:19 +00:00
|
|
|
f = tutils.tflow(req=netlib.tutils.treq(host=host, port=80), resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
f.response.headers["Set-Cookie"] = cookie
|
2011-02-24 02:15:51 +00:00
|
|
|
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):
|
2015-02-07 00:17:24 +00:00
|
|
|
c = "SSID=mooo; domain=.google.com, FOO=bar; Domain=.google.com; Path=/; "\
|
2011-02-24 02:15:51 +00:00
|
|
|
"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(".*"))
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
f.request.headers["authorization"] = "foo"
|
2011-03-20 04:31:54 +00:00
|
|
|
s.handle_request(f)
|
2014-02-05 19:26:47 +00:00
|
|
|
assert "address" in s.hosts
|
2011-03-20 04:31:54 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2011-03-20 04:31:54 +00:00
|
|
|
s.handle_request(f)
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.request.headers["authorization"] == "foo"
|
2011-03-20 04:31:54 +00:00
|
|
|
|
|
|
|
|
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
|
2014-11-13 23:26:22 +00:00
|
|
|
c.testing = True
|
2011-03-06 03:54:49 +00:00
|
|
|
|
|
|
|
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
|
2014-11-13 23:26:22 +00:00
|
|
|
c.tick(fm)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert s.flow_count()
|
2011-03-04 00:08:43 +00:00
|
|
|
assert c.count() == 1
|
|
|
|
|
2014-11-13 23:26:22 +00:00
|
|
|
c.tick(fm)
|
2011-03-04 00:08:43 +00:00
|
|
|
assert c.count() == 1
|
|
|
|
|
2011-03-05 22:21:31 +00:00
|
|
|
c.clear(c.current)
|
2014-11-13 23:26:22 +00:00
|
|
|
c.tick(fm)
|
2011-03-04 00:08:43 +00:00
|
|
|
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()
|
2014-09-13 23:46:01 +00:00
|
|
|
fm.tick(q, timeout=0)
|
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):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
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)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["foo"] = "bar"
|
2011-02-20 19:47:19 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r.request.path = "voing"
|
|
|
|
assert s._hash(r) != s._hash(r2)
|
|
|
|
|
2015-04-03 05:10:01 +00:00
|
|
|
r.request.path = "path?blank_value"
|
|
|
|
r2.request.path = "path?"
|
|
|
|
assert s._hash(r) != s._hash(r2)
|
|
|
|
|
2011-02-22 21:54:51 +00:00
|
|
|
def test_headers(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
["foo"],
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False)
|
2014-09-03 14:57:56 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["foo"] = "bar"
|
2014-09-03 14:57:56 +00:00
|
|
|
r2 = tutils.tflow(resp=True)
|
2011-02-22 21:54:51 +00:00
|
|
|
assert not s._hash(r) == s._hash(r2)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["foo"] = "bar"
|
2011-02-22 21:54:51 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["oink"] = "bar"
|
2011-02-22 21:54:51 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
|
|
|
r2 = tutils.tflow(resp=True)
|
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):
|
2014-09-03 14:57:56 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["key"] = "one"
|
2011-02-20 19:47:19 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["key"] = "two"
|
2011-02-20 19:47:19 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None, [
|
|
|
|
r, r2], False, False, None, False, None, 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)
|
2015-09-05 18:45:58 +00:00
|
|
|
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)
|
2015-09-05 18:45:58 +00:00
|
|
|
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):
|
2014-09-03 14:57:56 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["key"] = "one"
|
2012-03-05 11:57:57 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["key"] = "two"
|
2012-03-05 11:57:57 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None, [
|
|
|
|
r, r2], False, True, None, False, None, False)
|
2012-03-05 11:57:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-03 10:29:44 +00:00
|
|
|
def test_ignore_params(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None, [], False, False, [
|
|
|
|
"param1", "param2"], False, None, False)
|
2014-10-03 10:29:44 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-05-30 00:03:28 +00:00
|
|
|
r.request.path = "/test?param1=1"
|
2014-10-03 10:29:44 +00:00
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.path = "/test"
|
2014-10-03 10:29:44 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.path = "/test?param1=2"
|
2014-10-03 10:29:44 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.path = "/test?param2=1"
|
2014-10-03 10:29:44 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.path = "/test?param3=2"
|
2014-10-03 10:29:44 +00:00
|
|
|
assert not s._hash(r) == s._hash(r2)
|
|
|
|
|
2014-12-18 20:56:27 +00:00
|
|
|
def test_ignore_payload_params(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None, [], False, False, None, False, [
|
|
|
|
"param1", "param2"], False)
|
2014-12-18 20:56:27 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
2014-12-18 20:56:27 +00:00
|
|
|
r.request.content = "paramx=x¶m1=1"
|
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
2014-12-18 20:56:27 +00:00
|
|
|
r2.request.content = "paramx=x¶m1=1"
|
2015-04-03 05:10:01 +00:00
|
|
|
# same parameters
|
2014-12-18 20:56:27 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-04-03 05:10:01 +00:00
|
|
|
# ignored parameters !=
|
2014-12-18 20:56:27 +00:00
|
|
|
r2.request.content = "paramx=x¶m1=2"
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-04-03 05:10:01 +00:00
|
|
|
# missing parameter
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.content = "paramx=x"
|
2014-12-18 20:56:27 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
# ignorable parameter added
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.content = "paramx=x¶m1=2"
|
2014-12-18 20:56:27 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
# not ignorable parameter changed
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.content = "paramx=y¶m1=1"
|
2014-12-18 20:56:27 +00:00
|
|
|
assert not s._hash(r) == s._hash(r2)
|
|
|
|
# not ignorable parameter missing
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.content = "param1=1"
|
2014-12-18 20:56:27 +00:00
|
|
|
assert not s._hash(r) == s._hash(r2)
|
|
|
|
|
|
|
|
def test_ignore_payload_params_other_content_type(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None, [], False, False, None, False, [
|
|
|
|
"param1", "param2"], False)
|
2014-12-18 20:56:27 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["Content-Type"] = "application/json"
|
2014-12-18 20:56:27 +00:00
|
|
|
r.request.content = '{"param1":"1"}'
|
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["Content-Type"] = "application/json"
|
2014-12-18 20:56:27 +00:00
|
|
|
r2.request.content = '{"param1":"1"}'
|
2015-04-03 05:10:01 +00:00
|
|
|
# same content
|
2014-12-18 20:56:27 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
# distint content (note only x-www-form-urlencoded payload is analysed)
|
|
|
|
r2.request.content = '{"param1":"2"}'
|
|
|
|
assert not s._hash(r) == s._hash(r2)
|
|
|
|
|
|
|
|
def test_ignore_payload_wins_over_params(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
# NOTE: parameters are mutually exclusive in options
|
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None, [], False, False, None, True, [
|
|
|
|
"param1", "param2"], False)
|
2014-12-18 20:56:27 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
2014-12-18 20:56:27 +00:00
|
|
|
r.request.content = "paramx=y"
|
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
2014-12-18 20:56:27 +00:00
|
|
|
r2.request.content = "paramx=x"
|
2015-04-03 05:10:01 +00:00
|
|
|
# same parameters
|
2014-12-18 20:56:27 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
|
2014-10-03 10:29:44 +00:00
|
|
|
def test_ignore_content(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False)
|
2014-10-03 10:29:44 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
|
|
|
r2 = tutils.tflow(resp=True)
|
2015-03-07 16:38:18 +00:00
|
|
|
|
2014-10-03 10:29:44 +00:00
|
|
|
r.request.content = "foo"
|
|
|
|
r2.request.content = "foo"
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r2.request.content = "bar"
|
|
|
|
assert not s._hash(r) == s._hash(r2)
|
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
# now ignoring content
|
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
False)
|
2014-10-03 10:29:44 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
|
|
|
r2 = tutils.tflow(resp=True)
|
|
|
|
r.request.content = "foo"
|
|
|
|
r2.request.content = "foo"
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r2.request.content = "bar"
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r2.request.content = ""
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
r2.request.content = None
|
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
|
2015-03-07 16:38:18 +00:00
|
|
|
def test_ignore_host(self):
|
2015-05-30 00:03:28 +00:00
|
|
|
s = flow.ServerPlaybackState(
|
|
|
|
None,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
True)
|
2015-03-07 16:38:18 +00:00
|
|
|
r = tutils.tflow(resp=True)
|
|
|
|
r2 = tutils.tflow(resp=True)
|
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
r.request.host = "address"
|
|
|
|
r2.request.host = "address"
|
2015-03-07 16:38:18 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
2015-05-30 00:03:28 +00:00
|
|
|
r2.request.host = "wrong_address"
|
2015-03-07 16:38:18 +00:00
|
|
|
assert s._hash(r) == s._hash(r2)
|
|
|
|
|
2014-10-03 10:29:44 +00:00
|
|
|
|
2015-09-25 23:23:59 +00:00
|
|
|
class TestFlow(object):
|
2012-02-18 10:56:40 +00:00
|
|
|
def test_copy(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2014-09-16 23:35:14 +00:00
|
|
|
a0 = f.get_state()
|
2012-02-18 10:56:40 +00:00
|
|
|
f2 = f.copy()
|
2014-09-16 23:35:14 +00:00
|
|
|
a = f.get_state()
|
|
|
|
b = f2.get_state()
|
2014-12-24 00:39:38 +00:00
|
|
|
del a["id"]
|
|
|
|
del b["id"]
|
|
|
|
assert a == b
|
2014-02-05 13:33:17 +00:00
|
|
|
assert not f == f2
|
2012-02-18 10:56:40 +00:00
|
|
|
assert not f is f2
|
2014-09-16 23:35:14 +00:00
|
|
|
assert f.request.get_state() == f2.request.get_state()
|
2012-02-18 10:56:40 +00:00
|
|
|
assert not f.request is f2.request
|
|
|
|
assert f.request.headers == f2.request.headers
|
|
|
|
assert not f.request.headers is f2.request.headers
|
2014-09-16 23:35:14 +00:00
|
|
|
assert f.response.get_state() == f2.response.get_state()
|
2012-02-24 23:19:54 +00:00
|
|
|
assert not f.response is f2.response
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(err=True)
|
2012-02-24 23:19:54 +00:00
|
|
|
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
|
2014-09-16 23:35:14 +00:00
|
|
|
assert f.error.get_state() == f2.error.get_state()
|
2012-02-24 23:19:54 +00:00
|
|
|
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):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2012-09-13 21:41:01 +00:00
|
|
|
assert not f.match("~b test")
|
2011-07-31 23:17:01 +00:00
|
|
|
assert f.match(None)
|
2012-09-13 21:41:01 +00:00
|
|
|
assert not f.match("~b test")
|
2012-02-10 02:31:45 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(err=True)
|
2012-09-13 21:41:01 +00:00
|
|
|
assert f.match("~e")
|
|
|
|
|
|
|
|
tutils.raises(ValueError, f.match, "~")
|
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_backup(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
2015-08-30 13:27:29 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp())
|
2011-02-05 21:28:43 +00:00
|
|
|
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):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2012-07-04 23:52:56 +00:00
|
|
|
f.backup()
|
|
|
|
f.revert()
|
|
|
|
f.backup()
|
|
|
|
f.revert()
|
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_getset_state(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2014-09-16 23:35:14 +00:00
|
|
|
state = f.get_state()
|
2015-08-30 13:27:29 +00:00
|
|
|
assert f.get_state() == HTTPFlow.from_state(
|
2015-05-30 00:03:28 +00:00
|
|
|
state).get_state()
|
2011-02-19 04:21:08 +00:00
|
|
|
|
|
|
|
f.response = None
|
2014-02-04 04:02:17 +00:00
|
|
|
f.error = Error("error")
|
2014-09-16 23:35:14 +00:00
|
|
|
state = f.get_state()
|
2015-08-30 13:27:29 +00:00
|
|
|
assert f.get_state() == HTTPFlow.from_state(
|
2015-05-30 00:03:28 +00:00
|
|
|
state).get_state()
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
f2 = f.copy()
|
2014-12-24 00:39:38 +00:00
|
|
|
f2.id = f.id # copy creates a different uuid
|
2014-09-16 23:35:14 +00:00
|
|
|
assert f.get_state() == f2.get_state()
|
2014-02-05 13:33:17 +00:00
|
|
|
assert not f == f2
|
2014-02-04 04:02:17 +00:00
|
|
|
f2.error = Error("e2")
|
2011-02-19 20:55:42 +00:00
|
|
|
assert not f == f2
|
2014-09-16 23:35:14 +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()
|
2014-12-23 19:33:42 +00:00
|
|
|
f.intercept(mock.Mock())
|
2014-09-03 14:57:56 +00:00
|
|
|
assert not f.reply.acked
|
2011-03-13 03:50:11 +00:00
|
|
|
f.kill(fm)
|
2014-09-03 14:57:56 +00:00
|
|
|
assert f.reply.acked
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2011-08-02 04:52:47 +00:00
|
|
|
def test_killall(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
fm.handle_request(f)
|
2011-08-02 04:52:47 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
fm.handle_request(f)
|
2011-08-02 04:52:47 +00:00
|
|
|
|
|
|
|
for i in s.view:
|
2014-09-03 14:57:56 +00:00
|
|
|
assert not i.reply.acked
|
2011-08-02 04:52:47 +00:00
|
|
|
s.killall(fm)
|
|
|
|
for i in s.view:
|
2014-09-03 14:57:56 +00:00
|
|
|
assert i.reply.acked
|
2011-08-02 04:52:47 +00:00
|
|
|
|
2011-01-30 22:48:53 +00:00
|
|
|
def test_accept_intercept(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
2014-09-03 14:57:56 +00:00
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
f.intercept(mock.Mock())
|
2014-09-03 14:57:56 +00:00
|
|
|
assert not f.reply.acked
|
2014-12-23 19:33:42 +00:00
|
|
|
f.accept_intercept(mock.Mock())
|
2014-09-03 14:57:56 +00:00
|
|
|
assert f.reply.acked
|
2011-01-30 22:48:53 +00:00
|
|
|
|
2012-05-26 01:10:31 +00:00
|
|
|
def test_replace_unicode(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2012-05-26 01:10:31 +00:00
|
|
|
f.response.content = "\xc2foo"
|
|
|
|
f.replace("foo", u"bar")
|
|
|
|
|
2011-07-22 05:48:42 +00:00
|
|
|
def test_replace(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
f.request.headers["foo"] = "foo"
|
2011-07-22 05:48:42 +00:00
|
|
|
f.request.content = "afoob"
|
|
|
|
|
2015-09-05 18:45:58 +00:00
|
|
|
f.response.headers["foo"] = "foo"
|
2011-07-22 05:48:42 +00:00
|
|
|
f.response.content = "afoob"
|
|
|
|
|
|
|
|
assert f.replace("foo", "bar") == 6
|
|
|
|
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.request.headers["bar"] == "bar"
|
2011-07-22 05:48:42 +00:00
|
|
|
assert f.request.content == "abarb"
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.response.headers["bar"] == "bar"
|
2011-07-22 05:48:42 +00:00
|
|
|
assert f.response.content == "abarb"
|
|
|
|
|
2012-03-15 22:24:18 +00:00
|
|
|
def test_replace_encoded(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2012-03-15 22:24:18 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
|
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()
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
c.add_flow(f)
|
2011-02-02 23:16:03 +00:00
|
|
|
f.backup()
|
|
|
|
c.revert(f)
|
|
|
|
|
|
|
|
def test_flow(self):
|
|
|
|
"""
|
|
|
|
normal flow:
|
|
|
|
|
|
|
|
connect -> request -> response
|
|
|
|
"""
|
|
|
|
c = flow.State()
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
c.add_flow(f)
|
2011-02-19 04:00:24 +00:00
|
|
|
assert f
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.flow_count() == 1
|
|
|
|
assert c.active_flow_count() == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
newf = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
assert c.add_flow(newf)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.active_flow_count() == 2
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2015-08-30 13:27:29 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp())
|
2014-11-26 03:18:21 +00:00
|
|
|
assert c.update_flow(f)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.flow_count() == 2
|
|
|
|
assert c.active_flow_count() == 1
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2015-08-30 13:27:29 +00:00
|
|
|
_ = HTTPResponse.wrap(netlib.tutils.tresp())
|
2014-11-26 03:18:21 +00:00
|
|
|
assert not c.update_flow(None)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert c.active_flow_count() == 1
|
|
|
|
|
2015-08-30 13:27:29 +00:00
|
|
|
newf.response = HTTPResponse.wrap(netlib.tutils.tresp())
|
2014-11-26 03:18:21 +00:00
|
|
|
assert c.update_flow(newf)
|
2011-03-06 03:11:45 +00:00
|
|
|
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()
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
c.add_flow(f)
|
2014-02-05 13:33:17 +00:00
|
|
|
f.error = Error("message")
|
2014-11-26 03:18:21 +00:00
|
|
|
assert c.update_flow(f)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
c = flow.State()
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
c.add_flow(f)
|
2012-02-10 02:31:45 +00:00
|
|
|
c.set_limit("~e")
|
|
|
|
assert not c.view
|
2014-09-03 14:57:56 +00:00
|
|
|
f.error = tutils.terr()
|
2014-11-26 03:18:21 +00:00
|
|
|
assert c.update_flow(f)
|
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()
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2011-02-19 04:00:24 +00:00
|
|
|
assert len(c.view) == 0
|
|
|
|
|
2014-11-26 03:18:21 +00:00
|
|
|
c.add_flow(f)
|
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
|
2015-08-30 13:27:29 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp())
|
2014-11-26 03:18:21 +00:00
|
|
|
c.update_flow(f)
|
2011-02-19 04:00:24 +00:00
|
|
|
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
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
c.add_flow(f)
|
2011-02-02 23:16:03 +00:00
|
|
|
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)
|
2015-05-30 00:03:28 +00:00
|
|
|
assert c.intercept_txt is None
|
2011-03-13 01:51:25 +00:00
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def _add_request(self, state):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
state.add_flow(f)
|
2011-02-02 23:16:03 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def _add_response(self, state):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
state.add_flow(f)
|
2015-08-30 13:27:29 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp())
|
2014-11-26 03:18:21 +00:00
|
|
|
state.update_flow(f)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
def _add_error(self, state):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(err=True)
|
2014-11-26 03:18:21 +00:00
|
|
|
state.add_flow(f)
|
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)
|
2014-12-23 19:33:42 +00:00
|
|
|
f.intercepted = True
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
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)
|
2014-11-26 03:18:21 +00:00
|
|
|
assert isinstance(c.flows[0], 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)
|
2014-12-23 19:33:42 +00:00
|
|
|
c.accept_all(mock.Mock())
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
|
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):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2011-05-14 23:22:35 +00:00
|
|
|
w.add(f)
|
|
|
|
for i in range(3):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(err=True)
|
2011-05-14 23:22:35 +00:00
|
|
|
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]
|
2014-09-16 23:35:14 +00:00
|
|
|
assert f2.get_state() == f.get_state()
|
2015-07-29 09:39:53 +00:00
|
|
|
assert f2.request == f.request
|
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)
|
2014-11-26 03:18:21 +00:00
|
|
|
assert len(s.flows) == 6
|
2013-03-17 04:31:35 +00:00
|
|
|
|
2014-09-08 12:43:05 +00:00
|
|
|
def test_load_flows_reverse(self):
|
|
|
|
r = self._treader()
|
|
|
|
s = flow.State()
|
2015-05-30 00:03:28 +00:00
|
|
|
conf = ProxyConfig(
|
|
|
|
mode="reverse",
|
2015-08-27 23:51:13 +00:00
|
|
|
upstream_server=("https", ("use-this-domain", 80))
|
|
|
|
)
|
2014-09-08 12:43:05 +00:00
|
|
|
fm = flow.FlowMaster(DummyServer(conf), s)
|
|
|
|
fm.load_flows(r)
|
2014-11-26 03:18:21 +00:00
|
|
|
assert s.flows[0].request.host == "use-this-domain"
|
2014-09-08 12:43:05 +00:00
|
|
|
|
2013-03-13 20:19:43 +00:00
|
|
|
def test_filter(self):
|
|
|
|
sio = StringIO()
|
|
|
|
fl = filt.parse("~c 200")
|
|
|
|
w = flow.FilteredFlowWriter(sio, fl)
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-17 13:16:33 +00:00
|
|
|
f.response.status_code = 200
|
2013-03-13 20:19:43 +00:00
|
|
|
w.add(f)
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-17 13:16:33 +00:00
|
|
|
f.response.status_code = 201
|
2013-03-13 20:19:43 +00:00
|
|
|
w.add(f)
|
|
|
|
|
|
|
|
sio.seek(0)
|
|
|
|
r = flow.FlowReader(sio)
|
|
|
|
assert len(list(r.stream()))
|
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()
|
2014-09-16 23:35:14 +00:00
|
|
|
d = f.get_state()
|
2012-04-10 22:10:53 +00:00
|
|
|
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)
|
2014-01-12 10:01:59 +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"))
|
2014-01-13 01:20:34 +00:00
|
|
|
assert not fm.unload_scripts()
|
2014-01-12 10:01:59 +00:00
|
|
|
assert fm.load_script("nonexistent")
|
2015-05-30 00:03:28 +00:00
|
|
|
assert "ValueError" in fm.load_script(
|
|
|
|
tutils.test_data.path("scripts/starterr.py"))
|
2013-06-13 14:04:04 +00:00
|
|
|
assert len(fm.scripts) == 0
|
2011-08-03 04:36:20 +00:00
|
|
|
|
2014-09-08 10:20:40 +00:00
|
|
|
def test_getset_ignore(self):
|
|
|
|
p = mock.Mock()
|
2014-10-18 16:29:35 +00:00
|
|
|
p.config.check_ignore = HostMatcher()
|
2014-09-08 10:20:40 +00:00
|
|
|
fm = flow.FlowMaster(p, flow.State())
|
2014-10-18 16:29:35 +00:00
|
|
|
assert not fm.get_ignore_filter()
|
|
|
|
fm.set_ignore_filter(["^apple\.com:", ":443$"])
|
|
|
|
assert fm.get_ignore_filter()
|
2014-09-08 10:20:40 +00:00
|
|
|
|
2012-05-16 06:24:32 +00:00
|
|
|
def test_replay(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2014-03-09 20:51:24 +00:00
|
|
|
f.request.content = CONTENT_MISSING
|
2012-05-16 06:24:32 +00:00
|
|
|
assert "missing" in fm.replay_request(f)
|
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
f.intercepted = True
|
2012-05-16 06:24:32 +00:00
|
|
|
assert "intercepting" in fm.replay_request(f)
|
|
|
|
|
2015-02-07 18:33:36 +00:00
|
|
|
f.live = True
|
|
|
|
assert "live" in fm.replay_request(f, run_scripthooks=True)
|
|
|
|
|
2012-02-10 02:55:58 +00:00
|
|
|
def test_script_reqerr(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2014-01-12 10:01:59 +00:00
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py"))
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
fm.handle_clientconnect(f.client_conn)
|
|
|
|
assert fm.handle_request(f)
|
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)
|
2014-01-12 10:01:59 +00:00
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
|
|
|
|
|
|
|
fm.handle_clientconnect(f.client_conn)
|
2013-06-13 14:04:04 +00:00
|
|
|
assert fm.scripts[0].ns["log"][-1] == "clientconnect"
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_serverconnect(f.server_conn)
|
2013-12-16 21:10:06 +00:00
|
|
|
assert fm.scripts[0].ns["log"][-1] == "serverconnect"
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
2013-06-13 14:04:04 +00:00
|
|
|
assert fm.scripts[0].ns["log"][-1] == "request"
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_response(f)
|
2013-06-13 14:04:04 +00:00
|
|
|
assert fm.scripts[0].ns["log"][-1] == "response"
|
2015-05-30 00:03:28 +00:00
|
|
|
# load second script
|
2014-01-12 10:01:59 +00:00
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
|
2013-06-13 14:04:04 +00:00
|
|
|
assert len(fm.scripts) == 2
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_clientdisconnect(f.server_conn)
|
2013-06-13 14:04:04 +00:00
|
|
|
assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
|
|
|
|
assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"
|
2014-01-13 01:20:34 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
# unload first script
|
2014-01-13 01:20:34 +00:00
|
|
|
fm.unload_scripts()
|
|
|
|
assert len(fm.scripts) == 0
|
|
|
|
assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
|
2014-09-03 14:57:56 +00:00
|
|
|
|
|
|
|
f.error = tutils.terr()
|
|
|
|
fm.handle_error(f)
|
2013-06-13 14:04:04 +00:00
|
|
|
assert fm.scripts[0].ns["log"][-1] == "error"
|
2011-08-03 04:36:20 +00:00
|
|
|
|
2012-02-18 10:56:40 +00:00
|
|
|
def test_duplicate_flow(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2013-07-28 06:00:49 +00:00
|
|
|
f = fm.load_flow(f)
|
2012-02-18 10:56:40 +00:00
|
|
|
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
|
2014-02-05 13:33:17 +00:00
|
|
|
assert s.index(f2) == 1
|
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
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(req=None)
|
|
|
|
fm.handle_clientconnect(f.client_conn)
|
2015-08-30 13:27:29 +00:00
|
|
|
f.request = HTTPRequest.wrap(netlib.tutils.treq())
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert s.flow_count() == 1
|
2011-02-19 04:00:24 +00:00
|
|
|
|
2015-08-30 13:27:29 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp())
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_response(f)
|
|
|
|
assert not fm.handle_response(None)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert s.flow_count() == 1
|
2011-02-19 04:21:08 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_clientdisconnect(f.client_conn)
|
2011-02-19 04:21:08 +00:00
|
|
|
|
2014-02-05 13:33:17 +00:00
|
|
|
f.error = Error("msg")
|
|
|
|
f.error.reply = controller.DummyReply()
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_error(f)
|
2011-02-19 04:21:08 +00:00
|
|
|
|
2014-01-12 10:01:59 +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()
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
|
|
|
pb = [tutils.tflow(resp=True), f]
|
2014-11-11 12:09:05 +00:00
|
|
|
fm = flow.FlowMaster(DummyServer(ProxyConfig()), s)
|
2015-05-30 00:03:28 +00:00
|
|
|
assert not fm.start_server_playback(
|
|
|
|
pb,
|
|
|
|
False,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False)
|
2011-03-06 03:54:49 +00:00
|
|
|
assert not fm.start_client_playback(pb, False)
|
2014-11-13 23:26:22 +00:00
|
|
|
fm.client_playback.testing = True
|
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()
|
2014-09-13 23:46:01 +00:00
|
|
|
fm.tick(q, 0)
|
2011-03-06 03:11:45 +00:00
|
|
|
assert fm.state.flow_count()
|
2011-03-06 03:02:28 +00:00
|
|
|
|
2014-02-05 13:33:17 +00:00
|
|
|
f.error = Error("error")
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_error(f)
|
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()
|
2015-09-26 15:41:14 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=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
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
fm.start_server_playback(
|
|
|
|
pb,
|
|
|
|
False,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False)
|
2011-03-05 02:58:48 +00:00
|
|
|
assert fm.do_server_playback(tutils.tflow())
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
fm.start_server_playback(
|
|
|
|
pb,
|
|
|
|
False,
|
|
|
|
[],
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
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())
|
2013-01-05 08:41:16 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
fm.start_server_playback(
|
|
|
|
pb,
|
|
|
|
False,
|
|
|
|
[],
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False)
|
2011-03-06 04:08:56 +00:00
|
|
|
q = Queue.Queue()
|
2014-09-13 23:46:01 +00:00
|
|
|
fm.tick(q, 0)
|
2014-06-13 07:14:55 +00:00
|
|
|
assert fm.should_exit.is_set()
|
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
|
|
|
|
|
2013-01-05 08:41:16 +00:00
|
|
|
def test_server_playback_kill(self):
|
|
|
|
s = flow.State()
|
|
|
|
f = tutils.tflow()
|
2015-09-26 15:41:14 +00:00
|
|
|
f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request))
|
2013-01-05 08:41:16 +00:00
|
|
|
pb = [f]
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
fm.refresh_server_playback = True
|
2015-05-30 00:03:28 +00:00
|
|
|
fm.start_server_playback(
|
|
|
|
pb,
|
|
|
|
True,
|
|
|
|
[],
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
None,
|
|
|
|
False)
|
2013-01-05 08:41:16 +00:00
|
|
|
|
|
|
|
f = tutils.tflow()
|
|
|
|
f.request.host = "nonexistent"
|
|
|
|
fm.process_new_request(f)
|
|
|
|
assert "killed" in f.error.msg
|
|
|
|
|
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(".*")
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
f.response.headers["set-cookie"] = "foo=bar"
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
|
|
|
fm.handle_response(f)
|
2011-02-24 02:15:51 +00:00
|
|
|
assert fm.stickycookie_state.jar
|
2014-09-03 14:57:56 +00:00
|
|
|
assert not "cookie" in f.request.headers
|
|
|
|
f = f.copy()
|
|
|
|
fm.handle_request(f)
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.request.headers["cookie"] == "foo=bar"
|
2011-02-24 02:15:51 +00:00
|
|
|
|
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(".*")
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
f.request.headers["authorization"] = "foo"
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
2011-03-20 04:31:54 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2011-03-20 04:31:54 +00:00
|
|
|
assert fm.stickyauth_state.hosts
|
|
|
|
assert not "authorization" in f.request.headers
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.request.headers["authorization"] == "foo"
|
2011-03-20 04:31:54 +00:00
|
|
|
|
2012-07-08 22:18:37 +00:00
|
|
|
def test_stream(self):
|
|
|
|
with tutils.tmpdir() as tdir:
|
|
|
|
p = os.path.join(tdir, "foo")
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2012-07-08 22:18:37 +00:00
|
|
|
def r():
|
2015-05-30 00:03:28 +00:00
|
|
|
r = flow.FlowReader(open(p, "rb"))
|
2012-07-08 22:18:37 +00:00
|
|
|
return list(r.stream())
|
|
|
|
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2012-07-08 22:18:37 +00:00
|
|
|
|
2013-03-13 20:19:43 +00:00
|
|
|
fm.start_stream(file(p, "ab"), None)
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
|
|
|
fm.handle_response(f)
|
2012-07-08 22:18:37 +00:00
|
|
|
fm.stop_stream()
|
|
|
|
|
|
|
|
assert r()[0].response
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2013-03-13 20:19:43 +00:00
|
|
|
fm.start_stream(file(p, "ab"), None)
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f)
|
2012-07-08 22:18:37 +00:00
|
|
|
fm.shutdown()
|
|
|
|
|
|
|
|
assert not r()[1].response
|
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestRequest:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_simple(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
r = f.request
|
2014-09-03 21:44:54 +00:00
|
|
|
u = r.url
|
|
|
|
r.url = u
|
|
|
|
tutils.raises(ValueError, setattr, r, "url", "")
|
|
|
|
assert r.url == u
|
2011-08-03 10:38:23 +00:00
|
|
|
r2 = r.copy()
|
2014-09-16 23:35:14 +00:00
|
|
|
assert r.get_state() == r2.get_state()
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2013-03-17 04:31:35 +00:00
|
|
|
def test_get_url(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2014-02-05 19:26:47 +00:00
|
|
|
|
2014-09-03 21:44:54 +00:00
|
|
|
assert r.url == "http://address:22/path"
|
2014-02-05 19:26:47 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
r.scheme = "https"
|
2014-09-03 21:44:54 +00:00
|
|
|
assert r.url == "https://address:22/path"
|
2014-02-05 19:26:47 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
r.host = "host"
|
|
|
|
r.port = 42
|
2014-09-03 21:44:54 +00:00
|
|
|
assert r.url == "https://host:42/path"
|
2014-02-05 19:26:47 +00:00
|
|
|
|
|
|
|
r.host = "address"
|
|
|
|
r.port = 22
|
2015-05-30 00:03:28 +00:00
|
|
|
assert r.url == "https://address:22/path"
|
2014-02-05 19:26:47 +00:00
|
|
|
|
2015-09-25 23:23:59 +00:00
|
|
|
assert r.pretty_url == "https://address:22/path"
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["Host"] = "foo.com"
|
2015-09-25 23:23:59 +00:00
|
|
|
assert r.url == "https://address:22/path"
|
|
|
|
assert r.pretty_url == "https://foo.com:22/path"
|
2013-03-17 04:31:35 +00:00
|
|
|
|
2012-08-19 01:03:21 +00:00
|
|
|
def test_path_components(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2014-02-05 13:33:17 +00:00
|
|
|
r.path = "/"
|
2014-09-05 13:16:20 +00:00
|
|
|
assert r.get_path_components() == []
|
2014-02-05 13:33:17 +00:00
|
|
|
r.path = "/foo/bar"
|
2014-09-05 13:16:20 +00:00
|
|
|
assert r.get_path_components() == ["foo", "bar"]
|
2015-04-13 23:58:10 +00:00
|
|
|
q = odict.ODict()
|
2012-08-19 01:03:21 +00:00
|
|
|
q["test"] = ["123"]
|
2014-09-05 13:16:20 +00:00
|
|
|
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"]
|
2012-08-19 01:03:21 +00:00
|
|
|
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):
|
2015-04-13 23:58:10 +00:00
|
|
|
d = odict.ODict([("one", "two"), ("three", "four")])
|
2015-09-25 23:23:59 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq(content=netlib.utils.urlencode(d.lst)))
|
2015-09-26 15:41:14 +00:00
|
|
|
r.headers["content-type"] = "application/x-www-form-urlencoded"
|
2014-09-05 13:16:20 +00:00
|
|
|
assert r.get_form_urlencoded() == d
|
2012-02-10 01:27:39 +00:00
|
|
|
|
2015-04-13 23:58:10 +00:00
|
|
|
d = odict.ODict([("x", "y")])
|
2014-09-05 13:16:20 +00:00
|
|
|
r.set_form_urlencoded(d)
|
|
|
|
assert r.get_form_urlencoded() == d
|
2012-02-10 01:27:39 +00:00
|
|
|
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["content-type"] = "foo"
|
2014-09-05 13:16:20 +00:00
|
|
|
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):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2014-09-03 21:44:54 +00:00
|
|
|
r.path = "/foo?x=y&a=b"
|
2014-09-05 13:16:20 +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
|
|
|
|
2014-09-03 21:44:54 +00:00
|
|
|
r.path = "/"
|
2014-09-05 13:16:20 +00:00
|
|
|
q = r.get_query()
|
2012-02-09 03:40:31 +00:00
|
|
|
assert not q
|
|
|
|
|
2014-09-03 21:44:54 +00:00
|
|
|
r.path = "/?adsfa"
|
2014-09-05 13:16:20 +00:00
|
|
|
q = r.get_query()
|
2013-02-11 02:22:25 +00:00
|
|
|
assert q.lst == [("adsfa", "")]
|
2012-02-09 03:40:31 +00:00
|
|
|
|
2014-09-03 21:44:54 +00:00
|
|
|
r.path = "/foo?x=y&a=b"
|
2014-09-05 13:16:20 +00:00
|
|
|
assert r.get_query()
|
2015-04-13 23:58:10 +00:00
|
|
|
r.set_query(odict.ODict([]))
|
2014-09-05 13:16:20 +00:00
|
|
|
assert not r.get_query()
|
2015-04-13 23:58:10 +00:00
|
|
|
qv = odict.ODict([("a", "b"), ("c", "d")])
|
2014-09-05 13:16:20 +00:00
|
|
|
r.set_query(qv)
|
|
|
|
assert r.get_query() == qv
|
2012-02-09 03:40:31 +00:00
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_anticache(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers = Headers()
|
|
|
|
r.headers["if-modified-since"] = "test"
|
|
|
|
r.headers["if-none-match"] = "test"
|
2011-08-03 10:38:23 +00:00
|
|
|
r.anticache()
|
|
|
|
assert not "if-modified-since" in r.headers
|
|
|
|
assert not "if-none-match" in r.headers
|
|
|
|
|
|
|
|
def test_replace(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2011-08-03 10:38:23 +00:00
|
|
|
r.path = "path/foo"
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["Foo"] = "fOo"
|
2011-08-03 10:38:23 +00:00
|
|
|
r.content = "afoob"
|
|
|
|
assert r.replace("foo(?i)", "boo") == 4
|
|
|
|
assert r.path == "path/boo"
|
|
|
|
assert not "foo" in r.content
|
2015-09-05 18:45:58 +00:00
|
|
|
assert r.headers["boo"] == "boo"
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
def test_constrain_encoding(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["accept-encoding"] = "gzip, oink"
|
|
|
|
r.constrain_encoding()
|
|
|
|
assert "oink" not in r.headers["accept-encoding"]
|
|
|
|
|
|
|
|
r.headers.set_all("accept-encoding", ["gzip", "oink"])
|
2012-02-10 02:04:20 +00:00
|
|
|
r.constrain_encoding()
|
|
|
|
assert "oink" not in r.headers["accept-encoding"]
|
|
|
|
|
2013-01-28 21:41:45 +00:00
|
|
|
def test_get_decoded_content(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPRequest.wrap(netlib.tutils.treq())
|
2013-01-28 21:41:45 +00:00
|
|
|
r.content = None
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["content-encoding"] = "identity"
|
2013-01-28 21:41:45 +00:00
|
|
|
assert r.get_decoded_content() == None
|
|
|
|
|
|
|
|
r.content = "falafel"
|
|
|
|
r.encode("gzip")
|
|
|
|
assert r.get_decoded_content() == "falafel"
|
|
|
|
|
2013-01-28 15:37:25 +00:00
|
|
|
def test_get_content_type(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
resp = HTTPResponse.wrap(netlib.tutils.tresp())
|
2015-09-05 18:45:58 +00:00
|
|
|
resp.headers = Headers(content_type="text/plain")
|
|
|
|
assert resp.headers["content-type"] == "text/plain"
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestResponse:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_simple(self):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2014-02-05 13:33:17 +00:00
|
|
|
resp = f.response
|
2011-08-03 10:38:23 +00:00
|
|
|
resp2 = resp.copy()
|
2014-09-16 23:35:14 +00:00
|
|
|
assert resp2.get_state() == resp.get_state()
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
def test_refresh(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPResponse.wrap(netlib.tutils.tresp())
|
2011-08-03 10:38:23 +00:00
|
|
|
n = time.time()
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["date"] = email.utils.formatdate(n)
|
2011-08-03 10:38:23 +00:00
|
|
|
pre = r.headers["date"]
|
|
|
|
r.refresh(n)
|
|
|
|
assert pre == r.headers["date"]
|
2015-05-30 00:03:28 +00:00
|
|
|
r.refresh(n + 60)
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2015-09-05 18:45:58 +00:00
|
|
|
d = email.utils.parsedate_tz(r.headers["date"])
|
2011-08-03 10:38:23 +00:00
|
|
|
d = email.utils.mktime_tz(d)
|
|
|
|
# Weird that this is not exact...
|
2015-05-30 00:03:28 +00:00
|
|
|
assert abs(60 - (d - n)) <= 1
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["set-cookie"] = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
|
2011-08-03 10:38:23 +00:00
|
|
|
r.refresh()
|
|
|
|
|
|
|
|
def test_refresh_cookie(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPResponse.wrap(netlib.tutils.tresp())
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2015-09-28 12:55:13 +00:00
|
|
|
# https://github.com/mitmproxy/mitmproxy/issues/773
|
|
|
|
c = ">=A"
|
|
|
|
with tutils.raises(ValueError):
|
|
|
|
r._refresh_cookie(c, 60)
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_replace(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
r = HTTPResponse.wrap(netlib.tutils.tresp())
|
2015-09-05 18:45:58 +00:00
|
|
|
r.headers["Foo"] = "fOo"
|
2011-08-03 10:38:23 +00:00
|
|
|
r.content = "afoob"
|
|
|
|
assert r.replace("foo(?i)", "boo") == 3
|
|
|
|
assert not "foo" in r.content
|
2015-09-05 18:45:58 +00:00
|
|
|
assert r.headers["boo"] == "boo"
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2013-01-28 15:37:25 +00:00
|
|
|
def test_get_content_type(self):
|
2015-08-30 13:27:29 +00:00
|
|
|
resp = HTTPResponse.wrap(netlib.tutils.tresp())
|
2015-09-05 18:45:58 +00:00
|
|
|
resp.headers = Headers(content_type="text/plain")
|
|
|
|
assert resp.headers["content-type"] == "text/plain"
|
2013-01-28 15:37:25 +00:00
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestError:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_getset_state(self):
|
2014-02-04 04:02:17 +00:00
|
|
|
e = Error("Error")
|
2014-09-16 23:35:14 +00:00
|
|
|
state = e.get_state()
|
|
|
|
assert Error.from_state(state).get_state() == e.get_state()
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
assert e.copy()
|
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
e2 = Error("bar")
|
2011-08-03 10:38:23 +00:00
|
|
|
assert not e == e2
|
2014-09-16 23:35:14 +00:00
|
|
|
e.load_state(e2.get_state())
|
|
|
|
assert e.get_state() == e2.get_state()
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
e3 = e.copy()
|
2014-09-16 23:35:14 +00:00
|
|
|
assert e3.get_state() == e.get_state()
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
class TestClientConnection:
|
2011-08-03 10:38:23 +00:00
|
|
|
def test_state(self):
|
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
c = tutils.tclient_conn()
|
2014-09-16 23:35:14 +00:00
|
|
|
assert ClientConnection.from_state(c.get_state()).get_state() ==\
|
2015-05-30 00:03:28 +00:00
|
|
|
c.get_state()
|
2014-02-04 04:02:17 +00:00
|
|
|
|
|
|
|
c2 = tutils.tclient_conn()
|
|
|
|
c2.address.address = (c2.address.host, 4242)
|
2011-08-03 10:38:23 +00:00
|
|
|
assert not c == c2
|
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
c2.timestamp_start = 42
|
2014-09-16 23:35:14 +00:00
|
|
|
c.load_state(c2.get_state())
|
2014-02-04 04:02:17 +00:00
|
|
|
assert c.timestamp_start == 42
|
2011-08-03 10:38:23 +00:00
|
|
|
|
|
|
|
c3 = c.copy()
|
2014-09-16 23:35:14 +00:00
|
|
|
assert c3.get_state() == c.get_state()
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2013-04-29 21:13:33 +00:00
|
|
|
assert str(c)
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
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"
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2012-06-09 01:42:43 +00:00
|
|
|
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")
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow(resp=True)
|
2015-09-05 18:45:58 +00:00
|
|
|
f.request.headers["one"] = "xxx"
|
|
|
|
f.response.headers["one"] = "xxx"
|
2012-08-18 11:39:52 +00:00
|
|
|
h.run(f)
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.request.headers["one"] == "xxx"
|
|
|
|
assert f.response.headers.get_all("one") == ["two", "three"]
|
2012-08-18 11:39:52 +00:00
|
|
|
|
|
|
|
h.clear()
|
|
|
|
h.add("~q", "one", "two")
|
|
|
|
h.add("~q", "one", "three")
|
|
|
|
f = tutils.tflow()
|
2015-09-05 18:45:58 +00:00
|
|
|
f.request.headers["one"] = "xxx"
|
2012-08-18 11:39:52 +00:00
|
|
|
h.run(f)
|
2015-09-05 18:45:58 +00:00
|
|
|
assert f.request.headers.get_all("one") == ["two", "three"]
|
2012-08-18 11:39:52 +00:00
|
|
|
|
|
|
|
assert not h.add("~", "foo", "bar")
|