fix #191: request data dumps

This commit is contained in:
debugtalk
2018-04-20 22:00:56 +08:00
parent e5191fd740
commit 0a3f5cc146
2 changed files with 34 additions and 1 deletions

View File

@@ -137,7 +137,7 @@ def setup_hook_prepare_kwargs(method, url, kwargs):
content_type = kwargs.get("headers", {}).get("content-type")
if content_type and "data" in kwargs:
# if request content-type is application/json, request data should be dumped
if content_type.startswith("application/json"):
if content_type.startswith("application/json") and isinstance(kwargs["data"], (dict, list)):
kwargs["data"] = json.dumps(kwargs["data"])
if isinstance(kwargs["data"], str):

View File

@@ -106,3 +106,36 @@ class TestHttpRunner(ApiServerUnittest):
self.assertTrue(summary["success"])
self.assertEqual(summary["stat"]["testsRun"], 1)
self.assertIn("records", summary)
def test_run_post_data(self):
testsets = [
{
"name": "post data",
"config": {
"path": ""
},
"testcases": [
{
"name": "post data",
"request": {
"url": "https://httpbin.org/post",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"data": "abc"
},
"validate": [
{"eq": ["status_code", 200]}
]
}
]
}
]
runner = HttpRunner().run(testsets)
summary = runner.summary
self.assertTrue(summary["success"])
self.assertEqual(summary["stat"]["testsRun"], 1)
self.assertEqual(summary["records"][0]["meta_data"]["response_body"]["data"], "abc")