Ticket #9199: patch2_tests_added.diff
File patch2_tests_added.diff, 4.4 KB (added by , 16 years ago) |
---|
-
django/middleware/common.py
56 56 try: 57 57 urlresolvers.resolve(request.path_info) 58 58 except urlresolvers.Resolver404: 59 new_url[1] = new_url[1] + '/' 60 if settings.DEBUG and request.method == 'POST': 61 raise RuntimeError, ("" 62 "You called this URL via POST, but the URL doesn't end " 63 "in a slash and you have APPEND_SLASH set. Django can't " 64 "redirect to the slash URL while maintaining POST data. " 65 "Change your form to point to %s%s (note the trailing " 66 "slash), or set APPEND_SLASH=False in your Django " 67 "settings.") % (new_url[0], new_url[1]) 59 # Check if slash ending path exists 60 try: 61 urlresolvers.resolve("%s/" % request.path_info) 62 except urlresolvers.Resolver404: 63 pass 64 else: 65 new_url[1] = new_url[1] + '/' 66 if settings.DEBUG and request.method == 'POST': 67 raise RuntimeError, ("You called this URL via POST, " 68 "but the URL doesn't end in a slash and you have " 69 "APPEND_SLASH set. Django can't redirect to the slash " 70 "URL while maintaining POST data. Change your form to " 71 "point to %s%s (note the trailing slash), or set " 72 "APPEND_SLASH=False in your Django " 73 "settings.") % (new_url[0], new_url[1]) 68 74 69 75 if new_url != old_url: 70 # Redirect if the target url exists 71 try: 72 urlresolvers.resolve("%s/" % request.path_info) 73 except urlresolvers.Resolver404: 74 pass 76 if new_url[0]: 77 newurl = "%s://%s%s" % ( 78 request.is_secure() and 'https' or 'http', 79 new_url[0], urlquote(new_url[1])) 75 80 else: 76 if new_url[0]: 77 newurl = "%s://%s%s" % ( 78 request.is_secure() and 'https' or 'http', 79 new_url[0], urlquote(new_url[1])) 80 else: 81 newurl = urlquote(new_url[1]) 82 if request.GET: 83 newurl += '?' + request.META['QUERY_STRING'] 84 return http.HttpResponsePermanentRedirect(newurl) 81 newurl = urlquote(new_url[1]) 82 if request.GET: 83 newurl += '?' + request.META['QUERY_STRING'] 84 return http.HttpResponsePermanentRedirect(newurl) 85 85 86 86 return None 87 87 -
tests/regressiontests/middleware/tests.py
89 89 self.assertEquals( 90 90 r['Location'], 91 91 'http://testserver/middleware/needsquoting%23/') 92 93 def test_prepend_www(self): 94 settings.PREPEND_WWW = True 95 settings.APPEND_SLASH = False 96 request = self._get_request('path/') 97 r = CommonMiddleware().process_request(request) 98 self.assertEquals(r.status_code, 301) 99 self.assertEquals( 100 r['Location'], 101 'http://www.testserver/middleware/path/') 102 103 def test_prepend_www_append_slash_have_slash(self): 104 settings.PREPEND_WWW = True 105 settings.APPEND_SLASH = True 106 request = self._get_request('slash/') 107 r = CommonMiddleware().process_request(request) 108 self.assertEquals(r.status_code, 301) 109 self.assertEquals(r['Location'], 110 'http://www.testserver/middleware/slash/') 111 112 def test_prepend_www_append_slash_slashless(self): 113 settings.PREPEND_WWW = True 114 settings.APPEND_SLASH = True 115 request = self._get_request('slash') 116 r = CommonMiddleware().process_request(request) 117 self.assertEquals(r.status_code, 301) 118 self.assertEquals(r['Location'], 119 'http://www.testserver/middleware/slash/')