Skip to content

Commit

Permalink
Merge pull request apiflask#343 from hjlarry/patch-flask2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli authored Aug 24, 2022
2 parents d2e0e6f + d8c1459 commit fb70d84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/apiflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
from flask import jsonify
from flask import render_template_string
from flask.config import ConfigAttribute
from flask.globals import _request_ctx_stack
try:
from flask.globals import request_ctx
except ImportError:
from flask.globals import _request_ctx_stack
request_ctx = None # type: ignore
from flask.wrappers import Response

with warnings.catch_warnings():
Expand Down Expand Up @@ -406,7 +410,7 @@ def get_pet(name, pet_id, age, query, pet):
*Version added: 0.2.0*
"""
req = _request_ctx_stack.top.request
req = request_ctx.request if request_ctx else _request_ctx_stack.top.request
if req.routing_exception is not None:
self.raise_routing_exception(req)
rule = req.url_rule
Expand Down
25 changes: 13 additions & 12 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ def test_json_errors_reuse_werkzeug_headers(app, client):
def foo():
pass

rv = client.post('/foo')
assert rv.status_code == 405
assert 'Allow' in rv.headers

# test manually raise 405
@app.get('/bar')
def bar():
from werkzeug.exceptions import MethodNotAllowed
raise MethodNotAllowed(valid_methods=['GET'])

rv = client.post('/foo')
assert rv.status_code == 405
assert 'Allow' in rv.headers

rv = client.get('/bar')
assert rv.status_code == 405
assert rv.headers['Content-Type'] == 'application/json'
Expand Down Expand Up @@ -189,28 +189,29 @@ def get(self):


def test_dispatch_static_request(app, client):
# keyword arguments
rv = client.get('/static/hello.css') # endpoint: static
assert rv.status_code == 404

# positional arguments
@app.get('/mystatic/<int:pet_id>')
@app.input(Foo)
def mystatic(pet_id, foo): # endpoint: mystatic
return {'pet_id': pet_id, 'foo': foo}

# positional arguments
# blueprint static route accepts both keyword/positional arguments
bp = APIBlueprint('foo', __name__, static_folder='static')
app.register_blueprint(bp, url_prefix='/foo')

rv = client.get('/mystatic/2', json={'id': 1, 'name': 'foo'})
assert rv.status_code == 200
assert rv.json['pet_id'] == 2
assert rv.json['foo'] == {'id': 1, 'name': 'foo'}

# positional arguments
# blueprint static route accepts both keyword/positional arguments
bp = APIBlueprint('foo', __name__, static_folder='static')
app.register_blueprint(bp, url_prefix='/foo')
rv = client.get('/foo/static/hello.css') # endpoint: foo.static
assert rv.status_code == 404

# keyword arguments
rv = client.get('/static/hello.css') # endpoint: static
assert rv.status_code == 404


def schema_name_resolver1(schema):
name = schema.__class__.__name__
Expand Down

0 comments on commit fb70d84

Please sign in to comment.