aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/test/test_flow_export.py
blob: e5e9c0a30fd3d8988d57248c76af3409b8bd525d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from textwrap import dedent

import netlib.tutils
from libmproxy import flow_export
from . import tutils

req_get = netlib.tutils.treq(
    method='GET',
    content='',
)

req_post = netlib.tutils.treq(
    method='POST',
    headers=None,
)

req_patch = netlib.tutils.treq(
    method='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'"""
        assert flow_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 flow_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 flow_export.curl_command(flow) == result


class TestExportPythonCode():

    def test_get(self):
        flow = tutils.tflow(req=req_get)
        result = dedent("""
            import requests

            url = 'http://address/path'

            headers = {
                'header': 'qvalue',
                'content-length': '7',
            }

            response = requests.request(
                method='GET',
                url=url,
                headers=headers,
            )

            print(response.text)
        """).strip()
        assert flow_export.python_code(flow) == result

    def test_post(self):
        flow = tutils.tflow(req=req_post)
        result = dedent("""
            import requests

            url = 'http://address/path'

            data = '''content'''

            response = requests.request(
                method='POST',
                url=url,
                data=data,
            )

            print(response.text)
        """).strip()
        assert flow_export.python_code(flow) == result

    def test_patch(self):
        flow = tutils.tflow(req=req_patch)
        result = dedent("""
            import requests

            url = 'http://address/path'

            headers = {
                'header': 'qvalue',
                'content-length': '7',
            }

            params = {
                'query': 'param',
            }

            data = '''content'''

            response = requests.request(
                method='PATCH',
                url=url,
                headers=headers,
                params=params,
                data=data,
            )

            print(response.text)
        """).strip()
        assert flow_export.python_code(flow) == result


def TestRawRequest():

    def test_get(self):
        flow = tutils.tflow(req=req_get)
        result = dedent("""
            GET /path HTTP/1.1\r
            header: qvalue\r
            content-length: 7\r
            host: address:22\r
            \r
        """).strip(" ").lstrip()
        assert flow_export.raw_request(flow) == result

    def test_post(self):
        flow = tutils.tflow(req=req_post)
        result = dedent("""
            POST /path HTTP/1.1\r
            host: address:22\r
            \r
            content
        """).strip()
        assert flow_export.raw_request(flow) == result

    def test_patch(self):
        flow = tutils.tflow(req=req_patch)
        result = dedent("""
            PATCH /path?query=param HTTP/1.1\r
            header: qvalue\r
            content-length: 7\r
            host: address:22\r
            \r
            content
        """).strip()
        assert flow_export.raw_request(flow) == result