forked from aws/chalice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with local mode bodiless responses causing clients to hang
fixes aws#525
- Loading branch information
1 parent
c915880
commit 7c1e32f
Showing
6 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import socket | ||
import contextlib | ||
from threading import Thread | ||
from threading import Event | ||
|
||
import pytest | ||
import requests | ||
|
||
from chalice import app | ||
from chalice.local import LocalDevServer | ||
from chalice.config import Config | ||
|
||
|
||
class ThreadedLocalServer(Thread): | ||
def __init__(self, port): | ||
super(ThreadedLocalServer, self).__init__() | ||
self._app_object = None | ||
self._config = None | ||
self._port = port | ||
self._server = None | ||
self._server_ready = Event() | ||
|
||
def configure(self, app_object, config): | ||
self._app_object = app_object | ||
self._config = config | ||
|
||
def run(self): | ||
self._server = LocalDevServer( | ||
self._app_object, self._config, self._port) | ||
self._server_ready.set() | ||
self._server.serve_forever() | ||
|
||
def make_call(self, method, path, port, timeout=0.5): | ||
self._server_ready.wait() | ||
return method('http://localhost:{port}{path}'.format( | ||
path=path, port=port), timeout=timeout) | ||
|
||
def shutdown(self): | ||
if self._server is not None: | ||
self._server.server.shutdown() | ||
|
||
|
||
@pytest.fixture | ||
def config(): | ||
return Config() | ||
|
||
|
||
@pytest.fixture() | ||
def unused_tcp_port(): | ||
with contextlib.closing(socket.socket()) as sock: | ||
sock.bind(('127.0.0.1', 0)) | ||
return sock.getsockname()[1] | ||
|
||
|
||
@pytest.fixture() | ||
def local_server_factory(unused_tcp_port): | ||
threaded_server = ThreadedLocalServer(unused_tcp_port) | ||
|
||
def create_server(app_object, config): | ||
threaded_server.configure(app_object, config) | ||
threaded_server.start() | ||
return threaded_server, unused_tcp_port | ||
|
||
try: | ||
yield create_server | ||
finally: | ||
threaded_server.shutdown() | ||
|
||
|
||
@pytest.fixture | ||
def sample_app(): | ||
demo = app.Chalice('demo-app') | ||
|
||
@demo.route('/', methods=['GET']) | ||
def index(): | ||
return {'hello': 'world'} | ||
|
||
@demo.route('/test-cors', methods=['POST'], cors=True) | ||
def test_cors(): | ||
return {'hello': 'world'} | ||
|
||
return demo | ||
|
||
|
||
def test_can_accept_get_request(config, sample_app, local_server_factory): | ||
local_server, port = local_server_factory(sample_app, config) | ||
response = local_server.make_call(requests.get, '/', port) | ||
assert response.status_code == 200 | ||
assert response.text == '{"hello": "world"}' | ||
|
||
|
||
def test_can_accept_options_request(config, sample_app, local_server_factory): | ||
local_server, port = local_server_factory(sample_app, config) | ||
response = local_server.make_call(requests.options, '/test-cors', port) | ||
assert response.headers['Content-Length'] == '0' | ||
assert response.headers['Access-Control-Allow-Methods'] == 'POST,OPTIONS' | ||
assert response.text == '' | ||
|
||
|
||
def test_can_accept_multiple_options_request(config, sample_app, | ||
local_server_factory): | ||
local_server, port = local_server_factory(sample_app, config) | ||
|
||
response = local_server.make_call(requests.options, '/test-cors', port) | ||
assert response.headers['Content-Length'] == '0' | ||
assert response.headers['Access-Control-Allow-Methods'] == 'POST,OPTIONS' | ||
assert response.text == '' | ||
|
||
response = local_server.make_call(requests.options, '/test-cors', port) | ||
assert response.headers['Content-Length'] == '0' | ||
assert response.headers['Access-Control-Allow-Methods'] == 'POST,OPTIONS' | ||
assert response.text == '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters