| 128 | |
| 129 | |
| 130 | #The following tests examine expected behavior given a custom urlconf that |
| 131 | #overrides the default one through the request object. |
| 132 | |
| 133 | def test_append_slash_have_slash_custom_urlconf(self): |
| 134 | """ |
| 135 | Tests that URLs with slashes go unmolested. |
| 136 | """ |
| 137 | settings.APPEND_SLASH = True |
| 138 | request = self._get_request('customurlconf/slash/') |
| 139 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 140 | self.assertEquals(CommonMiddleware().process_request(request), None) |
| 141 | |
| 142 | def test_append_slash_slashless_resource_custom_urlconf(self): |
| 143 | """ |
| 144 | Tests that matches to explicit slashless URLs go unmolested. |
| 145 | """ |
| 146 | settings.APPEND_SLASH = True |
| 147 | request = self._get_request('customurlconf/noslash') |
| 148 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 149 | self.assertEquals(CommonMiddleware().process_request(request), None) |
| 150 | |
| 151 | def test_append_slash_slashless_unknown_custom_urlconf(self): |
| 152 | """ |
| 153 | Tests that APPEND_SLASH doesn't redirect to unknown resources. |
| 154 | """ |
| 155 | settings.APPEND_SLASH = True |
| 156 | request = self._get_request('customurlconf/unknown') |
| 157 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 158 | self.assertEquals(CommonMiddleware().process_request(request), None) |
| 159 | |
| 160 | def test_append_slash_redirect_custom_urlconf(self): |
| 161 | """ |
| 162 | Tests that APPEND_SLASH redirects slashless URLs to a valid pattern. |
| 163 | """ |
| 164 | settings.APPEND_SLASH = True |
| 165 | request = self._get_request('customurlconf/slash') |
| 166 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 167 | r = CommonMiddleware().process_request(request) |
| 168 | self.assertEquals(r.status_code, 301) |
| 169 | self.assertEquals(r['Location'], 'http://testserver/middleware/customurlconf/slash/') |
| 170 | |
| 171 | def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self): |
| 172 | """ |
| 173 | Tests that while in debug mode, an exception is raised with a warning |
| 174 | when a failed attempt is made to POST to an URL which would normally be |
| 175 | redirected to a slashed version. |
| 176 | """ |
| 177 | settings.APPEND_SLASH = True |
| 178 | settings.DEBUG = True |
| 179 | request = self._get_request('customurlconf/slash') |
| 180 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 181 | request.method = 'POST' |
| 182 | self.assertRaises( |
| 183 | RuntimeError, |
| 184 | CommonMiddleware().process_request, |
| 185 | request) |
| 186 | try: |
| 187 | CommonMiddleware().process_request(request) |
| 188 | except RuntimeError, e: |
| 189 | self.failUnless('end in a slash' in str(e)) |
| 190 | settings.DEBUG = False |
| 191 | |
| 192 | def test_append_slash_disabled_custom_urlconf(self): |
| 193 | """ |
| 194 | Tests disabling append slash functionality. |
| 195 | """ |
| 196 | settings.APPEND_SLASH = False |
| 197 | request = self._get_request('customurlconf/slash') |
| 198 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 199 | self.assertEquals(CommonMiddleware().process_request(request), None) |
| 200 | |
| 201 | def test_append_slash_quoted_custom_urlconf(self): |
| 202 | """ |
| 203 | Tests that URLs which require quoting are redirected to their slash |
| 204 | version ok. |
| 205 | """ |
| 206 | settings.APPEND_SLASH = True |
| 207 | request = self._get_request('customurlconf/needsquoting#') |
| 208 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 209 | r = CommonMiddleware().process_request(request) |
| 210 | self.assertEquals(r.status_code, 301) |
| 211 | self.assertEquals( |
| 212 | r['Location'], |
| 213 | 'http://testserver/middleware/customurlconf/needsquoting%23/') |
| 214 | |
| 215 | def test_prepend_www_custom_urlconf(self): |
| 216 | settings.PREPEND_WWW = True |
| 217 | settings.APPEND_SLASH = False |
| 218 | request = self._get_request('customurlconf/path/') |
| 219 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 220 | r = CommonMiddleware().process_request(request) |
| 221 | self.assertEquals(r.status_code, 301) |
| 222 | self.assertEquals( |
| 223 | r['Location'], |
| 224 | 'http://www.testserver/middleware/customurlconf/path/') |
| 225 | |
| 226 | def test_prepend_www_append_slash_have_slash_custom_urlconf(self): |
| 227 | settings.PREPEND_WWW = True |
| 228 | settings.APPEND_SLASH = True |
| 229 | request = self._get_request('customurlconf/slash/') |
| 230 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 231 | r = CommonMiddleware().process_request(request) |
| 232 | self.assertEquals(r.status_code, 301) |
| 233 | self.assertEquals(r['Location'], |
| 234 | 'http://www.testserver/middleware/customurlconf/slash/') |
| 235 | |
| 236 | def test_prepend_www_append_slash_slashless_custom_urlconf(self): |
| 237 | settings.PREPEND_WWW = True |
| 238 | settings.APPEND_SLASH = True |
| 239 | request = self._get_request('customurlconf/slash') |
| 240 | request.urlconf = 'regressiontests.middleware.extra_urls' |
| 241 | r = CommonMiddleware().process_request(request) |
| 242 | self.assertEquals(r.status_code, 301) |
| 243 | self.assertEquals(r['Location'], |
| 244 | 'http://www.testserver/middleware/customurlconf/slash/') |