summaryrefslogtreecommitdiff
path: root/tests/utils/wsgi/tests.py
blob: 5b2ffdc24e1b997a2057b74b397735d6fe19b299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from raven.utils.testutils import TestCase
from raven.utils.wsgi import get_headers, get_host, get_environ


class GetHeadersTest(TestCase):
    def test_tuple_as_key(self):
        result = dict(get_headers({
            ('a', 'tuple'): 'foo',
        }))
        self.assertEquals(result, {})

    def test_coerces_http_name(self):
        result = dict(get_headers({
            'HTTP_ACCEPT': 'text/plain',
        }))
        self.assertIn('Accept', result)
        self.assertEquals(result['Accept'], 'text/plain')

    def test_coerces_content_type(self):
        result = dict(get_headers({
            'CONTENT_TYPE': 'text/plain',
        }))
        self.assertIn('Content-Type', result)
        self.assertEquals(result['Content-Type'], 'text/plain')

    def test_coerces_content_length(self):
        result = dict(get_headers({
            'CONTENT_LENGTH': '134',
        }))
        self.assertIn('Content-Length', result)
        self.assertEquals(result['Content-Length'], '134')


class GetEnvironTest(TestCase):
    def test_has_remote_addr(self):
        result = dict(get_environ({'REMOTE_ADDR': '127.0.0.1'}))
        self.assertIn('REMOTE_ADDR', result)
        self.assertEquals(result['REMOTE_ADDR'], '127.0.0.1')

    def test_has_server_name(self):
        result = dict(get_environ({'SERVER_NAME': '127.0.0.1'}))
        self.assertIn('SERVER_NAME', result)
        self.assertEquals(result['SERVER_NAME'], '127.0.0.1')

    def test_has_server_port(self):
        result = dict(get_environ({'SERVER_PORT': 80}))
        self.assertIn('SERVER_PORT', result)
        self.assertEquals(result['SERVER_PORT'], 80)

    def test_hides_wsgi_input(self):
        result = list(get_environ({'wsgi.input': 'foo'}))
        self.assertNotIn('wsgi.input', result)


class GetHostTest(TestCase):
    def test_http_x_forwarded_host(self):
        result = get_host({'HTTP_X_FORWARDED_HOST': 'example.com'})
        self.assertEquals(result, 'example.com')

    def test_http_host(self):
        result = get_host({'HTTP_HOST': 'example.com'})
        self.assertEquals(result, 'example.com')

    def test_http_strips_port(self):
        result = get_host({
            'wsgi.url_scheme': 'http',
            'SERVER_NAME': 'example.com',
            'SERVER_PORT': '80',
        })
        self.assertEquals(result, 'example.com')

    def test_https_strips_port(self):
        result = get_host({
            'wsgi.url_scheme': 'https',
            'SERVER_NAME': 'example.com',
            'SERVER_PORT': '443',
        })
        self.assertEquals(result, 'example.com')

    def test_http_nonstandard_port(self):
        result = get_host({
            'wsgi.url_scheme': 'http',
            'SERVER_NAME': 'example.com',
            'SERVER_PORT': '81',
        })
        self.assertEquals(result, 'example.com:81')