aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_export_flow.py
blob: d026e21426c1df417d2e32d09988890da4ef54e0 (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
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",
)


def test_curl_command():
    flow = tutils.tflow(req=req_get)
    result = """curl -H 'header:qvalue' 'http://address/path'"""
    assert flow_export.curl_command(flow) == result

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

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


def test_python_code():
    flow = tutils.tflow(req=req_get)
    result = ("""import requests\n\n"""
              """url = 'http://address/path'\n\n"""
              """headers = {\n"""
              """    'header': 'qvalue',\n"""
              """}\n\n"""
              """response = requests.request(\n"""
              """    method='GET',\n"""
              """    url=url,\n"""
              """    headers=headers,\n"""
              """)\n\n"""
              """print(response.text)""")
    assert flow_export.python_code(flow) == result

    flow = tutils.tflow(req=req_post)
    result = ("""import requests\n\n"""
              """url = 'http://address/path'\n\n"""
              """data = '''content'''\n\n"""
              """response = requests.request(\n"""
              """    method='POST',\n"""
              """    url=url,\n"""
              """    data=data,\n)\n\n"""
              """print(response.text)""")
    assert flow_export.python_code(flow) == result

    flow = tutils.tflow(req=req_patch)
    result = ("""import requests\n\n"""
              """url = 'http://address/path'\n\n"""
              """headers = {\n"""
              """    'header': 'qvalue',\n"""
              """}\n\n"""
              """params = {\n"""
              """    'query': 'param',\n"""
              """}\n\n"""
              """data = '''content'''\n\n"""
              """response = requests.request(\n"""
              """    method='PATCH',\n"""
              """    url=url,\n"""
              """    headers=headers,\n"""
              """    params=params,\n"""
              """    data=data,\n"""
              """)\n\n"""
              """print(response.text)""")
    assert flow_export.python_code(flow) == result


def test_raw_request():
    flow = tutils.tflow(req=req_get)
    result = ("""GET /path HTTP/1.1\r\n"""
              """header: qvalue\r\n"""
              """host: address:22\r\n\r\n"""
              """""")
    assert flow_export.raw_request(flow) == result

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

    flow = tutils.tflow(req=req_patch)
    result = ("""PATCH /path?query=param HTTP/1.1\r\n"""
              """header: qvalue\r\n"""
              """host: address:22\r\n\r\n"""
              """content""")
    assert flow_export.raw_request(flow) == result