Skip to content

Commit

Permalink
Set default attributes on WebRequest (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinowski authored Feb 14, 2022
1 parent 6a50939 commit eae1ff0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
5 changes: 2 additions & 3 deletions daphne/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,9 +79,6 @@ def process(self):
# 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"

Expand Down
49 changes: 49 additions & 0 deletions tests/test_http_protocol.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit eae1ff0

Please sign in to comment.