mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-02 00:05:27 +00:00
Merge pull request #3676 from ylmrx/fix_curl_export
curl command export has several problems:
This commit is contained in:
commit
65edbda137
@ -11,17 +11,23 @@ import mitmproxy.types
|
||||
import pyperclip
|
||||
|
||||
|
||||
def raise_if_missing_request(f: flow.Flow) -> None:
|
||||
def cleanup_request(f: flow.Flow):
|
||||
if not hasattr(f, "request"):
|
||||
raise exceptions.CommandError("Can't export flow with no request.")
|
||||
request = f.request.copy() # type: ignore
|
||||
request.decode(strict=False)
|
||||
# a bit of clean-up
|
||||
if request.method == 'GET' and request.headers.get("content-length", None) == "0":
|
||||
request.headers.pop('content-length')
|
||||
request.headers.pop(':authority', None)
|
||||
return request
|
||||
|
||||
|
||||
def curl_command(f: flow.Flow) -> str:
|
||||
raise_if_missing_request(f)
|
||||
data = "curl "
|
||||
request = f.request.copy() # type: ignore
|
||||
request.decode(strict=False)
|
||||
request = cleanup_request(f)
|
||||
for k, v in request.headers.items(multi=True):
|
||||
data += "--compressed " if k == 'accept-encoding' else ""
|
||||
data += "-H '%s:%s' " % (k, v)
|
||||
if request.method != "GET":
|
||||
data += "-X %s " % request.method
|
||||
@ -35,11 +41,8 @@ def curl_command(f: flow.Flow) -> str:
|
||||
|
||||
|
||||
def httpie_command(f: flow.Flow) -> str:
|
||||
raise_if_missing_request(f)
|
||||
request = f.request.copy() # type: ignore
|
||||
data = "http %s " % request.method
|
||||
request.decode(strict=False)
|
||||
data += "%s" % request.url
|
||||
request = cleanup_request(f)
|
||||
data = "http %s %s" % (request.method, request.url)
|
||||
for k, v in request.headers.items(multi=True):
|
||||
data += " '%s:%s'" % (k, v)
|
||||
if request.content:
|
||||
@ -51,8 +54,7 @@ def httpie_command(f: flow.Flow) -> str:
|
||||
|
||||
|
||||
def raw(f: flow.Flow) -> bytes:
|
||||
raise_if_missing_request(f)
|
||||
return assemble.assemble_request(f.request) # type: ignore
|
||||
return assemble.assemble_request(cleanup_request(f)) # type: ignore
|
||||
|
||||
|
||||
formats = dict(
|
||||
|
@ -14,29 +14,23 @@ from unittest import mock
|
||||
@pytest.fixture
|
||||
def get_request():
|
||||
return tflow.tflow(
|
||||
req=tutils.treq(
|
||||
method=b'GET',
|
||||
content=b'',
|
||||
path=b"/path?a=foo&a=bar&b=baz"
|
||||
)
|
||||
)
|
||||
req=tutils.treq(method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def post_request():
|
||||
return tflow.tflow(
|
||||
req=tutils.treq(
|
||||
method=b'POST',
|
||||
headers=(),
|
||||
content=bytes(range(256))
|
||||
)
|
||||
)
|
||||
req=tutils.treq(method=b'POST', headers=(), content=bytes(range(256))))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_request():
|
||||
return tflow.tflow(
|
||||
req=tutils.treq(method=b'PATCH', path=b"/path?query=param")
|
||||
req=tutils.treq(
|
||||
method=b'PATCH',
|
||||
content=b'content',
|
||||
path=b"/path?query=param"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@ -47,7 +41,7 @@ def tcp_flow():
|
||||
|
||||
class TestExportCurlCommand:
|
||||
def test_get(self, get_request):
|
||||
result = """curl -H 'header:qvalue' -H 'content-length:0' 'http://address:22/path?a=foo&a=bar&b=baz'"""
|
||||
result = """curl -H 'header:qvalue' 'http://address:22/path?a=foo&a=bar&b=baz'"""
|
||||
assert export.curl_command(get_request) == result
|
||||
|
||||
def test_post(self, post_request):
|
||||
@ -67,7 +61,7 @@ class TestExportCurlCommand:
|
||||
|
||||
class TestExportHttpieCommand:
|
||||
def test_get(self, get_request):
|
||||
result = """http GET http://address:22/path?a=foo&a=bar&b=baz 'header:qvalue' 'content-length:0'"""
|
||||
result = """http GET http://address:22/path?a=foo&a=bar&b=baz 'header:qvalue'"""
|
||||
assert export.httpie_command(get_request) == result
|
||||
|
||||
def test_post(self, post_request):
|
||||
|
Loading…
Reference in New Issue
Block a user