Skip to content

Commit

Permalink
Merge 9ce03bb into 48ca816
Browse files Browse the repository at this point in the history
  • Loading branch information
joegasewicz committed Nov 2, 2019
2 parents 48ca816 + 9ce03bb commit 8304f9b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
24 changes: 20 additions & 4 deletions flask_restful/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import
from functools import wraps, partial
from flask import request, url_for, current_app
from flask import request, url_for, current_app, make_response
from flask import abort as original_flask_abort
from flask import make_response as original_flask_make_response
from flask.views import MethodView
Expand Down Expand Up @@ -563,8 +563,19 @@ class Resource(MethodView):
representations = None
method_decorators = []

def dispatch_request(self, *args, **kwargs):
def _is_template(self, response):
if "<html" == response[:5] or "<!DOCTYPE" == response[:9]:
return True
else:
return False

def output_html(self, data, code):
"""Return Flask HTML template"""
resp = make_response(data, code)
resp.headers["Content-Type"] = "text/html"
return resp

def dispatch_request(self, *args, **kwargs):
# Taken from flask
#noinspection PyUnresolvedReferences
meth = getattr(self, request.method.lower(), None)
Expand All @@ -582,11 +593,16 @@ def dispatch_request(self, *args, **kwargs):

resp = meth(*args, **kwargs)

representations = self.representations or OrderedDict()

if isinstance(resp, str) and self._is_template(resp):
representations["text/html"] = self.output_html
data, code, _ = unpack(resp)
return representations["text/html"](data, code)

if isinstance(resp, ResponseBase): # There may be a better way to test
return resp

representations = self.representations or OrderedDict()

#noinspection PyUnresolvedReferences
mediatype = request.accept_mimetypes.best_match(representations, default=None)
if mediatype in representations:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# python3
from unittest.mock import Mock
import flask
from flask import make_response
import werkzeug
from werkzeug.exceptions import HTTPException, Unauthorized, BadRequest, NotFound, _aborter
from werkzeug.http import quote_etag, unquote_etag
Expand Down Expand Up @@ -709,6 +710,26 @@ def get(self):
resp = resource.dispatch_request()
self.assertEquals(resp.data.decode(), 'hello')

def test_resource_template(self):
app = Flask(__name__)

def text(data, code, headers=None):
return flask.make_response(six.text_type(data))

mock_html = "<html><body>Hello World!</body></html>"

def render_template(html_str):
return html_str

class HelloWorldHtml(flask_restful.Resource):
def get(self):
return render_template(mock_html)

with app.test_request_context("/foo", headers={'Accept': 'text/plain'}):
resource = HelloWorldHtml()
resp = resource.dispatch_request()
self.assertEquals(resp.status_code, 200)

def test_resource_error(self):
app = Flask(__name__)
resource = flask_restful.Resource()
Expand Down

0 comments on commit 8304f9b

Please sign in to comment.