daphne/tests/http_strategies.py

138 lines
3.6 KiB
Python
Raw Permalink Normal View History

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
import string
2018-02-02 08:02:37 +03:00
from urllib import parse
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
from hypothesis import strategies
2017-11-26 00:40:15 +03:00
HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT"]
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
# Unicode characters of the "Letter" category
2017-11-26 00:40:15 +03:00
letters = strategies.characters(whitelist_categories=("Lu", "Ll", "Lt", "Lm", "Lo", "Nl"))
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
def http_method():
return strategies.sampled_from(HTTP_METHODS)
def _http_path_portion():
2017-11-26 00:40:15 +03:00
alphabet = string.ascii_letters + string.digits + "-._~"
return strategies.text(min_size=1, average_size=10, max_size=128, alphabet=alphabet)
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
def http_path():
"""
Returns a URL path (not encoded).
"""
return strategies.lists(
2017-11-26 05:23:54 +03:00
_http_path_portion(),
min_size=0,
max_size=10,
).map(lambda s: "/" + "/".join(s))
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
def http_body():
"""
2017-11-26 05:23:54 +03:00
Returns random binary body data.
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
"""
return strategies.binary(min_size=0, average_size=600, max_size=1500)
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
def valid_bidi(value):
"""
Rejects strings which nonsensical Unicode text direction flags.
Relying on random Unicode characters means that some combinations don't make sense, from a
direction of text point of view. This little helper just rejects those.
"""
try:
2017-11-26 00:40:15 +03:00
value.encode("idna")
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
except UnicodeError:
return False
else:
return True
def _domain_label():
return strategies.text(
2017-11-26 05:23:54 +03:00
alphabet=letters,
min_size=1,
average_size=6,
max_size=63,
).filter(valid_bidi)
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
def international_domain_name():
"""
Returns a byte string of a domain name, IDNA-encoded.
"""
return strategies.lists(
2017-11-26 05:23:54 +03:00
_domain_label(),
min_size=2,
average_size=2,
).map(lambda s: (".".join(s)).encode("idna"))
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
def _query_param():
2017-11-26 05:23:54 +03:00
return strategies.text(
alphabet=letters,
min_size=1,
average_size=10,
max_size=255,
).map(lambda s: s.encode("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
def query_params():
"""
Returns a list of two-tuples byte strings, ready for encoding with urlencode.
We're aiming for a total length of a URL below 2083 characters, so this strategy
ensures that the total urlencoded query string is not longer than 1500 characters.
"""
return strategies.lists(
2017-11-26 05:23:54 +03:00
strategies.tuples(_query_param(), _query_param()),
min_size=0,
average_size=5,
).filter(lambda x: len(parse.urlencode(x)) < 1500)
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
def header_name():
"""
Strategy returning something that looks like a HTTP header field
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields suggests they are between 4
and 20 characters long
"""
return strategies.text(
2017-11-26 05:23:54 +03:00
alphabet=string.ascii_letters + string.digits + "-",
min_size=1,
max_size=30,
)
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
def header_value():
"""
Strategy returning something that looks like a HTTP header value
"For example, the Apache 2.3 server by default limits the size of each field to 8190 bytes"
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
"""
return strategies.text(
2017-11-26 00:40:15 +03:00
alphabet=string.ascii_letters + string.digits + string.punctuation + " /t",
2017-11-26 05:23:54 +03:00
min_size=1,
average_size=40,
max_size=8190,
).filter(lambda s: len(s.encode("utf8")) < 8190)
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
def headers():
"""
Strategy returning a list of tuples, containing HTTP header fields and their values.
"[Apache 2.3] there can be at most 100 header fields in a single request."
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
"""
return strategies.lists(
strategies.tuples(header_name(), header_value()),
2017-11-26 05:23:54 +03:00
min_size=0,
average_size=10,
max_size=100,
)