Skip to content

Commit

Permalink
Fix bug that prevents returning Flask.Responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
twosigmajab committed Jan 18, 2020
1 parent c8f42e6 commit 1b0420b
Show file tree
Hide file tree
Showing 2 changed files with 21 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
18 changes: 17 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,22 @@ 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 1b0420b

Please sign in to comment.