def test_api_gateway_http_integration():
test_port = 12123
backend_url = 'http://localhost:%s%s' % (test_port, API_PATH_HTTP_BACKEND)
# create target HTTP backend
class TestListener(ProxyListener):
def forward_request(self, **kwargs):
response = Response()
response.status_code = 200
response._content = kwargs.get('data') or '{}'
return response
proxy = GenericProxy(test_port, update_listener=TestListener())
proxy.start()
# create API Gateway and connect it to the HTTP backend
result = connect_api_gateway_to_http('test_gateway2', backend_url, path=API_PATH_HTTP_BACKEND)
url = INBOUND_GATEWAY_URL_PATTERN.format(api_id=result['id'],
stage_name=TEST_STAGE_NAME, path=API_PATH_HTTP_BACKEND)
# make sure CORS headers are present
origin = 'localhost'
result = requests.options(url, headers={'origin': origin})
assert result.status_code == 200
assert re.match(result.headers['Access-Control-Allow-Origin'].replace('*', '.*'), origin)
assert 'POST' in result.headers['Access-Control-Allow-Methods']
# make test request to gateway
result = requests.get(url)
assert result.status_code == 200
assert to_str(result.content) == '{}'
data = {'data': 123}
result = requests.post(url, data=json.dumps(data))
assert result.status_code == 200
assert json.loads(to_str(result.content)) == data
# clean up
proxy.stop()
评论列表
文章目录