Skip to content

Commit

Permalink
Merge pull request #238 from mxmul/mmulder_swaggerpy_py3_async_post
Browse files Browse the repository at this point in the history
correctly handle async POST requests in swaggerpy + py3
  • Loading branch information
prat0318 committed Aug 9, 2016
2 parents f4567a4 + a6f4c64 commit 8c3e3ad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[report]
exclude_lines =
# Have to re-enable the standard pragma
\#\s*pragma: no cover

def __repr__
raise AssertionError
raise NotImplementedError
Expand Down
12 changes: 10 additions & 2 deletions swaggerpy/async_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ def start_request(self, request_params):
method=request_params.get('method')
)

if isinstance(
prepared_request.body,
six.text_type,
): # pragma: no cover (PY2)
body_bytes = prepared_request.body.encode('utf-8')
else:
body_bytes = prepared_request.body

request_for_crochet = {
'method': prepared_request.method or 'GET',
'bodyProducer': FileBodyProducer(BytesIO(prepared_request.body))
if prepared_request.body else None,
'bodyProducer': FileBodyProducer(BytesIO(body_bytes))
if body_bytes else None,
'headers': listify_headers(prepared_request.headers),
'uri': prepared_request.url,
}
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/async_http_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def two():
return ROUTE_2_RESPONSE


@bottle.route("/post", method="POST")
def post():
name = bottle.request.forms.get("name")
return 'Hello {name}! Have a snowman: ☃'.format(name=name)


def get_hopefully_free_port():
s = socket.socket()
s.bind(('', 0))
Expand Down Expand Up @@ -74,6 +80,27 @@ def test_multiple_requests_against_async_client(self):
self.assertEqual(resp_one.text, ROUTE_1_RESPONSE)
self.assertEqual(resp_two.text, ROUTE_2_RESPONSE)

def test_make_post_request(self):
port = get_hopefully_free_port()
launch_threaded_http_server(port)

client = AsynchronousHttpClient()

request_params = {
'method': b'POST',
'headers': {},
'url': "http://localhost:{0}/post".format(port),
'data': {'name': 'Matt'},
}

future = client.start_request(request_params)
response = future.wait(timeout=1)

self.assertEqual(
response.text,
u'Hello Matt! Have a snowman: ☃'.encode('utf-8'),
)

def test_erroring_request(self):
port = get_hopefully_free_port()
launch_threaded_http_server(port)
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ commands =
flake8 swaggerpy tests

[testenv:cover]
basepython = /usr/bin/python2.7
deps =
{[testenv]deps}
pytest-cov
commands =
py.test --cov swaggerpy {posargs:tests}
coverage combine
coverage combine --append
coverage report --omit=.tox/*,tests/*,/usr/share/pyshared/*,/usr/lib/pymodules/* -m

[testenv:docs]
Expand Down

0 comments on commit 8c3e3ad

Please sign in to comment.