Kill classes in test_testing
This commit is contained in:
parent
77d887526d
commit
f8a778deae
1 changed files with 182 additions and 196 deletions
|
|
@ -16,242 +16,228 @@ import unittest
|
||||||
from flask._compat import text_type
|
from flask._compat import text_type
|
||||||
|
|
||||||
|
|
||||||
class TestTestTools(object):
|
def test_environ_defaults_from_config():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
app.testing = True
|
||||||
|
app.config['SERVER_NAME'] = 'example.com:1234'
|
||||||
|
app.config['APPLICATION_ROOT'] = '/foo'
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return flask.request.url
|
||||||
|
|
||||||
def test_environ_defaults_from_config(self):
|
ctx = app.test_request_context()
|
||||||
app = flask.Flask(__name__)
|
assert ctx.request.url == 'http://example.com:1234/foo/'
|
||||||
app.testing = True
|
with app.test_client() as c:
|
||||||
app.config['SERVER_NAME'] = 'example.com:1234'
|
rv = c.get('/')
|
||||||
app.config['APPLICATION_ROOT'] = '/foo'
|
assert rv.data == b'http://example.com:1234/foo/'
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
return flask.request.url
|
|
||||||
|
|
||||||
ctx = app.test_request_context()
|
def test_environ_defaults():
|
||||||
assert ctx.request.url == 'http://example.com:1234/foo/'
|
app = flask.Flask(__name__)
|
||||||
with app.test_client() as c:
|
app.testing = True
|
||||||
rv = c.get('/')
|
@app.route('/')
|
||||||
assert rv.data == b'http://example.com:1234/foo/'
|
def index():
|
||||||
|
return flask.request.url
|
||||||
|
|
||||||
def test_environ_defaults(self):
|
ctx = app.test_request_context()
|
||||||
app = flask.Flask(__name__)
|
assert ctx.request.url == 'http://localhost/'
|
||||||
app.testing = True
|
with app.test_client() as c:
|
||||||
@app.route('/')
|
rv = c.get('/')
|
||||||
def index():
|
assert rv.data == b'http://localhost/'
|
||||||
return flask.request.url
|
|
||||||
|
|
||||||
ctx = app.test_request_context()
|
def test_redirect_keep_session():
|
||||||
assert ctx.request.url == 'http://localhost/'
|
app = flask.Flask(__name__)
|
||||||
with app.test_client() as c:
|
app.secret_key = 'testing'
|
||||||
rv = c.get('/')
|
|
||||||
assert rv.data == b'http://localhost/'
|
|
||||||
|
|
||||||
def test_redirect_keep_session(self):
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
app = flask.Flask(__name__)
|
def index():
|
||||||
app.secret_key = 'testing'
|
if flask.request.method == 'POST':
|
||||||
|
return flask.redirect('/getsession')
|
||||||
|
flask.session['data'] = 'foo'
|
||||||
|
return 'index'
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
@app.route('/getsession')
|
||||||
def index():
|
def get_session():
|
||||||
if flask.request.method == 'POST':
|
return flask.session.get('data', '<missing>')
|
||||||
return flask.redirect('/getsession')
|
|
||||||
flask.session['data'] = 'foo'
|
|
||||||
return 'index'
|
|
||||||
|
|
||||||
@app.route('/getsession')
|
with app.test_client() as c:
|
||||||
def get_session():
|
rv = c.get('/getsession')
|
||||||
return flask.session.get('data', '<missing>')
|
assert rv.data == b'<missing>'
|
||||||
|
|
||||||
with app.test_client() as c:
|
rv = c.get('/')
|
||||||
rv = c.get('/getsession')
|
assert rv.data == b'index'
|
||||||
assert rv.data == b'<missing>'
|
assert flask.session.get('data') == 'foo'
|
||||||
|
rv = c.post('/', data={}, follow_redirects=True)
|
||||||
|
assert rv.data == b'foo'
|
||||||
|
|
||||||
rv = c.get('/')
|
# This support requires a new Werkzeug version
|
||||||
assert rv.data == b'index'
|
if not hasattr(c, 'redirect_client'):
|
||||||
assert flask.session.get('data') == 'foo'
|
assert flask.session.get('data') == 'foo'
|
||||||
rv = c.post('/', data={}, follow_redirects=True)
|
|
||||||
assert rv.data == b'foo'
|
|
||||||
|
|
||||||
# This support requires a new Werkzeug version
|
rv = c.get('/getsession')
|
||||||
if not hasattr(c, 'redirect_client'):
|
assert rv.data == b'foo'
|
||||||
assert flask.session.get('data') == 'foo'
|
|
||||||
|
|
||||||
rv = c.get('/getsession')
|
def test_session_transactions():
|
||||||
assert rv.data == b'foo'
|
app = flask.Flask(__name__)
|
||||||
|
app.testing = True
|
||||||
|
app.secret_key = 'testing'
|
||||||
|
|
||||||
def test_session_transactions(self):
|
@app.route('/')
|
||||||
app = flask.Flask(__name__)
|
def index():
|
||||||
app.testing = True
|
return text_type(flask.session['foo'])
|
||||||
app.secret_key = 'testing'
|
|
||||||
|
|
||||||
@app.route('/')
|
with app.test_client() as c:
|
||||||
def index():
|
with c.session_transaction() as sess:
|
||||||
return text_type(flask.session['foo'])
|
assert len(sess) == 0
|
||||||
|
sess['foo'] = [42]
|
||||||
|
assert len(sess) == 1
|
||||||
|
rv = c.get('/')
|
||||||
|
assert rv.data == b'[42]'
|
||||||
|
with c.session_transaction() as sess:
|
||||||
|
assert len(sess) == 1
|
||||||
|
assert sess['foo'] == [42]
|
||||||
|
|
||||||
with app.test_client() as c:
|
def test_session_transactions_no_null_sessions():
|
||||||
with c.session_transaction() as sess:
|
app = flask.Flask(__name__)
|
||||||
assert len(sess) == 0
|
app.testing = True
|
||||||
sess['foo'] = [42]
|
|
||||||
assert len(sess) == 1
|
|
||||||
rv = c.get('/')
|
|
||||||
assert rv.data == b'[42]'
|
|
||||||
with c.session_transaction() as sess:
|
|
||||||
assert len(sess) == 1
|
|
||||||
assert sess['foo'] == [42]
|
|
||||||
|
|
||||||
def test_session_transactions_no_null_sessions(self):
|
with app.test_client() as c:
|
||||||
app = flask.Flask(__name__)
|
|
||||||
app.testing = True
|
|
||||||
|
|
||||||
with app.test_client() as c:
|
|
||||||
try:
|
|
||||||
with c.session_transaction() as sess:
|
|
||||||
pass
|
|
||||||
except RuntimeError as e:
|
|
||||||
assert 'Session backend did not open a session' in str(e)
|
|
||||||
else:
|
|
||||||
assert False, 'Expected runtime error'
|
|
||||||
|
|
||||||
def test_session_transactions_keep_context(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
app.testing = True
|
|
||||||
app.secret_key = 'testing'
|
|
||||||
|
|
||||||
with app.test_client() as c:
|
|
||||||
rv = c.get('/')
|
|
||||||
req = flask.request._get_current_object()
|
|
||||||
assert req is not None
|
|
||||||
with c.session_transaction():
|
|
||||||
assert req is flask.request._get_current_object()
|
|
||||||
|
|
||||||
def test_session_transaction_needs_cookies(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
app.testing = True
|
|
||||||
c = app.test_client(use_cookies=False)
|
|
||||||
try:
|
try:
|
||||||
with c.session_transaction() as s:
|
with c.session_transaction() as sess:
|
||||||
pass
|
pass
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
assert 'cookies' in str(e)
|
assert 'Session backend did not open a session' in str(e)
|
||||||
else:
|
else:
|
||||||
assert False, 'Expected runtime error'
|
assert False, 'Expected runtime error'
|
||||||
|
|
||||||
def test_test_client_context_binding(self):
|
def test_session_transactions_keep_context():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
app.testing = True
|
||||||
@app.route('/')
|
app.secret_key = 'testing'
|
||||||
def index():
|
|
||||||
flask.g.value = 42
|
|
||||||
return 'Hello World!'
|
|
||||||
|
|
||||||
@app.route('/other')
|
with app.test_client() as c:
|
||||||
def other():
|
rv = c.get('/')
|
||||||
1 // 0
|
req = flask.request._get_current_object()
|
||||||
|
assert req is not None
|
||||||
|
with c.session_transaction():
|
||||||
|
assert req is flask.request._get_current_object()
|
||||||
|
|
||||||
with app.test_client() as c:
|
def test_session_transaction_needs_cookies():
|
||||||
resp = c.get('/')
|
app = flask.Flask(__name__)
|
||||||
assert flask.g.value == 42
|
app.testing = True
|
||||||
assert resp.data == b'Hello World!'
|
c = app.test_client(use_cookies=False)
|
||||||
assert resp.status_code == 200
|
try:
|
||||||
|
with c.session_transaction() as s:
|
||||||
resp = c.get('/other')
|
|
||||||
assert not hasattr(flask.g, 'value')
|
|
||||||
assert b'Internal Server Error' in resp.data
|
|
||||||
assert resp.status_code == 500
|
|
||||||
flask.g.value = 23
|
|
||||||
|
|
||||||
try:
|
|
||||||
flask.g.value
|
|
||||||
except (AttributeError, RuntimeError):
|
|
||||||
pass
|
pass
|
||||||
else:
|
except RuntimeError as e:
|
||||||
raise AssertionError('some kind of exception expected')
|
assert 'cookies' in str(e)
|
||||||
|
else:
|
||||||
|
assert False, 'Expected runtime error'
|
||||||
|
|
||||||
def test_reuse_client(self):
|
def test_test_client_context_binding():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
c = app.test_client()
|
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
flask.g.value = 42
|
||||||
|
return 'Hello World!'
|
||||||
|
|
||||||
with c:
|
@app.route('/other')
|
||||||
assert c.get('/').status_code == 404
|
def other():
|
||||||
|
1 // 0
|
||||||
|
|
||||||
with c:
|
with app.test_client() as c:
|
||||||
assert c.get('/').status_code == 404
|
resp = c.get('/')
|
||||||
|
assert flask.g.value == 42
|
||||||
|
assert resp.data == b'Hello World!'
|
||||||
|
assert resp.status_code == 200
|
||||||
|
|
||||||
def test_test_client_calls_teardown_handlers(self):
|
resp = c.get('/other')
|
||||||
app = flask.Flask(__name__)
|
assert not hasattr(flask.g, 'value')
|
||||||
called = []
|
assert b'Internal Server Error' in resp.data
|
||||||
@app.teardown_request
|
assert resp.status_code == 500
|
||||||
def remember(error):
|
flask.g.value = 23
|
||||||
called.append(error)
|
|
||||||
|
|
||||||
with app.test_client() as c:
|
try:
|
||||||
assert called == []
|
flask.g.value
|
||||||
c.get('/')
|
except (AttributeError, RuntimeError):
|
||||||
assert called == []
|
pass
|
||||||
|
else:
|
||||||
|
raise AssertionError('some kind of exception expected')
|
||||||
|
|
||||||
|
def test_reuse_client():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
c = app.test_client()
|
||||||
|
|
||||||
|
with c:
|
||||||
|
assert c.get('/').status_code == 404
|
||||||
|
|
||||||
|
with c:
|
||||||
|
assert c.get('/').status_code == 404
|
||||||
|
|
||||||
|
def test_test_client_calls_teardown_handlers():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
called = []
|
||||||
|
@app.teardown_request
|
||||||
|
def remember(error):
|
||||||
|
called.append(error)
|
||||||
|
|
||||||
|
with app.test_client() as c:
|
||||||
|
assert called == []
|
||||||
|
c.get('/')
|
||||||
|
assert called == []
|
||||||
|
assert called == [None]
|
||||||
|
|
||||||
|
del called[:]
|
||||||
|
with app.test_client() as c:
|
||||||
|
assert called == []
|
||||||
|
c.get('/')
|
||||||
|
assert called == []
|
||||||
|
c.get('/')
|
||||||
assert called == [None]
|
assert called == [None]
|
||||||
|
assert called == [None, None]
|
||||||
|
|
||||||
del called[:]
|
def test_full_url_request():
|
||||||
with app.test_client() as c:
|
app = flask.Flask(__name__)
|
||||||
assert called == []
|
app.testing = True
|
||||||
c.get('/')
|
|
||||||
assert called == []
|
|
||||||
c.get('/')
|
|
||||||
assert called == [None]
|
|
||||||
assert called == [None, None]
|
|
||||||
|
|
||||||
def test_full_url_request(self):
|
@app.route('/action', methods=['POST'])
|
||||||
app = flask.Flask(__name__)
|
def action():
|
||||||
app.testing = True
|
return 'x'
|
||||||
|
|
||||||
@app.route('/action', methods=['POST'])
|
with app.test_client() as c:
|
||||||
def action():
|
rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})
|
||||||
return 'x'
|
assert rv.status_code == 200
|
||||||
|
assert 'gin' in flask.request.form
|
||||||
|
assert 'vodka' in flask.request.args
|
||||||
|
|
||||||
with app.test_client() as c:
|
def test_subdomain():
|
||||||
rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})
|
app = flask.Flask(__name__)
|
||||||
assert rv.status_code == 200
|
app.config['SERVER_NAME'] = 'example.com'
|
||||||
assert 'gin' in flask.request.form
|
@app.route('/', subdomain='<company_id>')
|
||||||
assert 'vodka' in flask.request.args
|
def view(company_id):
|
||||||
|
return company_id
|
||||||
|
|
||||||
class TestSubdomain(object):
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def app(self, request):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
app.config['SERVER_NAME'] = 'example.com'
|
|
||||||
|
|
||||||
ctx = app.test_request_context()
|
|
||||||
ctx.push()
|
|
||||||
|
|
||||||
def teardown():
|
|
||||||
if ctx is not None:
|
|
||||||
ctx.pop()
|
|
||||||
request.addfinalizer(teardown)
|
|
||||||
return app
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def client(self, app):
|
|
||||||
return app.test_client()
|
|
||||||
|
|
||||||
def test_subdomain(self, app, client):
|
|
||||||
@app.route('/', subdomain='<company_id>')
|
|
||||||
def view(company_id):
|
|
||||||
return company_id
|
|
||||||
|
|
||||||
|
with app.test_request_context():
|
||||||
url = flask.url_for('view', company_id='xxx')
|
url = flask.url_for('view', company_id='xxx')
|
||||||
response = client.get(url)
|
|
||||||
|
|
||||||
assert 200 == response.status_code
|
with app.test_client() as c:
|
||||||
assert b'xxx' == response.data
|
response = c.get(url)
|
||||||
|
|
||||||
|
assert 200 == response.status_code
|
||||||
|
assert b'xxx' == response.data
|
||||||
|
|
||||||
def test_nosubdomain(self, app, client):
|
def test_nosubdomain():
|
||||||
@app.route('/<company_id>')
|
app = flask.Flask(__name__)
|
||||||
def view(company_id):
|
app.config['SERVER_NAME'] = 'example.com'
|
||||||
return company_id
|
@app.route('/<company_id>')
|
||||||
|
def view(company_id):
|
||||||
|
return company_id
|
||||||
|
|
||||||
|
with app.test_request_context():
|
||||||
url = flask.url_for('view', company_id='xxx')
|
url = flask.url_for('view', company_id='xxx')
|
||||||
response = client.get(url)
|
|
||||||
|
|
||||||
assert 200 == response.status_code
|
with app.test_client() as c:
|
||||||
assert b'xxx' == response.data
|
response = c.get(url)
|
||||||
|
|
||||||
|
assert 200 == response.status_code
|
||||||
|
assert b'xxx' == response.data
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue