* Migrate additional test modules away from the deprecated framework, taking care to demonstrate both unittest- and pytest-style testing. * Add an additional Cookie class and parse cookies in the Result object to facilitate cookie testing. * Extend BoundStream to implement more of the IO protocol so that it works with mock WSGI input objects (and potentially improves compatibility as a drop-in replacement for the native WSGI input object). * Disable pypy3 testing on Travis, since it uses a buggy cookie impl, and also we don't support pypy3 (yet) since overall it isn't fully baked. * Specify latest pypy version for Travis testing, which has improved cookie handling.
100 lines
2.6 KiB
Python
100 lines
2.6 KiB
Python
from falcon.request import Request
|
|
import falcon.testing as testing
|
|
|
|
|
|
def test_remote_addr_only():
|
|
req = Request(testing.create_environ(
|
|
host='example.com',
|
|
path='/access_route',
|
|
headers={
|
|
'Forwarded': ('for=192.0.2.43, for="[2001:db8:cafe::17]:555",'
|
|
'for="unknown", by=_hidden,for="\\"\\\\",'
|
|
'for="198\\.51\\.100\\.17\\:1236";'
|
|
'proto=https;host=example.com')
|
|
}))
|
|
|
|
assert req.remote_addr == '127.0.0.1'
|
|
|
|
|
|
def test_rfc_forwarded():
|
|
req = Request(testing.create_environ(
|
|
host='example.com',
|
|
path='/access_route',
|
|
headers={
|
|
'Forwarded': ('for=192.0.2.43,for=,'
|
|
'for="[2001:db8:cafe::17]:555",'
|
|
'for=x,'
|
|
'for="unknown", by=_hidden,for="\\"\\\\",'
|
|
'for="_don\\\"t_\\try_this\\\\at_home_\\42",'
|
|
'for="198\\.51\\.100\\.17\\:1236";'
|
|
'proto=https;host=example.com')
|
|
}))
|
|
|
|
compares = ['192.0.2.43', '2001:db8:cafe::17', 'x',
|
|
'unknown', '"\\', '_don"t_try_this\\at_home_42',
|
|
'198.51.100.17']
|
|
|
|
req.access_route == compares
|
|
|
|
# test cached
|
|
req.access_route == compares
|
|
|
|
|
|
def test_malformed_rfc_forwarded():
|
|
req = Request(testing.create_environ(
|
|
host='example.com',
|
|
path='/access_route',
|
|
headers={
|
|
'Forwarded': 'for'
|
|
}))
|
|
|
|
req.access_route == []
|
|
|
|
# test cached
|
|
req.access_route == []
|
|
|
|
|
|
def test_x_forwarded_for():
|
|
req = Request(testing.create_environ(
|
|
host='example.com',
|
|
path='/access_route',
|
|
headers={
|
|
'X-Forwarded-For': ('192.0.2.43, 2001:db8:cafe::17,'
|
|
'unknown, _hidden, 203.0.113.60')
|
|
}))
|
|
|
|
assert req.access_route == [
|
|
'192.0.2.43',
|
|
'2001:db8:cafe::17',
|
|
'unknown',
|
|
'_hidden',
|
|
'203.0.113.60'
|
|
]
|
|
|
|
|
|
def test_x_real_ip():
|
|
req = Request(testing.create_environ(
|
|
host='example.com',
|
|
path='/access_route',
|
|
headers={
|
|
'X-Real-IP': '2001:db8:cafe::17'
|
|
}))
|
|
|
|
assert req.access_route == ['2001:db8:cafe::17']
|
|
|
|
|
|
def test_remote_addr():
|
|
req = Request(testing.create_environ(
|
|
host='example.com',
|
|
path='/access_route'))
|
|
|
|
assert req.access_route == ['127.0.0.1']
|
|
|
|
|
|
def test_remote_addr_missing():
|
|
env = testing.create_environ(host='example.com', path='/access_route')
|
|
del env['REMOTE_ADDR']
|
|
|
|
req = Request(env)
|
|
assert req.access_route == []
|