diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-06-02 11:37:18 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-06-02 11:37:18 +1200 |
commit | eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad (patch) | |
tree | a9a73ca4695beb00a6d83aa65cdc17945fc77107 /mitmproxy | |
parent | 92cdca50c7a43a5ab59b435e73e3b17dbe01cd3b (diff) | |
download | mitmproxy-eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad.tar.gz mitmproxy-eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad.tar.bz2 mitmproxy-eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad.zip |
Fix non-deterministic test failures in export
We had various places in the code where we relied on incidental order of dict
keys. Add a helper to multidict, and fix.
Diffstat (limited to 'mitmproxy')
-rw-r--r-- | mitmproxy/flow/export.py | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/mitmproxy/flow/export.py b/mitmproxy/flow/export.py index 97cee984..f0ac02ab 100644 --- a/mitmproxy/flow/export.py +++ b/mitmproxy/flow/export.py @@ -9,6 +9,13 @@ from six.moves.urllib.parse import quote, quote_plus import netlib.http +def dictstr(items, indent): + lines = [] + for k, v in items: + lines.append(indent + "%s: %s,\n" % (repr(k), repr(v))) + return "{\n%s}\n" % "".join(lines) + + def curl_command(flow): data = "curl " @@ -47,24 +54,19 @@ def python_code(flow): args = "" headers = "" if flow.request.headers: - lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.headers.fields] - headers += "\nheaders = {\n%s}\n" % "".join(lines) + headers += "\nheaders = %s\n" % dictstr(flow.request.headers.fields, " ") args += "\n headers=headers," params = "" if flow.request.query: - lines = [" %s: %s,\n" % (repr(k), repr(v)) for k, v in flow.request.query.to_dict().items()] - params = "\nparams = {\n%s}\n" % "".join(lines) + params = "\nparams = %s\n" % dictstr(flow.request.query.collect(), " ") args += "\n params=params," data = "" if flow.request.body: json_obj = is_json(flow.request.headers, flow.request.body) if json_obj: - # Without the separators field json.dumps() produces - # trailing white spaces: https://bugs.python.org/issue16333 - data = json.dumps(json_obj, indent=4, separators=(',', ': ')) - data = "\njson = %s\n" % data + data = "\njson = %s\n" % dictstr(sorted(json_obj.items()), " ") args += "\n json=json," else: data = "\ndata = '''%s'''\n" % flow.request.body @@ -78,7 +80,6 @@ def python_code(flow): method=flow.request.method, args=args, ) - return code @@ -142,7 +143,7 @@ def locust_code(flow): params = "" if flow.request.query: - lines = [" %s: %s,\n" % (repr(k), repr(v)) for k, v in flow.request.query.to_dict().items()] + lines = [" %s: %s,\n" % (repr(k), repr(v)) for k, v in flow.request.query.collect()] params = "\n params = {\n%s }\n" % "".join(lines) args += "\n params=params," |