Changeset 6662
- Timestamp:
- 11/10/07 21:55:44 (10 months ago)
- Files:
-
- django/trunk/django/core/handlers/base.py (modified) (3 diffs)
- django/trunk/django/core/handlers/modpython.py (modified) (1 diff)
- django/trunk/django/core/handlers/wsgi.py (modified) (2 diffs)
- django/trunk/django/http/__init__.py (modified) (1 diff)
- django/trunk/django/http/utils.py (added)
- django/trunk/django/middleware/http.py (modified) (2 diffs)
- django/trunk/django/test/client.py (modified) (1 diff)
- django/trunk/tests/regressiontests/test_client_regress/models.py (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/handlers/base.py
r6296 r6662 5 5 6 6 class BaseHandler(object): 7 # Changes that are always applied to a response (in this order). 8 response_fixes = [http.fix_location_header, 9 http.conditional_content_removal] 10 7 11 def __init__(self): 8 12 self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None … … 51 55 def get_response(self, request): 52 56 "Returns an HttpResponse object for the given HttpRequest" 53 response = self._real_get_response(request)54 return fix_location_header(request, response)55 56 def _real_get_response(self, request):57 57 from django.core import exceptions, urlresolvers 58 58 from django.core.mail import mail_admins … … 135 135 return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) 136 136 137 def fix_location_header(request, response): 138 """ 139 Ensure that we always use an absolute URI in any location header in the 140 response. This is required by RFC 2616, section 14.30. 137 def apply_response_fixes(self, request, response): 138 """ 139 Applies each of the functions in self.response_fixes to the request and 140 response, modifying the response in the process. Returns the new 141 response. 142 """ 143 for func in self.response_fixes: 144 response = func(request, response) 145 return response 141 146 142 Code constructing response objects is free to insert relative paths and143 this function converts them to absolute paths.144 """145 if 'Location' in response and request.get_host():146 response['Location'] = request.build_absolute_uri(response['Location'])147 return response148 django/trunk/django/core/handlers/modpython.py
r6550 r6662 163 163 for middleware_method in self._response_middleware: 164 164 response = middleware_method(request, response) 165 response = self.apply_response_fixes(request, response) 165 166 finally: 166 167 dispatcher.send(signal=signals.request_finished) django/trunk/django/core/handlers/wsgi.py
r6592 r6662 208 208 for middleware_method in self._response_middleware: 209 209 response = middleware_method(request, response) 210 response = self.apply_response_fixes(request, response) 210 211 finally: 211 212 dispatcher.send(signal=signals.request_finished) … … 221 222 start_response(status, response_headers) 222 223 return response 224 django/trunk/django/http/__init__.py
r6549 r6662 6 6 from django.utils.datastructures import MultiValueDict, FileDict 7 7 from django.utils.encoding import smart_str, iri_to_uri, force_unicode 8 from utils import * 8 9 9 10 RESERVED_CHARS="!*'();:@&=+$,/?%#[]" django/trunk/django/middleware/http.py
r6635 r6662 6 6 Last-Modified header, and the request has If-None-Match or 7 7 If-Modified-Since, the response is replaced by an HttpNotModified. 8 9 Removes the content from any response to a HEAD request.10 8 11 9 Also sets the Date and Content-Length response-headers. … … 19 17 if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None) 20 18 if if_none_match == response['ETag']: 21 response.status_code = 304 22 response.content = '' 23 response['Content-Length'] = '0' 19 # Setting the status is enough here. The response handling path 20 # automatically removes content for this status code (in 21 # http.conditional_content_removal()). 22 response.status = 304 24 23 25 24 if response.has_header('Last-Modified'): 26 25 if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None) 27 26 if if_modified_since == response['Last-Modified']: 28 response.status_code = 304 29 response.content = '' 30 response['Content-Length'] = '0' 31 32 if request.method == 'HEAD': 33 response.content = '' 27 # Setting the status code is enough here (same reasons as 28 # above). 29 response.status = 304 34 30 35 31 return response django/trunk/django/test/client.py
r6338 r6662 43 43 for middleware_method in self._response_middleware: 44 44 response = middleware_method(request, response) 45 45 response = self.apply_response_fixes(request, response) 46 46 finally: 47 47 dispatcher.send(signal=signals.request_finished) django/trunk/tests/regressiontests/test_client_regress/models.py
r6183 r6662 32 32 except AssertionError, e: 33 33 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)") 34 34 35 35 try: 36 36 self.assertContains(response, 'twice', 1) 37 37 except AssertionError, e: 38 38 self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)") 39 39 40 40 try: 41 41 self.assertContains(response, 'thrice') … … 47 47 except AssertionError, e: 48 48 self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)") 49 49 50 50 class AssertTemplateUsedTests(TestCase): 51 51 fixtures = ['testdata.json'] 52 52 53 53 def test_no_context(self): 54 54 "Template usage assertions work then templates aren't in use" … … 57 57 # Check that the no template case doesn't mess with the template assertions 58 58 self.assertTemplateNotUsed(response, 'GET Template') 59 59 60 60 try: 61 61 self.assertTemplateUsed(response, 'GET Template') … … 63 63 self.assertEquals(str(e), "No templates used to render the response") 64 64 65 def test_single_context(self): 65 def test_single_context(self): 66 66 "Template assertions work when there is a single context" 67 67 response = self.client.get('/test_client/post_view/', {}) 68 68 69 # 69 # 70 70 try: 71 71 self.assertTemplateNotUsed(response, 'Empty GET Template') 72 72 except AssertionError, e: 73 73 self.assertEquals(str(e), "Template 'Empty GET Template' was used unexpectedly in rendering the response") 74 75 try: 76 self.assertTemplateUsed(response, 'Empty POST Template') 74 75 try: 76 self.assertTemplateUsed(response, 'Empty POST Template') 77 77 except AssertionError, e: 78 78 self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template") 79 79 80 80 def test_multiple_context(self): 81 81 "Template assertions work when there are multiple contexts" … … 100 100 101 101 try: 102 self.assertTemplateUsed(response, "Valid POST Template") 102 self.assertTemplateUsed(response, "Valid POST Template") 103 103 except AssertionError, e: 104 104 self.assertEquals(str(e), "Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html") … … 106 106 class AssertRedirectsTests(TestCase): 107 107 def test_redirect_page(self): 108 "An assertion is raised if the original page couldn't be retrieved as expected" 108 "An assertion is raised if the original page couldn't be retrieved as expected" 109 109 # This page will redirect with code 301, not 302 110 response = self.client.get('/test_client/permanent_redirect_view/') 110 response = self.client.get('/test_client/permanent_redirect_view/') 111 111 try: 112 112 self.assertRedirects(response, '/test_client/get_view/') 113 113 except AssertionError, e: 114 114 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)") 115 115 116 116 def test_lost_query(self): 117 117 "An assertion is raised if the redirect location doesn't preserve GET parameters" … … 120 120 self.assertRedirects(response, '/test_client/get_view/') 121 121 except AssertionError, e: 122 self.assertEquals(str(e), "Response redirected to 'http://testserver/test_client/get_view/?var=value', expected ' /test_client/get_view/'")122 self.assertEquals(str(e), "Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'") 123 123 124 124 def test_incorrect_target(self): 125 125 "An assertion is raised if the response redirects to another target" 126 response = self.client.get('/test_client/permanent_redirect_view/') 126 response = self.client.get('/test_client/permanent_redirect_view/') 127 127 try: 128 128 # Should redirect to get_view … … 130 130 except AssertionError, e: 131 131 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)") 132 132 133 133 def test_target_page(self): 134 134 "An assertion is raised if the response redirect target cannot be retrieved as expected" … … 139 139 except AssertionError, e: 140 140 self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)") 141 141 142 142 class AssertFormErrorTests(TestCase): 143 143 def test_unknown_form(self): … … 158 158 except AssertionError, e: 159 159 self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response") 160 160 161 161 def test_unknown_field(self): 162 162 "An assertion is raised if the field name is unknown" … … 176 176 except AssertionError, e: 177 177 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'") 178 178 179 179 def test_noerror_field(self): 180 180 "An assertion is raised if the field doesn't have any errors" … … 194 194 except AssertionError, e: 195 195 self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors") 196 196 197 197 def test_unknown_error(self): 198 198 "An assertion is raised if the field doesn't contain the provided error" … … 212 212 except AssertionError, e: 213 213 self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])") 214 214 215 215 def test_unknown_nonfield_error(self): 216 216 """ … … 232 232 self.assertFormError(response, 'form', None, 'Some error.') 233 233 except AssertionError, e: 234 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )") 234 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )") 235 235 236 236 class FileUploadTests(TestCase): … … 257 257 # Get a redirection page with the second client. 258 258 response = c.get("/test_client_regress/login_protected_redirect_view/") 259 260 # At this points, the self.client isn't logged in. 261 # Check that assertRedirects uses the original client, not the 259 260 # At this points, the self.client isn't logged in. 261 # Check that assertRedirects uses the original client, not the 262 262 # default client. 263 263 self.assertRedirects(response, "http://testserver/test_client_regress/get_view/")
