|
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 | from .views import JsonView
|
23 | 23 |
|
@@ -396,3 +396,101 @@ def get(self, request):
|
396 | 396 | eq_(JSON, res['content-type'])
|
397 | 397 | data = json.loads(res.content.decode('utf-8'))
|
398 | 398 | eq_('bar', data['foo'])
|
| 399 | + |
| 400 | + |
| 401 | +class JsonRequestTests(TestCase): |
| 402 | + def test_application_json(self): |
| 403 | + data = { |
| 404 | + 'foo': 'bar', |
| 405 | + 'baz': 'qux', |
| 406 | + 'quz': [{'foo': 'bar'}], |
| 407 | + } |
| 408 | + |
| 409 | + @json_request |
| 410 | + def temp(req): |
| 411 | + return req.data |
| 412 | + |
| 413 | + res = temp(rf.post( |
| 414 | + '/', |
| 415 | + data=json.dumps(data), |
| 416 | + content_type='application/json' |
| 417 | + )) |
| 418 | + eq_(res, data) |
| 419 | + |
| 420 | + def test_get_requests(self): |
| 421 | + data = { |
| 422 | + 'foo': 'bar', |
| 423 | + 'baz': '0' |
| 424 | + } |
| 425 | + |
| 426 | + @json_request(assume_json=False) |
| 427 | + def temp(req): |
| 428 | + return req.data |
| 429 | + |
| 430 | + res = temp(rf.get('/?foo=bar&baz=0')) |
| 431 | + eq_(res, data) |
| 432 | + |
| 433 | + def test_post_requests(self): |
| 434 | + data = { |
| 435 | + 'foo': 'bar', |
| 436 | + 'baz': '0' |
| 437 | + } |
| 438 | + |
| 439 | + @json_request(assume_json=False) |
| 440 | + def temp(req): |
| 441 | + return req.data |
| 442 | + |
| 443 | + # test application/x-www-form-urlencoded |
| 444 | + res = temp(rf.post( |
| 445 | + '/', |
| 446 | + data='foo=bar&baz=0', |
| 447 | + content_type='application/x-www-form-urlencoded' |
| 448 | + )) |
| 449 | + eq_(res, data) |
| 450 | + |
| 451 | + # test multipart/form-data |
| 452 | + res = temp(rf.post('/', data=data, files=None)) |
| 453 | + eq_(res, data) |
| 454 | + |
| 455 | + def test_assume_json(self): |
| 456 | + data = { |
| 457 | + 'foo': 'bar', |
| 458 | + 'baz': '0' |
| 459 | + } |
| 460 | + |
| 461 | + @json_request(assume_json=True) |
| 462 | + def temp(req): |
| 463 | + return req.data |
| 464 | + |
| 465 | + @json_request(assume_json=False) |
| 466 | + def temp_2(req): |
| 467 | + return req.data |
| 468 | + |
| 469 | + # test get request |
| 470 | + res = temp(rf.get('/?foo=bar&baz=0')) |
| 471 | + eq_(res, {}) |
| 472 | + |
| 473 | + res = temp_2(rf.get('/?foo=bar&baz=0')) |
| 474 | + eq_(res, data) |
| 475 | + |
| 476 | + # test application/x-www-form-urlencoded |
| 477 | + res = temp(rf.post( |
| 478 | + '/', |
| 479 | + data='foo=bar&baz=0', |
| 480 | + content_type='application/x-www-form-urlencoded' |
| 481 | + )) |
| 482 | + eq_(res, {}) |
| 483 | + |
| 484 | + res = temp_2(rf.post( |
| 485 | + '/', |
| 486 | + data='foo=bar&baz=0', |
| 487 | + content_type='application/x-www-form-urlencoded' |
| 488 | + )) |
| 489 | + eq_(res, data) |
| 490 | + |
| 491 | + # test multipart/form-data |
| 492 | + res = temp(rf.post('/', data=data, files=None)) |
| 493 | + eq_(res, {}) |
| 494 | + |
| 495 | + res = temp_2(rf.post('/', data=data, files=None)) |
| 496 | + eq_(res, data) |
0 commit comments