1, refactor ResponseObject;
2, response can be modified in teardown_hooks.
This commit is contained in:
debugtalk
2018-05-31 00:36:10 +08:00
parent 2dd9413674
commit 0b3c6665c2
9 changed files with 142 additions and 162 deletions

View File

@@ -95,19 +95,6 @@ def get_token():
response.headers["Content-Type"] = "application/json"
return response
@app.route('/customize-response', methods=['POST'])
def get_customized_response():
expected_resp_json = request.get_json()
status_code = expected_resp_json.get('status_code', 200)
headers_dict = expected_resp_json.get('headers', {})
body = expected_resp_json.get('body', {})
response = make_response(json.dumps(body), status_code)
for header_key, header_value in headers_dict.items():
response.headers[header_key] = header_value
return response
@app.route('/api/users')
@validate_request
def get_users():

View File

@@ -94,3 +94,8 @@ def setup_hook_httpntlmauth(request):
auth_account = request.pop("httpntlmauth")
request["auth"] = HttpNtlmAuth(
auth_account["username"], auth_account["password"])
def alter_response(response):
response.status_code = 500
response.headers["Content-Type"] = "html/text"
response.json["headers"]["Host"] = "127.0.0.1:8888"

View File

@@ -20,3 +20,17 @@
validate:
- eq: ["status_code", 200]
- eq: [content.headers.Host, "127.0.0.1:3458"]
- test:
name: alter response
request:
url: /headers
method: GET
teardown_hooks:
- ${alter_response($response)}
validate:
- eq: ["status_code", 500]
- eq: ["headers.content-type", "html/text"]
- eq: [json.headers.Host, "127.0.0.1:8888"]
- eq: [content.headers.Host, "127.0.0.1:3458"]
- eq: [text.headers.Host, "127.0.0.1:3458"]

View File

@@ -113,32 +113,3 @@ class TestApiServer(ApiServerUnittest):
resp = self.delete_user(1000)
self.assertEqual(200, resp.status_code)
self.assertEqual(resp.json()['success'], True)
def test_get_customized_response_status_code(self):
status_code = random.randint(200, 511)
url = "%s/customize-response" % self.host
expected_response = {
'status_code': status_code,
}
resp = self.api_client.post(
url,
headers=self.headers,
json=expected_response
)
self.assertEqual(status_code, resp.status_code)
def test_get_customized_response_headers(self):
expected_response = {
'headers': {
'abc': 123,
'def': 456
}
}
url = "%s/customize-response" % self.host
resp = self.api_client.post(
url,
headers=self.headers,
json=expected_response
)
self.assertIn('abc', resp.headers)
self.assertIn('123', resp.headers['abc'])

View File

