aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_flow_export.py
blob: aafd5a1c0e56108a758510a3d0cb609e01b0486d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import re

import netlib.tutils
from netlib.http import Headers
from mitmproxy import export  # heh
from . 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 netlib.tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz")


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


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


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

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

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


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

    def test_post(self):
        flow = tutils.tflow(req=req_post())
        python_equals("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 = tutils.tflow(req=p)
        python_equals("data/test_flow_export/python_post_json.py", export.python_code(flow))

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


class TestExportLocustCode:
    def test_get(self):
        flow = tutils.tflow(req=req_get())
        python_equals("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 = tutils.tflow(req=p)
        python_equals("data/test_flow_export/locust_post.py", export.locust_code(flow))

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


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

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

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


class TestIsJson:
    def test_empty(self):
        assert export.is_json(None, None) is False

    def test_json_type(self):
        headers = Headers(content_type="application/json")
        assert export.is_json(headers, b"foobar") is False

    def test_valid(self):
        headers = Headers(content_type="application/foobar")
        j = export.is_json(headers, b'{"name": "example", "email": "example@example.com"}')
        assert j is False

    def test_valid2(self):
        headers = Headers(content_type="application/json")
        j = export.is_json(headers, b'{"name": "example", "email": "example@example.com"}')
        assert isinstance(j, dict)


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