apiserver: get_customized_response, including status code and headers

This commit is contained in:
debugtalk
2017-06-21 13:39:08 +08:00
parent 4a025164a6
commit 1f5ff5502c
3 changed files with 35 additions and 21 deletions

View File

@@ -23,17 +23,18 @@ users_dict = {}
def index():
return "Hello World!"
@app.route('/status_code/<int:status_code>')
def get_response_with_status_code(status_code):
return "Status Code: %d" % status_code, status_code
@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', "")
content = "Response: %s" % json.dumps(expected_resp_json)
response = make_response(content, status_code)
@app.route('/response_headers', methods=['POST'])
def get_response_with_headers():
headers_dict = request.get_json()
content = "Response headers: %s" % json.dumps(headers_dict)
response = make_response(content)
for header_key, header_value in headers_dict.items():
response.headers[header_key] = header_value
return response
@app.route('/api/users')

View File

@@ -108,18 +108,23 @@ class TestApiServer(ApiServerUnittest):
self.assertEqual(200, resp.status_code)
self.assertEqual(resp.json()['success'], True)
def test_get_response_with_status_code(self):
def test_get_customized_response_status_code(self):
status_code = random.randint(200, 511)
url = "%s/status_code/%d" % (self.host, status_code)
resp = self.api_client.get(url)
url = "%s/customize-response" % self.host
expected_response = {
'status_code': status_code,
}
resp = self.api_client.post(url, json=expected_response)
self.assertEqual(status_code, resp.status_code)
def test_get_response_with_headers(self):
headers = {
'abc': 123,
'def': 456
def test_get_customized_response_headers(self):
expected_response = {
'headers': {
'abc': 123,
'def': 456
}
}
url = "%s/response_headers" % self.host
resp = self.api_client.post(url, json=headers)
url = "%s/customize-response" % self.host
resp = self.api_client.post(url, json=expected_response)
self.assertIn('abc', resp.headers)
self.assertIn('123', resp.headers['abc'])

View File

@@ -56,8 +56,12 @@ class TestUtils(ApiServerUnittest):
def test_diff_response_status_code_equal(self):
status_code = random.randint(200, 511)
url = "http://127.0.0.1:5000/status_code/%d" % status_code
resp_obj = requests.get(url)
url = "http://127.0.0.1:5000/customize-response"
response_dict = {
'status_code': status_code,
}
resp_obj = requests.post(url, json=response_dict)
expected_resp_json = {
'status_code': status_code
}
@@ -66,8 +70,12 @@ class TestUtils(ApiServerUnittest):
def test_diff_response_status_code_not_equal(self):
status_code = random.randint(200, 511)
url = "http://127.0.0.1:5000/status_code/%d" % status_code
resp_obj = requests.get(url)
url = "http://127.0.0.1:5000/customize-response"
response_dict = {
'status_code': status_code,
}
resp_obj = requests.post(url, json=response_dict)
expected_resp_json = {
'status_code': 512
}