From 51c35295af94f8f401a4942aab674f161222a984 Mon Sep 17 00:00:00 2001 From: Nadav Geva Date: Thu, 15 Oct 2015 00:02:46 +0300 Subject: [PATCH 1/2] Fix name mismatch in apierrors page --- docs/patterns/apierrors.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/patterns/apierrors.rst b/docs/patterns/apierrors.rst index 264b9ae5..b06966e6 100644 --- a/docs/patterns/apierrors.rst +++ b/docs/patterns/apierrors.rst @@ -47,7 +47,7 @@ At that point views can raise that error, but it would immediately result in an internal server error. The reason for this is that there is no handler registered for this error class. That however is easy to add:: - @app.errorhandler(InvalidAPIUsage) + @app.errorhandler(InvalidUsage) def handle_invalid_usage(error): response = jsonify(error.to_dict()) response.status_code = error.status_code From daa3f272da477470d23481d409d54f989136ac32 Mon Sep 17 00:00:00 2001 From: Ivan Velichko Date: Fri, 20 Nov 2015 19:43:49 +0300 Subject: [PATCH 2/2] Allow to specify subdomain and/or url_scheme in app.test_request_context() --- flask/app.py | 6 +++++- flask/testing.py | 19 +++++++++++++++---- flask/testsuite/testing.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/flask/app.py b/flask/app.py index e073798a..8a191d0a 100644 --- a/flask/app.py +++ b/flask/app.py @@ -1776,7 +1776,11 @@ class Flask(_PackageBoundObject): def test_request_context(self, *args, **kwargs): """Creates a WSGI environment from the given values (see :func:`werkzeug.test.EnvironBuilder` for more information, this - function accepts the same arguments). + function accepts the same arguments plus two additional). + + Additional arguments (might be used only if `base_url` is not specified): + :param subdomain: subdomain in case of testing requests handled by blueprint + :param url_scheme: a URL scheme (default scheme is http) """ from flask.testing import make_test_environ_builder builder = make_test_environ_builder(self, *args, **kwargs) diff --git a/flask/testing.py b/flask/testing.py index 6351462b..73043f1b 100644 --- a/flask/testing.py +++ b/flask/testing.py @@ -20,13 +20,24 @@ except ImportError: from urlparse import urlsplit as url_parse -def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs): +def make_test_environ_builder(app, path='/', base_url=None, subdomain=None, + url_scheme=None, *args, **kwargs): """Creates a new test builder with some application defaults thrown in.""" - http_host = app.config.get('SERVER_NAME') - app_root = app.config.get('APPLICATION_ROOT') + assert not (base_url or subdomain or url_scheme) \ + or (base_url is not None) != bool(subdomain or url_scheme), \ + 'If "base_url" parameter is passed in, pass of ' \ + '"subdomain" and/or "url_scheme" is meaningless.' + if base_url is None: + http_host = app.config.get('SERVER_NAME') + app_root = app.config.get('APPLICATION_ROOT') + if subdomain: + http_host = '%s.%s' % (subdomain, http_host) + if url_scheme is None: + url_scheme = 'http' + url = url_parse(path) - base_url = 'http://%s/' % (url.netloc or http_host or 'localhost') + base_url = '%s://%s/' % (url_scheme, url.netloc or http_host or 'localhost') if app_root: base_url += app_root.lstrip('/') if url.netloc: diff --git a/flask/testsuite/testing.py b/flask/testsuite/testing.py index 11ab763b..9d58fef4 100644 --- a/flask/testsuite/testing.py +++ b/flask/testsuite/testing.py @@ -45,6 +45,38 @@ class TestToolsTestCase(FlaskTestCase): rv = c.get('/') self.assert_equal(rv.data, b'http://localhost/') + def test_specify_url_scheme(self): + app = flask.Flask(__name__) + app.testing = True + @app.route('/') + def index(): + return flask.request.url + + ctx = app.test_request_context(url_scheme='https') + self.assert_equal(ctx.request.url, 'https://localhost/') + with app.test_client() as c: + rv = c.get('/', url_scheme='https') + self.assert_equal(rv.data, b'https://localhost/') + + def test_blueprint_with_subdomain(self): + app = flask.Flask(__name__) + app.testing = True + app.config['SERVER_NAME'] = 'example.com:1234' + app.config['APPLICATION_ROOT'] = '/foo' + + bp = flask.Blueprint('company', __name__, subdomain='xxx') + @bp.route('/') + def index(): + return flask.request.url + app.register_blueprint(bp) + + ctx = app.test_request_context('/', subdomain='xxx') + self.assert_equal(ctx.request.url, 'http://xxx.example.com:1234/foo/') + self.assert_equal(ctx.request.blueprint, bp.name) + with app.test_client() as c: + rv = c.get('/', subdomain='xxx') + self.assert_equal(rv.data, b'http://xxx.example.com:1234/foo/') + def test_redirect_keep_session(self): app = flask.Flask(__name__) app.secret_key = 'testing'