Skip to content

Commit

Permalink
Correct tests
Browse files Browse the repository at this point in the history
We've discovered that passing Unicode in Host actually works, except for
test client limitations on Python 2 - and the only things that don't
work are non-printable characters.
  • Loading branch information
jarek committed Nov 13, 2018
1 parent ed9775f commit 9b0bd91
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions tests/test_reqctx.py
Expand Up @@ -234,13 +234,44 @@ def index():
def test_bad_environ_raises_bad_request():
app = flask.Flask(__name__)

# We cannot use app.test_client() for the Unicode-rich Host header,
# because werkzeug enforces latin1 on Python 2.
# However it works when actually passed to the server.

from flask.testing import make_test_environ_builder
builder = make_test_environ_builder(app)
environ = builder.get_environ()

# use a non-printable character in the Host - this is key to this test
environ['HTTP_HOST'] = u'\x8a'

with app.request_context(environ):
response = app.full_dispatch_request()
assert response.status_code == 400


def test_environ_for_valid_idna_completes():
app = flask.Flask(__name__)

@app.route('/')
def index():
# shouldn't get here anyway
assert False
return 'Hello World!'

response = app.test_client().get('/', headers={'host': 'ąśź.com'})
assert response.status_code == 400
# We cannot use app.test_client() for the Unicode-rich Host header,
# because werkzeug enforces latin1 on Python 2.
# However it works when actually passed to the server.

from flask.testing import make_test_environ_builder
builder = make_test_environ_builder(app)
environ = builder.get_environ()

# these characters are all IDNA-compatible
environ['HTTP_HOST'] = u'ąśźäüжŠßя.com'

with app.request_context(environ):
response = app.full_dispatch_request()

assert response.status_code == 200


def test_normal_environ_completes():
Expand Down

0 comments on commit 9b0bd91

Please sign in to comment.