|
17 | 17 |
|
18 | 18 | import mock
|
19 | 19 |
|
20 |
| -from .decorators import json_view |
| 20 | +from .decorators import json_view, json_request |
21 | 21 | from .exceptions import BadRequest
|
22 | 22 |
|
23 | 23 |
|
@@ -326,3 +326,101 @@ def get(self, request):
|
326 | 326 | eq_(JSON, res['content-type'])
|
327 | 327 | data = json.loads(res.content.decode('utf-8'))
|
328 | 328 | eq_('bar', data['foo'])
|
| 329 | + |
| 330 | + |
| 331 | +class JsonRequestTests(TestCase): |
| 332 | + def test_application_json(self): |
| 333 | + data = { |
| 334 | + 'foo': 'bar', |
| 335 | + 'baz': 'qux', |
| 336 | + 'quz': [{'foo': 'bar'}], |
| 337 | + } |
| 338 | + |
| 339 | + @json_request |
| 340 | + def temp(req): |
| 341 | + return req.data |
| 342 | + |
| 343 | + res = temp(rf.post( |
| 344 | + '/', |
| 345 | + data=json.dumps(data), |
| 346 | + content_type='application/json' |
| 347 | + )) |
| 348 | + eq_(res, data) |
| 349 | + |
| 350 | + def test_get_requests(self): |
| 351 | + data = { |
| 352 | + 'foo': 'bar', |
| 353 | + 'baz': '0' |
| 354 | + } |
| 355 | + |
| 356 | + @json_request(assume_json=False) |
| 357 | + def temp(req): |
| 358 | + return req.data |
| 359 | + |
| 360 | + res = temp(rf.get('/?foo=bar&baz=0')) |
| 361 | + eq_(res, data) |
| 362 | + |
| 363 | + def test_post_requests(self): |
| 364 | + data = { |
| 365 | + 'foo': 'bar', |
| 366 | + 'baz': '0' |
| 367 | + } |
| 368 | + |
| 369 | + @json_request(assume_json=False) |
| 370 | + def temp(req): |
| 371 | + return req.data |
| 372 | + |
| 373 | + # test application/x-www-form-urlencoded |
| 374 | + res = temp(rf.post( |
| 375 | + '/', |
| 376 | + data='foo=bar&baz=0', |
| 377 | + content_type='application/x-www-form-urlencoded' |
| 378 | + )) |
| 379 | + eq_(res, data) |
| 380 | + |
| 381 | + # test multipart/form-data |
| 382 | + res = temp(rf.post('/', data=data, files=None)) |
| 383 | + eq_(res, data) |
| 384 | + |
| 385 | + def test_assume_json(self): |
| 386 | + data = { |
| 387 | + 'foo': 'bar', |
| 388 | + 'baz': '0' |
| 389 | + } |
| 390 | + |
| 391 | + @json_request(assume_json=True) |
| 392 | + def temp(req): |
| 393 | + return req.data |
| 394 | + |
| 395 | + @json_request(assume_json=False) |
| 396 | + def temp_2(req): |
| 397 | + return req.data |
| 398 | + |
| 399 | + # test get request |
| 400 | + res = temp(rf.get('/?foo=bar&baz=0')) |
| 401 | + eq_(res, {}) |
| 402 | + |
| 403 | + res = temp_2(rf.get('/?foo=bar&baz=0')) |
| 404 | + eq_(res, data) |
| 405 | + |
| 406 | + # test application/x-www-form-urlencoded |
| 407 | + res = temp(rf.post( |
| 408 | + '/', |
| 409 | + data='foo=bar&baz=0', |
| 410 | + content_type='application/x-www-form-urlencoded' |
| 411 | + )) |
| 412 | + eq_(res, {}) |
| 413 | + |
| 414 | + res = temp_2(rf.post( |
| 415 | + '/', |
| 416 | + data='foo=bar&baz=0', |
| 417 | + content_type='application/x-www-form-urlencoded' |
| 418 | + )) |
| 419 | + eq_(res, data) |
| 420 | + |
| 421 | + # test multipart/form-data |
| 422 | + res = temp(rf.post('/', data=data, files=None)) |
| 423 | + eq_(res, {}) |
| 424 | + |
| 425 | + res = temp_2(rf.post('/', data=data, files=None)) |
| 426 | + eq_(res, data) |
0 commit comments