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