* Python 2 fix for host address
This is a copy of
57051a48cd
for the Websocket protocol.
In Python 2, Twisted returns a byte string for the host address, while
the spec requires a unicode string. A simple cast gives us consistency.
* Test suite for websocket tests
This commit
* introduces some new helpers to test the Websocket protocol
* renames the old ASGITestCase class to ASGIHTTPTestCase, and
introduces a test case for testing Websockets
* moves some helper methods that are shared between HTTP and Websockets
into a mutual base class
* uses the new helpers to simplfiy the existing tests
* and adds a couple new tests.
* add websocket timeout and websocket connetion timeout to Cli. Add support for infinite time to websocket timeout and websocket connection timeout
* change test
* Fix: Always call Request.write()
The spec says 'content' is an optional key, defaulting to b''.
But before this commit, if 'content' wasn't specified, Request.write()
was not called. In conjunction with setting 'more_content' to True,
this would result in nothing being written on the transport. If
'content' was set to b'' instead, the HTTP preamble and any headers were
written as expected. That smells like a bug, so I'm making sure we're
always calling Request.write().
* Require status key in first message to response channel
Previous to this commit, it was possible to not pass in a 'status' key.
This would result in any passed in headers being ignored as well.
Instead of relying on user data ('status' being present or not), this
commit now enforces that the first message to a response channel is
indead a HTTP Response-style message, and hence contains status. It will
complain loudly if that isn't the case.
* Helper for getting HTTP Response for a given channel message
To test Daphne's message-to-HTTP part, we need an easy way to fetch the
HTTP response for a given response channel message. I borrowed the
approach from Andrew's existing code. I feel like we might be able to do
with less scaffolding at some point, but didn't have time to
investigate. It's good enough for now.
* Add assert method to check a response for spec conformance
Similarly to the method for checking HTTP requests for spec conformance,
we're adding a method to do the same for HTTP responses. This one is a bit
less exciting because we're testing raw HTTP responses.
* Add Hypothesis tests for HTTP responses
Similarly to what I did for HTTP requests, this commit adds a couple
test that try to check different parts of the ASGI spec. Because going
from message to HTTP response is more straightforward than going from
HTTP request to channel message, there's not a whole lot going on here.
* Add Hypothesis for property-based tests
Hypothesis:
"It works by letting you write tests that assert that
something should be true for every case, not just the ones you happen to
think of."
I think it's well suited for the task of ensuring Daphne conforms to the
ASGI specification.
* Fix accidental cast to byte string under Python 2
While grepping for calls to str(), I found this bit which looks like a
cast to unicode was intended under Python 2.
* ASGITestCase - checking channel messages for spec conformance
This commit introduces a new test case class, with it's main method
assert_valid_http_request_message. The idea is
that this method is a translation of the ASGI spec to code, and can be
used to check channel messages for conformance with that part of the
spec.
I plan to add further methods for other parts of the spec.
* Add Hypothesis strategies for generating HTTP requests
Hypothesis, our framework for test data generation, contains only
general so-called strategies for generating data. This commit adds a few
which will be useful for generating the data for our tests.
Alos see http://hypothesis.readthedocs.io/en/latest/data.html.
* Add and convert tests for HTTP requests
This commit introduces a few Hypothesis tests to test the HTTP request
part of the specification. To keep things organized, I split the
existing tests module into two: one concerned with requests, and one
concerned with responses. I anticipate that we'll also add modules for
chunks and server push later.
daphne already had tests for the HTTP protocol. Some of them I converted
to Hypothesis tests to increase what was tested. Some were also
concerned with HTTP responses, so they were moved to the new response
module. And three tests were concerned with proxy behaviour, which I
wasn't sure about, and I just kept them as-is, but also moved them
to the request tests.
* Fix byte string issue in Python 2
Twisted seems to return a byte string for the client and server IP
address. It is easily rectified by casting to the required unicode
string. Also added a test to ensure this is also handled correctly in
the X-Forwarded-For header parsing.
* Check order of header values
I'm in the process of updating the ASGI spec to require that the order
of header values is kept. To match that work, I'm adding matching
assertions to the tests.
The code unfortunately is not as elegant as I'd like, but then it's only
a result of the underlying HTTP spec.
* Suppress warning about slow test
The kitchen sink test expectedly can be slow. So far it wasn't slow
enough for hypothesis to trigger a warning, but today Travis must've had
a bad day. I think for this test is is acceptable to exceed hypothesis'
warning threshold.
* Test against Python 3.4 and multiple Twisted versions
This commit adds tox to be able to test against different dependencies
locally. We agreed that Python 3.4 should be supported across all Channels
projects, so it is also added with this commit.
Furthermore, I think it makes sense to support a broad range of Twisted
releases, as users of daphne are not unlikely to have other Twisted code
running. It's not feasible to test against all releases since 16.0, and
it would require constant maintenance to add new releases as they come
out. So I opted to keep things simple for now, and only test against the
oldest supported and the current Twisted release.
I did consider @jpic's great idea from
https://github.com/django/daphne/pull/19 to just use tox to avoid having
to duplicate the dependency matrix. But it does lead to slower test runs
as it bypasses Travis' caching, and is slightly more verbose.
* Require asgiref 1.0 and use receive instead of receive_many
As both daphne and asgiref had a 1.0 release, I think it makes sense to
require the presumably more stable asgiref 1.0. It's also a good
occasion to fix the deprecation warnings when running the tests by
switching to receive instead of receive_many.
* Document supported Python and Twisted versions