forked from orbit-oss/flask
make_test_environ_builder when used with subdomains was not working correctly, now it uses urlparse module for detecting full URL and changing path and base_url correctly
This commit is contained in:
parent
261c4a6aee
commit
4f1cb42123
2 changed files with 44 additions and 1 deletions
|
|
@ -15,6 +15,7 @@ from __future__ import with_statement
|
|||
from contextlib import contextmanager
|
||||
from werkzeug.test import Client, EnvironBuilder
|
||||
from flask import _request_ctx_stack
|
||||
from urlparse import urlparse
|
||||
|
||||
|
||||
def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):
|
||||
|
|
@ -22,9 +23,12 @@ def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):
|
|||
http_host = app.config.get('SERVER_NAME')
|
||||
app_root = app.config.get('APPLICATION_ROOT')
|
||||
if base_url is None:
|
||||
base_url = 'http://%s/' % (http_host or 'localhost')
|
||||
url = urlparse(path)
|
||||
base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')
|
||||
if app_root:
|
||||
base_url += app_root.lstrip('/')
|
||||
if url.netloc:
|
||||
path = url.path
|
||||
return EnvironBuilder(path, base_url, *args, **kwargs)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -198,7 +198,46 @@ class TestToolsTestCase(FlaskTestCase):
|
|||
self.assert_equal(called, [None, None])
|
||||
|
||||
|
||||
class SubdomainTestCase(FlaskTestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.app = flask.Flask(__name__)
|
||||
self.app.config['SERVER_NAME'] = 'example.com'
|
||||
self.client = self.app.test_client()
|
||||
|
||||
self._ctx = self.app.test_request_context()
|
||||
self._ctx.push()
|
||||
|
||||
def tearDown(self):
|
||||
if self._ctx is not None:
|
||||
self._ctx.pop()
|
||||
|
||||
def test_subdomain(self):
|
||||
@self.app.route('/', subdomain='<company_id>')
|
||||
def view(company_id):
|
||||
return company_id
|
||||
|
||||
url = flask.url_for('view', company_id='xxx')
|
||||
response = self.client.get(url)
|
||||
|
||||
self.assertEquals(200, response.status_code)
|
||||
self.assertEquals('xxx', response.data)
|
||||
|
||||
|
||||
def test_nosubdomain(self):
|
||||
@self.app.route('/<company_id>')
|
||||
def view(company_id):
|
||||
return company_id
|
||||
|
||||
url = flask.url_for('view', company_id='xxx')
|
||||
response = self.client.get(url)
|
||||
|
||||
self.assertEquals(200, response.status_code)
|
||||
self.assertEquals('xxx', response.data)
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(TestToolsTestCase))
|
||||
suite.addTest(unittest.makeSuite(SubdomainTestCase))
|
||||
return suite
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue