Resolve client address from Forwarded and X-Forwarded-* headers
This commit is contained in:
@@ -132,6 +132,37 @@ class WebSocketTest(unittest.TestCase):
|
||||
self.assertEqual(1, len(sent_messages))
|
||||
self.assertEqual({'type': 'websocket.accept'}, sent_messages[0])
|
||||
|
||||
@async_test
|
||||
async def test_client_forwarded_header(self):
|
||||
async def send(message):
|
||||
pass
|
||||
|
||||
async def receive():
|
||||
return {'type': 'websocket.connect'}
|
||||
|
||||
scope = {
|
||||
'type': 'websocket',
|
||||
'path': '/echo',
|
||||
'query_string': b'',
|
||||
'scheme': 'ws',
|
||||
'client': ('127.0.0.1', 12345),
|
||||
'server': ('127.0.0.1', 80),
|
||||
'headers': [(b'forwarded', b'for=203.0.113.5:1234')],
|
||||
}
|
||||
ws = AsgiWebSocket(scope, receive, send)
|
||||
self.assertEqual(('203.0.113.5', 1234), ws.client)
|
||||
|
||||
scope['headers'] = [
|
||||
(b'x-forwarded-for', b'203.0.113.5, 70.41.3.18'),
|
||||
(b'x-forwarded-port', b'8443'),
|
||||
]
|
||||
ws = AsgiWebSocket(scope, receive, send)
|
||||
self.assertEqual(('203.0.113.5', 8443), ws.client)
|
||||
|
||||
scope['headers'] = []
|
||||
ws = AsgiWebSocket(scope, receive, send)
|
||||
self.assertEqual(('127.0.0.1', 12345), ws.client)
|
||||
|
||||
@async_test
|
||||
async def test_websocket_scope_without_scheme(self):
|
||||
# Daphne omits the optional `scheme` key from websocket scopes.
|
||||
|
||||
Reference in New Issue
Block a user