Skip to content

Add port option for ChannelsLiveServerTestCase #2136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions channels/testing/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ class ChannelsLiveServerTestCase(TransactionTestCase):
ProtocolServerProcess = DaphneProcess
static_wrapper = ASGIStaticFilesHandler
serve_static = True
port = 0

@property
def live_server_url(self):
return "http://%s:%s" % (self.host, self._port)
return "http://%s:%s" % (self.host, self.port)

@property
def live_server_ws_url(self):
return "ws://%s:%s" % (self.host, self._port)
return "ws://%s:%s" % (self.host, self.port)

def _pre_setup(self):
for connection in connections.all():
Expand All @@ -57,10 +58,14 @@ def _pre_setup(self):
make_application,
static_wrapper=self.static_wrapper if self.serve_static else None,
)
self._server_process = self.ProtocolServerProcess(self.host, get_application)
self._server_process = self.ProtocolServerProcess(
self.host,
get_application,
port=self.port
)
self._server_process.start()
self._server_process.ready.wait()
self._port = self._server_process.port.value
self.port = self._server_process.port.value

def _post_teardown(self):
self._server_process.terminate()
Expand Down
6 changes: 6 additions & 0 deletions docs/topics/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ standard Django ``LiveServerTestCase``:
},
}

port
~~~~

Subclass ``ChannelsLiveServerTestCase`` with ``port = {port_number}`` to bind
both the HTTP and WebSocket servers to a specific port.

serve_static
~~~~~~~~~~~~

Expand Down
Loading