aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/test_examples.py90
-rw-r--r--test/mitmproxy/test_server.py16
-rw-r--r--test/netlib/http/test_cookies.py68
3 files changed, 147 insertions, 27 deletions
diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py
index 6c24ace5..83a37a36 100644
--- a/test/mitmproxy/test_examples.py
+++ b/test/mitmproxy/test_examples.py
@@ -1,16 +1,20 @@
import json
+import os
import six
-import sys
-import os.path
-from mitmproxy.flow import master
-from mitmproxy.flow import state
+
from mitmproxy import options
from mitmproxy import contentviews
from mitmproxy.builtins import script
+from mitmproxy.flow import master
+from mitmproxy.flow import state
+
import netlib.utils
+
from netlib import tutils as netutils
from netlib.http import Headers
+from netlib.http import cookies
+
from . import tutils, mastertest
example_dir = netlib.utils.Data(__name__).push("../../examples")
@@ -98,30 +102,66 @@ class TestScripts(mastertest.MasterTest):
m.request(f)
assert f.request.host == "mitmproxy.org"
- def test_har_extractor(self):
- if sys.version_info >= (3, 0):
- with tutils.raises("does not work on Python 3"):
- tscript("har_extractor.py")
- return
+class TestHARDump():
+
+ def flow(self, resp_content=b'message'):
+ times = dict(
+ timestamp_start=746203272,
+ timestamp_end=746203272,
+ )
+
+ # Create a dummy flow for testing
+ return tutils.tflow(
+ req=netutils.treq(method=b'GET', **times),
+ resp=netutils.tresp(content=resp_content, **times)
+ )
+
+ def test_no_file_arg(self):
with tutils.raises(ScriptError):
- tscript("har_extractor.py")
+ tscript("har_dump.py")
+
+ def test_simple(self):
+ with tutils.tmpdir() as tdir:
+ path = os.path.join(tdir, "somefile")
+
+ m, sc = tscript("har_dump.py", six.moves.shlex_quote(path))
+ m.addons.invoke(m, "response", self.flow())
+ m.addons.remove(sc)
+ with open(path, "r") as inp:
+ har = json.load(inp)
+
+ assert len(har["log"]["entries"]) == 1
+
+ def test_base64(self):
with tutils.tmpdir() as tdir:
- times = dict(
- timestamp_start=746203272,
- timestamp_end=746203272,
- )
-
- path = os.path.join(tdir, "file")
- m, sc = tscript("har_extractor.py", six.moves.shlex_quote(path))
- f = tutils.tflow(
- req=netutils.treq(**times),
- resp=netutils.tresp(**times)
- )
- m.response(f)
+ path = os.path.join(tdir, "somefile")
+
+ m, sc = tscript("har_dump.py", six.moves.shlex_quote(path))
+ m.addons.invoke(m, "response", self.flow(resp_content=b"foo" + b"\xFF" * 10))
m.addons.remove(sc)
- with open(path, "rb") as f:
- test_data = json.load(f)
- assert len(test_data["log"]["pages"]) == 1
+ with open(path, "r") as inp:
+ har = json.load(inp)
+
+ assert har["log"]["entries"][0]["response"]["content"]["encoding"] == "base64"
+
+ def test_format_cookies(self):
+ m, sc = tscript("har_dump.py", "-")
+ format_cookies = sc.ns.ns["format_cookies"]
+
+ CA = cookies.CookieAttrs
+
+ f = format_cookies([("n", "v", CA([("k", "v")]))])[0]
+ assert f['name'] == "n"
+ assert f['value'] == "v"
+ assert not f['httpOnly']
+ assert not f['secure']
+
+ f = format_cookies([("n", "v", CA([("httponly", None), ("secure", None)]))])[0]
+ assert f['httpOnly']
+ assert f['secure']
+
+ f = format_cookies([("n", "v", CA([("expires", "Mon, 24-Aug-2037 00:00:00 GMT")]))])[0]
+ assert f['expires']
diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py
index 78e9b5c7..78a97dc3 100644
--- a/test/mitmproxy/test_server.py
+++ b/test/mitmproxy/test_server.py
@@ -313,6 +313,22 @@ class TestHTTPAuth(tservers.HTTPProxyTest):
assert ret.status_code == 202
+class TestHTTPReverseAuth(tservers.ReverseProxyTest):
+ def test_auth(self):
+ self.master.options.auth_singleuser = "test:test"
+ assert self.pathod("202").status_code == 401
+ p = self.pathoc()
+ ret = p.request("""
+ get
+ '/p/202'
+ h'%s'='%s'
+ """ % (
+ http.authentication.BasicWebsiteAuth.AUTH_HEADER,
+ authentication.assemble_http_basic_auth("basic", "test", "test")
+ ))
+ assert ret.status_code == 202
+
+
class TestHTTPS(tservers.HTTPProxyTest, CommonMixin, TcpMixin):
ssl = True
ssloptions = pathod.SSLOptions(request_client_cert=True)
diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py
index 17e21b94..efd8ba80 100644
--- a/test/netlib/http/test_cookies.py
+++ b/test/netlib/http/test_cookies.py
@@ -1,6 +1,10 @@
+import time
+
from netlib.http import cookies
from netlib.tutils import raises
+import mock
+
def test_read_token():
tokens = [
@@ -247,6 +251,22 @@ def test_refresh_cookie():
assert cookies.refresh_set_cookie_header(c, 0)
+@mock.patch('time.time')
+def test_get_expiration_ts(*args):
+ # Freeze time
+ now_ts = 17
+ time.time.return_value = now_ts
+
+ CA = cookies.CookieAttrs
+ F = cookies.get_expiration_ts
+
+ assert F(CA([("Expires", "Thu, 01-Jan-1970 00:00:00 GMT")])) == 0
+ assert F(CA([("Expires", "Mon, 24-Aug-2037 00:00:00 GMT")])) == 2134684800
+
+ assert F(CA([("Max-Age", "0")])) == now_ts
+ assert F(CA([("Max-Age", "31")])) == now_ts + 31
+
+
def test_is_expired():
CA = cookies.CookieAttrs
@@ -260,9 +280,53 @@ def test_is_expired():
# or both
assert cookies.is_expired(CA([("Expires", "Thu, 01-Jan-1970 00:00:00 GMT"), ("Max-Age", "0")]))
- assert not cookies.is_expired(CA([("Expires", "Thu, 24-Aug-2063 00:00:00 GMT")]))
+ assert not cookies.is_expired(CA([("Expires", "Mon, 24-Aug-2037 00:00:00 GMT")]))
assert not cookies.is_expired(CA([("Max-Age", "1")]))
- assert not cookies.is_expired(CA([("Expires", "Thu, 15-Jul-2068 00:00:00 GMT"), ("Max-Age", "1")]))
+ assert not cookies.is_expired(CA([("Expires", "Wed, 15-Jul-2037 00:00:00 GMT"), ("Max-Age", "1")]))
assert not cookies.is_expired(CA([("Max-Age", "nan")]))
assert not cookies.is_expired(CA([("Expires", "false")]))
+
+
+def test_group_cookies():
+ CA = cookies.CookieAttrs
+ groups = [
+ [
+ "one=uno; foo=bar; foo=baz",
+ [
+ ('one', 'uno', CA([])),
+ ('foo', 'bar', CA([])),
+ ('foo', 'baz', CA([]))
+ ]
+ ],
+ [
+ "one=uno; Path=/; foo=bar; Max-Age=0; foo=baz; expires=24-08-1993",
+ [
+ ('one', 'uno', CA([('Path', '/')])),
+ ('foo', 'bar', CA([('Max-Age', '0')])),
+ ('foo', 'baz', CA([('expires', '24-08-1993')]))
+ ]
+ ],
+ [
+ "one=uno;",
+ [
+ ('one', 'uno', CA([]))
+ ]
+ ],
+ [
+ "one=uno; Path=/; Max-Age=0; Expires=24-08-1993",
+ [
+ ('one', 'uno', CA([('Path', '/'), ('Max-Age', '0'), ('Expires', '24-08-1993')]))
+ ]
+ ],
+ [
+ "path=val; Path=/",
+ [
+ ('path', 'val', CA([('Path', '/')]))
+ ]
+ ]
+ ]
+
+ for c, expected in groups:
+ observed = cookies.group_cookies(cookies.parse_cookie_header(c))
+ assert observed == expected