Skip to content
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

Full test suite for HTTP requests #91

Merged
merged 8 commits into from
Mar 14, 2017
Merged

Full test suite for HTTP requests #91

merged 8 commits into from
Mar 14, 2017

Conversation

maiksprenger
Copy link
Contributor

@maiksprenger maiksprenger commented Mar 7, 2017

The main goal of this pull request is to add a couple tests for HTTP requests. All channel messages generated by those tests also get verified by a new method, which is a translation of the ASGI spec. Instead of using static test data, the tests employ hypothesis, which is rather good at generating data that breaks tests.

As part of this pull requests, some of the existing tests were removed, or moved into a new module. I also stumbled across two minor byte string issues that I could fix.

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.
While grepping for calls to str(), I found this bit which looks like a
cast to unicode was intended under Python 2.
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.
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.
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.
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.
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.
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.
@maiksprenger
Copy link
Contributor Author

I proposed some changes to the ASGI spec in django/channels#554, and updated this pull request with matching tests.
Travis had a slow day, so it triggered a warning from hypothesis about a slow test. I added a flag to mute the warning for this test, because we expect this test to be slow.

@andrewgodwin
Copy link
Member

Ugh, somehow I have missed looking at this for over a week. It looks good, I'll pull it in.

@andrewgodwin andrewgodwin merged commit 7f92a48 into django:master Mar 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants