Skip to content

Commit

Permalink
Fix bug that prevents returning Flask.Responses
Browse files Browse the repository at this point in the history
which https://flask-rebar.readthedocs.io/en/latest/quickstart/basics.html#marshaling
promises should be supported.

Also add a test case that fails without this fix.
  • Loading branch information
twosigmajab committed Jan 18, 2020
1 parent c8f42e6 commit 52b08a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion flask_rebar/rebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _unpack_view_func_return_value(rv):
This imitates Flask's own `Flask.make_response` method.
:param Optional[tuple] rv: (body, status, headers), (body, status), or (body, headers)
:param rv: (body, status, headers), (body, status), (body, headers), or body
:return: (body, status, headers)
:rtype: tuple
"""
Expand Down Expand Up @@ -146,6 +146,9 @@ def wrapped(*args, **kwargs):
if not response_body_schema:
return rv

if isinstance(rv, current_app.response_class):
return rv

data, status_code, headers = _unpack_view_func_return_value(rv)

schema = response_body_schema[status_code] # May raise KeyError.
Expand Down
15 changes: 14 additions & 1 deletion tests/test_rebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import unittest

import marshmallow as m
from flask import Flask
from flask import Flask, make_response

from flask_rebar import messages
from flask_rebar import HeaderApiKeyAuthenticator, SwaggerV3Generator
Expand Down Expand Up @@ -241,6 +241,19 @@ def update_foo(foo_uid):
)
self.assertEqual(resp.status_code, 400)

def test_flask_response_instance_interop(self):
rebar = Rebar()
registry = rebar.create_handler_registry()

@registry.handles(rule="/foo", response_body_schema={302: None})
def foo():
return make_response(("Redirecting", 302, {"Location": "http://foo.com"}))

app = create_rebar_app(rebar)
resp = app.test_client().get(path="/foo")
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.headers["Location"], "http://foo.com")

def test_validate_query_parameters(self):
rebar = Rebar()
registry = rebar.create_handler_registry()
Expand Down

0 comments on commit 52b08a7

Please sign in to comment.