@@ -1,5 +1,6 @@
import requests
from httprunner import exception, response, utils
from httprunner.compat import bytes
from tests.base import ApiServerUnittest
@@ -13,55 +14,76 @@ class TestResponse(ApiServerUnittest):
url = "http://127.0.0.1:5000/api/users"
resp = requests.get(url)
resp_obj = response.ResponseObject(resp)
parsed_dict = resp_obj.parsed_dict()
self.assertIn('status_code', parsed_dict)
self.assertIn('headers', parsed_dict)
self.assertIn('body', parsed_dict)
self.assertIn('Content-Type', parsed_dict['headers'])
self.assertIn('Content-Length', parsed_dict['headers'])
self.assertIn('success', parsed_dict['body'])
self.assertTrue(hasattr(resp_obj, 'status_code'))
self.assertTrue(hasattr(resp_obj, 'headers'))
self.assertTrue(hasattr(resp_obj, 'content'))
self.assertIn('Content-Type', resp_obj.headers)
self.assertIn('Content-Length', resp_obj.headers)
self.assertIn('success', resp_obj.json)
def test_parse_response_object_text(self):
def test_parse_response_object_content(self):
url = "http://127.0.0.1:5000/"
resp = requests.get(url)
resp_obj = response.ResponseObject(resp)
parsed_dict = resp_obj.parsed_dict()
self.assertIn('status_code', parsed_dict)
self.assertIn('headers', parsed_dict)
self.assertIn('body', parsed_dict)
self.assertIn('Content-Type', parsed_dict['headers'])
self.assertIn('Content-Length', parsed_dict['headers'])
self.assertTrue(str, type(parsed_dict['body']))
self.assertEqual(bytes, type(resp_obj.content))
def test_extract_response_json(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
url="http://127.0.0.1:3458/anything",
json={
'headers': {
'Content-Type': "application/json"
},
'body': {
'success': False,
"person": {
"name": {
"first_name": "Leo",
"last_name": "Lee",
},
"age": 29,
"cities": ["Guangzhou", "Shenzhen"]
}
'success': False,
"person": {
"name": {
"first_name": "Leo",
"last_name": "Lee",
},
"age": 29,
"cities": ["Guangzhou", "Shenzhen"]
}
}
)
# resp.text
# {
# "args": {},
# "data": "{\"success\": false, \"person\": {\"name\": {\"first_name\": \"Leo\", \"last_name\": \"Lee\"}, \"age\": 29, \"cities\": [\"Guangzhou\", \"Shenzhen\"]}}",
# "files": {},
# "form": {},
# "headers": {
# "Accept": "*/*",
# "Accept-Encoding": "gzip, deflate",
# "Connection": "keep-alive",
# "Content-Length": "129",
# "Content-Type": "application/json",
# "Host": "127.0.0.1:3458",
# "User-Agent": "python-requests/2.18.4"
# },
# "json": {
# "person": {
# "age": 29,
# "cities": [
# "Guangzhou",
# "Shenzhen"
# ],
# "name": {
# "first_name": "Leo",
# "last_name": "Lee"
# }
# },
# "success": false
# },
# "method": "POST",
# "origin": "127.0.0.1",
# "url": "http://127.0.0.1:3458/anything"
# }
extract_binds_list = [
{"resp_status_code": "status_code"},
{"resp_headers_content_type": "headers.content-type"},
{"resp_content_body_success": "body.success"},
{"resp_content_content_success": "content.success"},
{"resp_content_text_success": "text.success"},
{"resp_content_person_first_name": "content.person.name.first_name"},
{"resp_content_cities_1": "content.person.cities.1"}
{"resp_content_body_success": "json.json.success"},
{"resp_content_content_success": "content.json.success"},
{"resp_content_text_success": "text.json.success"},
{"resp_content_person_first_name": "content.json.person.name.first_name"},
{"resp_content_cities_1": "content.json.person.cities.1"}
]
resp_obj = response.ResponseObject(resp)
extract_binds_dict = resp_obj.extract_response(extract_binds_list)
@@ -97,21 +119,16 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_fail(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
url="http://127.0.0.1:3458/anything",
json={
'headers': {
'Content-Type': "application/json"
},
'body': {
'success': False,
"person": {
"name": {
"first_name": "Leo",
"last_name": "Lee",
},
"age": 29,
"cities": ["Guangzhou", "Shenzhen"]
}
'success': False,
"person": {
"name": {
"first_name": "Leo",
"last_name": "Lee",
},
"age": 29,
"cities": ["Guangzhou", "Shenzhen"]
}
}
)
@@ -134,17 +151,12 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_json_string(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'Content-Type': "application/json"
},
'body': "abc"
}
url="http://127.0.0.1:3458/anything",
data="abc"
)
extract_binds_list = [
{"resp_content_body": "content"}
{"resp_content_body": "content.data"}
]
resp_obj = response.ResponseObject(resp)
@@ -156,13 +168,8 @@ class TestResponse(ApiServerUnittest):
def test_extract_text_response(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'Content-Type': "application/json"
},
'body': "LB123abcRB789"
}
url="http://127.0.0.1:3458/anything",
data="LB123abcRB789"
)
extract_binds_list = [
@@ -188,13 +195,8 @@ class TestResponse(ApiServerUnittest):
def test_extract_text_response_exception(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'Content-Type': "application/json"
},
'body': "LB123abcRB789"
}
url="http://127.0.0.1:3458/anything",
data="LB123abcRB789"
)
extract_binds_list = [
{"resp_content_key1": "LB123.*RB789"}
@@ -205,28 +207,23 @@ class TestResponse(ApiServerUnittest):
def test_extract_response_empty(self):
resp = requests.post(
url="http://127.0.0.1:5000/customize-response",
json={
'headers': {
'Content-Type': "application/json"
},
'body': ""
}
url="http://127.0.0.1:3458/anything",
data="abc"
)
extract_binds_list = [
{"resp_content_body": "content"}
{"resp_content_body": "content.data"}
]
resp_obj = response.ResponseObject(resp)
extract_binds_dict = resp_obj.extract_response(extract_binds_list)
self.assertEqual(
extract_binds_dict["resp_content_body"],
""
'abc'
)
extract_binds_list = [
{"resp_content_body": "content.abc"}
{"resp_content_body": "content.data.def"}
]
resp_obj = response.ResponseObject(resp)
with self.assertRaises(exception.ParamsError):
with self.assertRaises(exception.ParseResponseError):
resp_obj.extract_response(extract_binds_list)