aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/data/har_extractor.har78
-rw-r--r--test/mitmproxy/test_cmdline.py11
-rw-r--r--test/mitmproxy/test_flow_export.py31
-rw-r--r--test/mitmproxy/test_har_extractor.py37
-rw-r--r--test/mitmproxy/test_proxy.py4
5 files changed, 161 insertions, 0 deletions
diff --git a/test/mitmproxy/data/har_extractor.har b/test/mitmproxy/data/har_extractor.har
new file mode 100644
index 00000000..2f5099b3
--- /dev/null
+++ b/test/mitmproxy/data/har_extractor.har
@@ -0,0 +1,78 @@
+{
+ "test_response": {
+ "log": {
+ "__page_count__": 1,
+ "version": "1.2",
+ "creator": {
+ "comment": "",
+ "version": "0.1",
+ "name": "MITMPROXY HARExtractor"
+ },
+ "pages": [
+ {
+ "startedDateTime": "1993-08-24T14:41:12",
+ "id": "autopage_1",
+ "title": "http://address:22/path"
+ }
+ ],
+ "entries": [
+ {
+ "pageref": "autopage_1",
+ "startedDateTime": "1993-08-24T14:41:12",
+ "cache": {},
+ "request": {
+ "cookies": [],
+ "url": "http://address:22/path",
+ "queryString": [],
+ "headers": [
+ {
+ "name": "header",
+ "value": "qvalue"
+ },
+ {
+ "name": "content-length",
+ "value": "7"
+ }
+ ],
+ "headersSize": 35,
+ "httpVersion": "HTTP/1.1",
+ "method": "GET",
+ "bodySize": 7
+ },
+ "timings": {
+ "receive": 0,
+ "ssl": 1000,
+ "connect": 1000,
+ "send": 0,
+ "wait": 0
+ },
+ "time": 2000,
+ "response": {
+ "status": 200,
+ "cookies": [],
+ "statusText": "OK",
+ "content": {
+ "mimeType": "",
+ "compression": 0,
+ "size": 7
+ },
+ "headers": [
+ {
+ "name": "content-length",
+ "value": "7"
+ },
+ {
+ "name": "header-response",
+ "value": "svalue"
+ }
+ ],
+ "headersSize": 44,
+ "redirectURL": "",
+ "httpVersion": "HTTP/1.1",
+ "bodySize": 7
+ }
+ }
+ ]
+ }
+ }
+} \ No newline at end of file
diff --git a/test/mitmproxy/test_cmdline.py b/test/mitmproxy/test_cmdline.py
index 5a70f3e0..e75b7baf 100644
--- a/test/mitmproxy/test_cmdline.py
+++ b/test/mitmproxy/test_cmdline.py
@@ -1,4 +1,5 @@
import argparse
+import base64
from mitmproxy import cmdline
from . import tutils
@@ -53,6 +54,16 @@ def test_parse_server_spec():
"http://")
+def test_parse_upstream_auth():
+ tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, "")
+ tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, ":")
+ tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, ":test")
+ assert cmdline.parse_upstream_auth(
+ "test:test") == "Basic" + " " + base64.b64encode("test:test")
+ assert cmdline.parse_upstream_auth(
+ "test:") == "Basic" + " " + base64.b64encode("test:")
+
+
def test_parse_setheaders():
x = cmdline.parse_setheader("/foo/bar/voing")
assert x == ("foo", "bar", "voing")
diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py
index 2dce3fd6..3dc07427 100644
--- a/test/mitmproxy/test_flow_export.py
+++ b/test/mitmproxy/test_flow_export.py
@@ -1,6 +1,8 @@
+import json
from textwrap import dedent
import netlib.tutils
+from netlib.http import Headers
from mitmproxy import flow_export
from . import tutils
@@ -81,6 +83,35 @@ class TestExportPythonCode():
""").strip()
assert flow_export.python_code(flow) == result
+ def test_post_json(self):
+ req_post.content = '{"name": "example", "email": "example@example.com"}'
+ req_post.headers = Headers(content_type="application/json")
+ flow = tutils.tflow(req=req_post)
+ result = dedent("""
+ import requests
+
+ url = 'http://address/path'
+
+ headers = {
+ 'content-type': 'application/json',
+ }
+
+ json = {
+ "name": "example",
+ "email": "example@example.com"
+ }
+
+ response = requests.request(
+ method='POST',
+ url=url,
+ headers=headers,
+ json=json,
+ )
+
+ print(response.text)
+ """).strip()
+ assert flow_export.python_code(flow) == result
+
def test_patch(self):
flow = tutils.tflow(req=req_patch)
result = dedent("""
diff --git a/test/mitmproxy/test_har_extractor.py b/test/mitmproxy/test_har_extractor.py
new file mode 100644
index 00000000..7838f713
--- /dev/null
+++ b/test/mitmproxy/test_har_extractor.py
@@ -0,0 +1,37 @@
+import json
+import netlib.tutils
+from . import tutils
+
+from examples import har_extractor
+
+
+class Context(object):
+ pass
+
+
+trequest = netlib.tutils.treq(
+ timestamp_start=746203272,
+ timestamp_end=746203272,
+)
+
+tresponse = netlib.tutils.tresp(
+ timestamp_start=746203272,
+ timestamp_end=746203272,
+)
+
+
+def test_start():
+ tutils.raises(ValueError, har_extractor.start, Context(), [])
+
+
+def test_response():
+ ctx = Context()
+ ctx.HARLog = har_extractor._HARLog([])
+ ctx.seen_server = set()
+
+ fl = tutils.tflow(req=trequest, resp=tresponse)
+ har_extractor.response(ctx, fl)
+
+ with open(tutils.test_data.path("data/har_extractor.har")) as fp:
+ test_data = json.load(fp)
+ assert json.loads(ctx.HARLog.json()) == test_data["test_response"]
diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py
index 34b75b62..fddb851e 100644
--- a/test/mitmproxy/test_proxy.py
+++ b/test/mitmproxy/test_proxy.py
@@ -92,6 +92,10 @@ class TestProcessProxyOptions:
self.assert_err("expected one argument", "-U")
self.assert_err("Invalid server specification", "-U", "upstream")
+ self.assert_noerr("--upstream-auth", "test:test")
+ self.assert_err("expected one argument", "--upstream-auth")
+ self.assert_err("Invalid upstream auth specification", "--upstream-auth", "test")
+
self.assert_err("not allowed with", "-R", "http://localhost", "-T")
def test_socks_auth(self):