|
| 1 | +# -*- coding: utf-8 -*- |
1 | 2 | from __future__ import unicode_literals
|
2 | 3 |
|
3 | 4 | from django.conf.urls import url
|
|
11 | 12 | from rest_framework.serializers import ModelSerializer
|
12 | 13 | from rest_framework.utils import json
|
13 | 14 | from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
| 15 | +from rest_framework.utils.urls import remove_query_param, replace_query_param |
14 | 16 | from rest_framework.views import APIView
|
15 | 17 | from rest_framework.viewsets import ModelViewSet
|
16 | 18 | from tests.models import BasicModel
|
@@ -205,3 +207,66 @@ class NonStrictJsonFloatTests(JsonFloatTests):
|
205 | 207 | """
|
206 | 208 | 'STRICT_JSON = False' should not somehow affect internal json behavior
|
207 | 209 | """
|
| 210 | + |
| 211 | + |
| 212 | +class UrlsReplaceQueryParamTests(TestCase): |
| 213 | + """ |
| 214 | + Tests the replace_query_param functionality. |
| 215 | + """ |
| 216 | + def test_valid_unicode_preserved(self): |
| 217 | + # Encoded string: '查询' |
| 218 | + q = '/?q=%E6%9F%A5%E8%AF%A2' |
| 219 | + new_key = 'page' |
| 220 | + new_value = 2 |
| 221 | + value = '%E6%9F%A5%E8%AF%A2' |
| 222 | + |
| 223 | + assert new_key in replace_query_param(q, new_key, new_value) |
| 224 | + assert value in replace_query_param(q, new_key, new_value) |
| 225 | + |
| 226 | + def test_valid_unicode_replaced(self): |
| 227 | + q = '/?page=1' |
| 228 | + value = '1' |
| 229 | + new_key = 'q' |
| 230 | + new_value = '%E6%9F%A5%E8%AF%A2' |
| 231 | + |
| 232 | + assert new_key in replace_query_param(q, new_key, new_value) |
| 233 | + assert value in replace_query_param(q, new_key, new_value) |
| 234 | + |
| 235 | + def test_invalid_unicode(self): |
| 236 | + # Encoded string: '��<script>alert(313)</script>=1' |
| 237 | + q = '/e/?%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1' |
| 238 | + key = 'from' |
| 239 | + value = 'login' |
| 240 | + |
| 241 | + assert key in replace_query_param(q, key, value) |
| 242 | + |
| 243 | + |
| 244 | +class UrlsRemoveQueryParamTests(TestCase): |
| 245 | + """ |
| 246 | + Tests the remove_query_param functionality. |
| 247 | + """ |
| 248 | + def test_valid_unicode_preserved(self): |
| 249 | + q = '/?q=%E6%9F%A5%E8%AF%A2' |
| 250 | + new_key = 'page' |
| 251 | + new_value = 2 |
| 252 | + value = '%E6%9F%A5%E8%AF%A2' |
| 253 | + |
| 254 | + assert new_key in replace_query_param(q, new_key, new_value) |
| 255 | + assert value in replace_query_param(q, new_key, new_value) |
| 256 | + |
| 257 | + def test_valid_unicode_removed(self): |
| 258 | + q = '/?page=2345&q=%E6%9F%A5%E8%AF%A2' |
| 259 | + key = 'page' |
| 260 | + value = '2345' |
| 261 | + removed_key = 'q' |
| 262 | + |
| 263 | + assert key in remove_query_param(q, removed_key) |
| 264 | + assert value in remove_query_param(q, removed_key) |
| 265 | + assert '%' not in remove_query_param(q, removed_key) |
| 266 | + |
| 267 | + def test_invalid_unicode(self): |
| 268 | + q = '/?from=login&page=2&%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1' |
| 269 | + key = 'from' |
| 270 | + removed_key = 'page' |
| 271 | + |
| 272 | + assert key in remove_query_param(q, removed_key) |
0 commit comments