daphne/tests/test_utils.py

115 lines
2.9 KiB
Python
Raw Normal View History

# coding: utf8
2017-11-26 05:23:54 +03:00
from unittest import TestCase
2017-11-29 05:00:15 +03:00
from twisted.web.http_headers import Headers
2017-11-26 05:23:54 +03:00
from daphne.utils import parse_x_forwarded_for
class TestXForwardedForHttpParsing(TestCase):
"""
Tests that the parse_x_forwarded_for util correctly parses twisted Header.
"""
def test_basic(self):
headers = Headers({
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": [b"10.1.2.3"],
b"X-Forwarded-Port": [b"1234"]
})
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
result = parse_x_forwarded_for(headers)
2017-11-26 00:40:15 +03:00
self.assertEqual(result, ["10.1.2.3", 1234])
2017-11-26 05:23:54 +03:00
self.assertIsInstance(result[0], str)
def test_address_only(self):
headers = Headers({
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": [b"10.1.2.3"],
})
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["10.1.2.3", 0]
)
def test_v6_address(self):
headers = Headers({
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": [b"1043::a321:0001, 10.0.5.6"],
})
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["1043::a321:0001", 0]
)
def test_multiple_proxys(self):
headers = Headers({
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": [b"10.1.2.3, 10.1.2.4"],
})
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["10.1.2.3", 0]
)
def test_original(self):
headers = Headers({})
self.assertEqual(
2017-11-26 00:40:15 +03:00
parse_x_forwarded_for(headers, original=["127.0.0.1", 80]),
["127.0.0.1", 80]
)
def test_no_original(self):
headers = Headers({})
self.assertIsNone(parse_x_forwarded_for(headers))
class TestXForwardedForWsParsing(TestCase):
"""
Tests that the parse_x_forwarded_for util correctly parses dict headers.
"""
def test_basic(self):
headers = {
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": b"10.1.2.3",
b"X-Forwarded-Port": b"1234",
}
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["10.1.2.3", 1234]
)
def test_address_only(self):
headers = {
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": b"10.1.2.3",
}
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["10.1.2.3", 0]
)
def test_v6_address(self):
headers = {
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": [b"1043::a321:0001, 10.0.5.6"],
}
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["1043::a321:0001", 0]
)
2017-11-26 05:23:54 +03:00
def test_multiple_proxies(self):
headers = {
2017-11-26 00:40:15 +03:00
b"X-Forwarded-For": b"10.1.2.3, 10.1.2.4",
}
self.assertEqual(
parse_x_forwarded_for(headers),
2017-11-26 00:40:15 +03:00
["10.1.2.3", 0]
)
def test_original(self):
headers = {}
self.assertEqual(
2017-11-26 00:40:15 +03:00
parse_x_forwarded_for(headers, original=["127.0.0.1", 80]),
["127.0.0.1", 80]
)
def test_no_original(self):
headers = {}
self.assertIsNone(parse_x_forwarded_for(headers))