|
2 | 2 | from datetime import datetime, timedelta
|
3 | 3 | from StringIO import StringIO
|
4 | 4 |
|
| 5 | +from django.conf import settings |
5 | 6 | from django.core.handlers.modpython import ModPythonRequest
|
6 | 7 | from django.core.handlers.wsgi import WSGIRequest, LimitedStream
|
7 | 8 | from django.http import HttpRequest, HttpResponse, parse_cookie
|
8 | 9 | from django.utils import unittest
|
9 | 10 | from django.utils.http import cookie_date
|
10 | 11 |
|
| 12 | + |
11 | 13 | class RequestsTests(unittest.TestCase):
|
12 | 14 | def test_httprequest(self):
|
13 | 15 | request = HttpRequest()
|
@@ -58,6 +60,94 @@ def test_httprequest_location(self):
|
58 | 60 | self.assertEqual(request.build_absolute_uri(location="/path/with:colons"),
|
59 | 61 | 'http://www.example.com/path/with:colons')
|
60 | 62 |
|
| 63 | + def test_http_get_host(self): |
| 64 | + old_USE_X_FORWARDED_HOST = settings.USE_X_FORWARDED_HOST |
| 65 | + try: |
| 66 | + settings.USE_X_FORWARDED_HOST = False |
| 67 | + |
| 68 | + # Check if X_FORWARDED_HOST is provided. |
| 69 | + request = HttpRequest() |
| 70 | + request.META = { |
| 71 | + u'HTTP_X_FORWARDED_HOST': u'forward.com', |
| 72 | + u'HTTP_HOST': u'example.com', |
| 73 | + u'SERVER_NAME': u'internal.com', |
| 74 | + u'SERVER_PORT': 80, |
| 75 | + } |
| 76 | + # X_FORWARDED_HOST is ignored. |
| 77 | + self.assertEqual(request.get_host(), 'example.com') |
| 78 | + |
| 79 | + # Check if X_FORWARDED_HOST isn't provided. |
| 80 | + request = HttpRequest() |
| 81 | + request.META = { |
| 82 | + u'HTTP_HOST': u'example.com', |
| 83 | + u'SERVER_NAME': u'internal.com', |
| 84 | + u'SERVER_PORT': 80, |
| 85 | + } |
| 86 | + self.assertEqual(request.get_host(), 'example.com') |
| 87 | + |
| 88 | + # Check if HTTP_HOST isn't provided. |
| 89 | + request = HttpRequest() |
| 90 | + request.META = { |
| 91 | + u'SERVER_NAME': u'internal.com', |
| 92 | + u'SERVER_PORT': 80, |
| 93 | + } |
| 94 | + self.assertEqual(request.get_host(), 'internal.com') |
| 95 | + |
| 96 | + # Check if HTTP_HOST isn't provided, and we're on a nonstandard port |
| 97 | + request = HttpRequest() |
| 98 | + request.META = { |
| 99 | + u'SERVER_NAME': u'internal.com', |
| 100 | + u'SERVER_PORT': 8042, |
| 101 | + } |
| 102 | + self.assertEqual(request.get_host(), 'internal.com:8042') |
| 103 | + |
| 104 | + finally: |
| 105 | + settings.USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST |
| 106 | + |
| 107 | + def test_http_get_host_with_x_forwarded_host(self): |
| 108 | + old_USE_X_FORWARDED_HOST = settings.USE_X_FORWARDED_HOST |
| 109 | + try: |
| 110 | + settings.USE_X_FORWARDED_HOST = True |
| 111 | + |
| 112 | + # Check if X_FORWARDED_HOST is provided. |
| 113 | + request = HttpRequest() |
| 114 | + request.META = { |
| 115 | + u'HTTP_X_FORWARDED_HOST': u'forward.com', |
| 116 | + u'HTTP_HOST': u'example.com', |
| 117 | + u'SERVER_NAME': u'internal.com', |
| 118 | + u'SERVER_PORT': 80, |
| 119 | + } |
| 120 | + # X_FORWARDED_HOST is obeyed. |
| 121 | + self.assertEqual(request.get_host(), 'forward.com') |
| 122 | + |
| 123 | + # Check if X_FORWARDED_HOST isn't provided. |
| 124 | + request = HttpRequest() |
| 125 | + request.META = { |
| 126 | + u'HTTP_HOST': u'example.com', |
| 127 | + u'SERVER_NAME': u'internal.com', |
| 128 | + u'SERVER_PORT': 80, |
| 129 | + } |
| 130 | + self.assertEqual(request.get_host(), 'example.com') |
| 131 | + |
| 132 | + # Check if HTTP_HOST isn't provided. |
| 133 | + request = HttpRequest() |
| 134 | + request.META = { |
| 135 | + u'SERVER_NAME': u'internal.com', |
| 136 | + u'SERVER_PORT': 80, |
| 137 | + } |
| 138 | + self.assertEqual(request.get_host(), 'internal.com') |
| 139 | + |
| 140 | + # Check if HTTP_HOST isn't provided, and we're on a nonstandard port |
| 141 | + request = HttpRequest() |
| 142 | + request.META = { |
| 143 | + u'SERVER_NAME': u'internal.com', |
| 144 | + u'SERVER_PORT': 8042, |
| 145 | + } |
| 146 | + self.assertEqual(request.get_host(), 'internal.com:8042') |
| 147 | + |
| 148 | + finally: |
| 149 | + settings.USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST |
| 150 | + |
61 | 151 | def test_near_expiration(self):
|
62 | 152 | "Cookie will expire when an near expiration time is provided"
|
63 | 153 | response = HttpResponse()
|
|
0 commit comments