mirror of
https://github.com/django/daphne.git
synced 2024-11-21 15:36:33 +03:00
Set default attributes on WebRequest (#406)
This commit is contained in:
parent
6a5093982c
commit
eae1ff0df4
|
@ -50,6 +50,8 @@ class WebRequest(http.Request):
|
|||
) # Shorten it a bit, bytes wise
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.client_addr = None
|
||||
self.server_addr = None
|
||||
try:
|
||||
http.Request.__init__(self, *args, **kwargs)
|
||||
# Easy server link
|
||||
|
@ -77,9 +79,6 @@ class WebRequest(http.Request):
|
|||
# requires unicode string.
|
||||
self.client_addr = [str(self.client.host), self.client.port]
|
||||
self.server_addr = [str(self.host.host), self.host.port]
|
||||
else:
|
||||
self.client_addr = None
|
||||
self.server_addr = None
|
||||
|
||||
self.client_scheme = "https" if self.isSecure() else "http"
|
||||
|
||||
|
|
49
tests/test_http_protocol.py
Normal file
49
tests/test_http_protocol.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
import unittest
|
||||
|
||||
from daphne.http_protocol import WebRequest
|
||||
|
||||
|
||||
class MockServer:
|
||||
"""
|
||||
Mock server object for testing.
|
||||
"""
|
||||
|
||||
def protocol_connected(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class MockFactory:
|
||||
"""
|
||||
Mock factory object for testing.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.server = MockServer()
|
||||
|
||||
|
||||
class MockChannel:
|
||||
"""
|
||||
Mock channel object for testing.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.factory = MockFactory()
|
||||
self.transport = None
|
||||
|
||||
def getPeer(self, *args, **kwargs):
|
||||
return "peer"
|
||||
|
||||
def getHost(self, *args, **kwargs):
|
||||
return "host"
|
||||
|
||||
|
||||
class TestHTTPProtocol(unittest.TestCase):
|
||||
"""
|
||||
Tests the HTTP protocol classes.
|
||||
"""
|
||||
|
||||
def test_web_request_initialisation(self):
|
||||
channel = MockChannel()
|
||||
request = WebRequest(channel)
|
||||
self.assertIsNone(request.client_addr)
|
||||
self.assertIsNone(request.server_addr)
|
Loading…
Reference in New Issue
Block a user