aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_flow_export.py
blob: 457d88361b866362434845188bed873b4f40410e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from mitmproxy.test import tflow
import re

from mitmproxy.net.http import Headers
from mitmproxy import export  # heh
from mitmproxy.test import tutils


def clean_blanks(s):
    return re.sub(r"^(\s+)$", "", s, flags=re.MULTILINE)


def python_equals(testdata, text):
    """
        Compare two bits of Python code, disregarding non-significant differences
        like whitespace on blank lines and trailing space.
    """
    d = open(tutils.test_data.path(testdata)).read()
    assert clean_blanks(text).rstrip() == clean_blanks(d).rstrip()


def req_get():
    return tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz")


def req_post():
    return tutils.treq(method=b'POST', headers=())


def req_patch():
    return tutils.treq(method=b'PATCH', path=b"/path?query=param")


class TestExportCurlCommand:
    def test_get(self):
        flow = tflow.tflow(req=req_get())
        result = """curl -H 'header:qvalue' -H 'content-length:7' 'http://address:22/path?a=foo&a=bar&b=baz'"""
        assert export.curl_command(flow) == result

    def test_post(self):
        flow = tflow.tflow(req=req_post())
        result = """curl -X POST 'http://address:22/path' --data-binary 'content'"""
        assert export.curl_command(flow) == result

    def test_patch(self):
        flow = tflow.tflow(req=req_patch())
        result = """curl -H 'header:qvalue' -H 'content-length:7' -X PATCH 'http://address:22/path?query=param' --data-binary 'content'"""
        assert export.curl_command(flow) == result


class TestExportPythonCode:
    def test_get(self):
        flow = tflow.tflow(req=req_get())
        python_equals("mitmproxy/data/test_flow_export/python_get.py", export.python_code(flow))

    def test_post(self):
        flow = tflow.tflow(req=req_post())
        python_equals("mitmproxy/data/test_flow_export/python_post.py", export.python_code(flow))

    def test_post_json(self):
        p = req_post()
        p.content = b'{"name": "example", "email": "example@example.com"}'
        p.headers = Headers(content_type="application/json")
        flow = tflow.tflow(req=p)
        python_equals("mitmproxy/data/test_flow_export/python_post_json.py", export.python_code(flow))

    def test_patch(self):
        flow = tflow.tflow(req=req_patch())
        python_equals("mitmproxy/data/test_flow_export/python_patch.py", export.python_code(flow))


class TestExportLocustCode:
    def test_get(self):
        flow = tflow.tflow(req=req_get())
        python_equals("mitmproxy/data/test_flow_export/locust_get.py", export.locust_code(flow))

    def test_post(self):
        p = req_post()
        p.content = b'content'
        p.headers = ''
        flow = tflow.tflow(req=p)
        python_equals("mitmproxy/data/test_flow_export/locust_post.py", export.locust_code(flow))

    def test_patch(self):
        flow = tflow.tflow(req=req_patch())
        python_equals("mitmproxy/data/test_flow_export/locust_patch.py", export.locust_code(flow))


class TestExportLocustTask:
    def test_get(self):
        flow = tflow.tflow(req=req_get())
        python_equals("mitmproxy/data/test_flow_export/locust_task_get.py", export.locust_task(flow))

    def test_post(self):
        flow = tflow.tflow(req=req_post())
        python_equals("mitmproxy/data/test_flow_export/locust_task_post.py", export.locust_task(flow))

    def test_patch(self):
        flow = tflow.tflow(req=req_patch())
        python_equals("mitmproxy/data/test_flow_export/locust_task_patch.py", export.locust_task(flow))


class TestURL:
    def test_url(self):
        flow = tflow.tflow()
        assert export.url(flow) == "http://address:22/path"