2016-04-26 15:43:14 +03:00
|
|
|
# coding: utf8
|
Full test suite for HTTP requests (#91)
* 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.
2017-03-15 00:12:07 +03:00
|
|
|
"""
|
|
|
|
Tests for the HTTP response section of the ASGI spec
|
|
|
|
"""
|
2016-02-06 04:23:49 +03:00
|
|
|
from __future__ import unicode_literals
|
Full test suite for HTTP requests (#91)
* 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.
2017-03-15 00:12:07 +03:00
|
|
|
|
2016-02-06 04:23:49 +03:00
|
|
|
from unittest import TestCase
|
Full test suite for HTTP requests (#91)
* 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.
2017-03-15 00:12:07 +03:00
|
|
|
|
2016-02-06 04:23:49 +03:00
|
|
|
from asgiref.inmemory import ChannelLayer
|
HTTP responses: two fixes and some tests (#96)
* 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.
2017-03-23 01:55:28 +03:00
|
|
|
from hypothesis import given
|
2016-02-06 04:23:49 +03:00
|
|
|
from twisted.test import proto_helpers
|
|
|
|
|
HTTP responses: two fixes and some tests (#96)
* 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.
2017-03-23 01:55:28 +03:00
|
|
|
from daphne.http_protocol import HTTPFactory
|
|
|
|
from . import factories, http_strategies, testcases
|
|
|
|
|
|
|
|
|
2017-04-29 00:45:07 +03:00
|
|
|
class TestHTTPResponseSpec(testcases.ASGIHTTPTestCase):
|
HTTP responses: two fixes and some tests (#96)
* 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.
2017-03-23 01:55:28 +03:00
|
|
|
|
|
|
|
def test_minimal_response(self):
|
|
|
|
"""
|
|
|
|
Smallest viable example. Mostly verifies that our response building works.
|
|
|
|
"""
|
|
|
|
message = {'status': 200}
|
|
|
|
response = factories.response_for_message(message)
|
|
|
|
self.assert_valid_http_response_message(message, response)
|
|
|
|
self.assertIn(b'200 OK', response)
|
|
|
|
# Assert that the response is the last of the chunks.
|
|
|
|
# N.b. at the time of writing, Daphne did not support multiple response chunks,
|
|
|
|
# but still sends with Transfer-Encoding: chunked if no Content-Length header
|
|
|
|
# is specified (and maybe even if specified).
|
|
|
|
self.assertTrue(response.endswith(b'0\r\n\r\n'))
|
|
|
|
|
|
|
|
def test_status_code_required(self):
|
|
|
|
"""
|
|
|
|
Asserts that passing in the 'status' key is required.
|
|
|
|
|
|
|
|
Previous versions of Daphne did not enforce this, so this test is here
|
|
|
|
to make sure it stays required.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
factories.response_for_message({})
|
|
|
|
|
|
|
|
def test_status_code_is_transmitted(self):
|
|
|
|
"""
|
|
|
|
Tests that a custom status code is present in the response.
|
|
|
|
|
|
|
|
We can't really use hypothesis to test all sorts of status codes, because a lot
|
|
|
|
of them have meaning that is respected by Twisted. E.g. setting 204 (No Content)
|
|
|
|
as a status code results in Twisted discarding the body.
|
|
|
|
"""
|
|
|
|
message = {'status': 201} # 'Created'
|
|
|
|
response = factories.response_for_message(message)
|
|
|
|
self.assert_valid_http_response_message(message, response)
|
|
|
|
self.assertIn(b'201 Created', response)
|
|
|
|
|
|
|
|
@given(body=http_strategies.http_body())
|
|
|
|
def test_body_is_transmitted(self, body):
|
|
|
|
message = {'status': 200, 'content': body.encode('ascii')}
|
|
|
|
response = factories.response_for_message(message)
|
|
|
|
self.assert_valid_http_response_message(message, response)
|
|
|
|
|
|
|
|
@given(headers=http_strategies.headers())
|
|
|
|
def test_headers(self, headers):
|
|
|
|
# The ASGI spec requires us to lowercase our header names
|
|
|
|
message = {'status': 200, 'headers': [(name.lower(), value) for name, value in headers]}
|
|
|
|
response = factories.response_for_message(message)
|
|
|
|
# The assert_ method does the heavy lifting of checking that headers are
|
|
|
|
# as expected.
|
|
|
|
self.assert_valid_http_response_message(message, response)
|
|
|
|
|
|
|
|
@given(
|
|
|
|
headers=http_strategies.headers(),
|
|
|
|
body=http_strategies.http_body()
|
|
|
|
)
|
|
|
|
def test_kitchen_sink(self, headers, body):
|
|
|
|
"""
|
|
|
|
This tests tries to let Hypothesis find combinations of variables that result
|
|
|
|
in breaking our assumptions. But responses are less exciting than responses,
|
|
|
|
so there's not a lot going on here.
|
|
|
|
"""
|
|
|
|
message = {
|
|
|
|
'status': 202, # 'Accepted'
|
|
|
|
'headers': [(name.lower(), value) for name, value in headers],
|
|
|
|
'content': body.encode('ascii')
|
|
|
|
}
|
|
|
|
response = factories.response_for_message(message)
|
|
|
|
self.assert_valid_http_response_message(message, response)
|
2016-02-06 04:23:49 +03:00
|
|
|
|
|
|
|
|
Full test suite for HTTP requests (#91)
* 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.
2017-03-15 00:12:07 +03:00
|
|
|
class TestHTTPResponse(TestCase):
|
2016-02-06 04:23:49 +03:00
|
|
|
"""
|
|
|
|
Tests that the HTTP protocol class correctly generates and parses messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.channel_layer = ChannelLayer()
|
2017-04-01 17:18:43 +03:00
|
|
|
self.factory = HTTPFactory(self.channel_layer, send_channel="test!")
|
2016-02-06 04:23:49 +03:00
|
|
|
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
|
|
|
|
self.tr = proto_helpers.StringTransport()
|
|
|
|
self.proto.makeConnection(self.tr)
|
|
|
|
|
2016-08-29 02:03:42 +03:00
|
|
|
def test_http_disconnect_sets_path_key(self):
|
|
|
|
"""
|
2016-09-21 10:43:24 +03:00
|
|
|
Tests http disconnect has the path key set, see https://channels.readthedocs.io/en/latest/asgi.html#disconnect
|
2016-08-29 02:03:42 +03:00
|
|
|
"""
|
|
|
|
# Send a simple request to the protocol
|
|
|
|
self.proto.dataReceived(
|
|
|
|
b"GET /te%20st-%C3%A0/?foo=bar HTTP/1.1\r\n" +
|
|
|
|
b"Host: anywhere.com\r\n" +
|
|
|
|
b"\r\n"
|
|
|
|
)
|
|
|
|
# Get the request message
|
2017-01-31 04:24:17 +03:00
|
|
|
_, message = self.channel_layer.receive(["http.request"])
|
2016-08-29 02:03:42 +03:00
|
|
|
|
|
|
|
# Send back an example response
|
|
|
|
self.factory.dispatch_reply(
|
|
|
|
message['reply_channel'],
|
|
|
|
{
|
|
|
|
"status": 200,
|
|
|
|
"status_text": b"OK",
|
|
|
|
"content": b"DISCO",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the disconnection notification
|
2017-01-31 04:24:17 +03:00
|
|
|
_, disconnect_message = self.channel_layer.receive(["http.disconnect"])
|
2016-09-21 10:43:24 +03:00
|
|
|
self.assertEqual(disconnect_message['path'], "/te st-à/")
|