Debian Codegraph @

pecan/tests/test_rest.py

raw
1 # -*- coding: utf-8 -*-
2
3 from json import dumps, loads
4 import unittest
5 import struct
6 import sys
7 import warnings
8
9 from webtest import TestApp
10
11 from pecan import abort, expose, make_app, response, redirect
12 from pecan.rest import RestController
13 from pecan.tests import PecanTestCase
14
15
16 class TestRestController(PecanTestCase):
17
18 def test_basic_rest(self):
19
20 class OthersController(object):
21
22 @expose()
23 def index(self):
24 return 'OTHERS'
25
26 @expose()
27 def echo(self, value):
28 return str(value)
29
30 class ThingsController(RestController):
31 data = ['zero', 'one', 'two', 'three']
32
33 _custom_actions = {'count': ['GET'], 'length': ['GET', 'POST']}
34
35 others = OthersController()
36
37 @expose()
38 def get_one(self, id):
39 return self.data[int(id)]
40
41 @expose('json')
42 def get_all(self):
43 return dict(items=self.data)
44
45 @expose()
46 def length(self, id, value=None):
47 length = len(self.data[int(id)])
48 if value:
49 length += len(value)
50 return str(length)
51
52 @expose()
53 def get_count(self):
54 return str(len(self.data))
55
56 @expose()
57 def new(self):
58 return 'NEW'
59
60 @expose()
61 def post(self, value):
62 self.data.append(value)
63 response.status = 302
64 return 'CREATED'
65
66 @expose()
67 def edit(self, id):
68 return 'EDIT %s' % self.data[int(id)]
69
70 @expose()
71 def put(self, id, value):
72 self.data[int(id)] = value
73 return 'UPDATED'
74
75 @expose()
76 def get_delete(self, id):
77 return 'DELETE %s' % self.data[int(id)]
78
79 @expose()
80 def delete(self, id):
81 del self.data[int(id)]
82 return 'DELETED'
83
84 @expose()
85 def trace(self):
86 return 'TRACE'
87
88 @expose()
89 def post_options(self):
90 return 'OPTIONS'
91
92 @expose()
93 def options(self):
94 abort(500)
95
96 @expose()
97 def other(self):
98 abort(500)
99
100 class RootController(object):
101 things = ThingsController()
102
103 # create the app
104 app = TestApp(make_app(RootController()))
105
106 # test get_all
107 r = app.get('/things')
108 assert r.status_int == 200
109 assert r.body == dumps(
110 dict(items=ThingsController.data)
111 ).encode('utf-8')
112
113 # test get_one
114 for i, value in enumerate(ThingsController.data):
115 r = app.get('/things/%d' % i)
116 assert r.status_int == 200
117 assert r.body == value.encode('utf-8')
118
119 # test post
120 r = app.post('/things', {'value': 'four'})
121 assert r.status_int == 302
122 assert r.body == b'CREATED'
123
124 # make sure it works
125 r = app.get('/things/4')
126 assert r.status_int == 200
127 assert r.body == b'four'
128
129 # test edit
130 r = app.get('/things/3/edit')
131 assert r.status_int == 200
132 assert r.body == b'EDIT three'
133
134 # test put
135 r = app.put('/things/4', {'value': 'FOUR'})
136 assert r.status_int == 200
137 assert r.body == b'UPDATED'
138
139 # make sure it works
140 r = app.get('/things/4')
141 assert r.status_int == 200
142 assert r.body == b'FOUR'
143
144 # test put with _method parameter and GET
145 r = app.get('/things/4?_method=put', {'value': 'FOUR!'}, status=405)
146 assert r.status_int == 405
147
148 # make sure it works
149 r = app.get('/things/4')
150 assert r.status_int == 200
151 assert r.body == b'FOUR'
152
153 # test put with _method parameter and POST
154 r = app.post('/things/4?_method=put', {'value': 'FOUR!'})
155 assert r.status_int == 200
156 assert r.body == b'UPDATED'
157
158 # make sure it works
159 r = app.get('/things/4')
160 assert r.status_int == 200
161 assert r.body == b'FOUR!'
162
163 # test get delete
164 r = app.get('/things/4/delete')
165 assert r.status_int == 200
166 assert r.body == b'DELETE FOUR!'
167
168 # test delete
169 r = app.delete('/things/4')
170 assert r.status_int == 200
171 assert r.body == b'DELETED'
172
173 # make sure it works
174 r = app.get('/things')
175 assert r.status_int == 200
176 assert len(loads(r.body.decode())['items']) == 4
177
178 # test delete with _method parameter and GET
179 r = app.get('/things/3?_method=DELETE', status=405)
180 assert r.status_int == 405
181
182 # make sure it works
183 r = app.get('/things')
184 assert r.status_int == 200
185 assert len(loads(r.body.decode())['items']) == 4
186
187 # test delete with _method parameter and POST
188 r = app.post('/things/3?_method=DELETE')
189 assert r.status_int == 200
190 assert r.body == b'DELETED'
191
192 # make sure it works
193 r = app.get('/things')
194 assert r.status_int == 200
195 assert len(loads(r.body.decode())['items']) == 3
196
197 # test "TRACE" custom action
198 r = app.request('/things', method='TRACE')
199 assert r.status_int == 200
200 assert r.body == b'TRACE'
201
202 # test "TRACE" custom action with _method parameter
203 r = app.get('/things?_method=TRACE')
204 assert r.status_int == 200
205 assert r.body == b'TRACE'
206
207 # test the "OPTIONS" custom action
208 r = app.request('/things', method='OPTIONS')
209 assert r.status_int == 200
210 assert r.body == b'OPTIONS'
211
212 # test the "OPTIONS" custom action with the _method parameter
213 r = app.post('/things', {'_method': 'OPTIONS'})
214 assert r.status_int == 200
215 assert r.body == b'OPTIONS'
216
217 # test the "other" custom action
218 with warnings.catch_warnings():
219 warnings.simplefilter("ignore")
220 r = app.request('/things/other', method='MISC', status=405)
221 assert r.status_int == 405
222
223 # test the "other" custom action with the _method parameter
224 r = app.post('/things/other', {'_method': 'MISC'}, status=405)
225 assert r.status_int == 405
226
227 # test the "others" custom action
228 with warnings.catch_warnings():
229 warnings.simplefilter("ignore")
230 r = app.request('/things/others/', method='MISC')
231 assert r.status_int == 200
232 assert r.body == b'OTHERS'
233
234 # test the "others" custom action missing trailing slash
235 with warnings.catch_warnings():
236 warnings.simplefilter("ignore")
237 r = app.request('/things/others', method='MISC', status=302)
238 assert r.status_int == 302
239
240 # test the "others" custom action with the _method parameter
241 r = app.get('/things/others/?_method=MISC')
242 assert r.status_int == 200
243 assert r.body == b'OTHERS'
244
245 # test an invalid custom action
246 r = app.get('/things?_method=BAD', status=405)
247 assert r.status_int == 405
248
249 # test custom "GET" request "count"
250 r = app.get('/things/count')
251 assert r.status_int == 200
252 assert r.body == b'3'
253
254 # test custom "GET" request "length"
255 r = app.get('/things/1/length')
256 assert r.status_int == 200
257 assert r.body == b'3'
258
259 # test custom "GET" request through subcontroller
260 r = app.get('/things/others/echo?value=test')
261 assert r.status_int == 200
262 assert r.body == b'test'
263
264 # test custom "POST" request "length"
265 r = app.post('/things/1/length', {'value': 'test'})
266 assert r.status_int == 200
267 assert r.body == b'7'
268
269 # test custom "POST" request through subcontroller
270 r = app.post('/things/others/echo', {'value': 'test'})
271 assert r.status_int == 200
272 assert r.body == b'test'
273
274 def test_getall_with_trailing_slash(self):
275
276 class ThingsController(RestController):
277
278 data = ['zero', 'one', 'two', 'three']
279
280 @expose('json')
281 def get_all(self):
282 return dict(items=self.data)
283
284 class RootController(object):
285 things = ThingsController()
286
287 # create the app
288 app = TestApp(make_app(RootController()))
289
290 # test get_all
291 r = app.get('/things/')
292 assert r.status_int == 200
293 assert r.body == dumps(
294 dict(items=ThingsController.data)
295 ).encode('utf-8')
296
297 def test_405_with_lookup(self):
298
299 class LookupController(RestController):
300
301 def __init__(self, _id):
302 self._id = _id
303
304 @expose()
305 def get_all(self):
306 return 'ID: %s' % self._id
307
308 class ThingsController(RestController):
309
310 @expose()
311 def _lookup(self, _id, *remainder):
312 return LookupController(_id), remainder
313
314 class RootController(object):
315 things = ThingsController()
316
317 # create the app
318 app = TestApp(make_app(RootController()))
319
320 # these should 405
321 for path in ('/things', '/things/'):
322 r = app.get(path, expect_errors=True)
323 assert r.status_int == 405
324
325 r = app.get('/things/foo')
326 assert r.status_int == 200
327 assert r.body == b'ID: foo'
328
329 def test_getall_with_lookup(self):
330
331 class LookupController(RestController):
332
333 def __init__(self, _id):
334 self._id = _id
335
336 @expose()
337 def get_all(self):
338 return 'ID: %s' % self._id
339
340 class ThingsController(RestController):
341
342 data = ['zero', 'one', 'two', 'three']
343
344 @expose()
345 def _lookup(self, _id, *remainder):
346 return LookupController(_id), remainder
347
348 @expose('json')
349 def get_all(self):
350 return dict(items=self.data)
351
352 class RootController(object):
353 things = ThingsController()
354
355 # create the app
356 app = TestApp(make_app(RootController()))
357
358 # test get_all
359 for path in ('/things', '/things/'):
360 r = app.get(path)
361 assert r.status_int == 200
362 assert r.body == dumps(
363 dict(items=ThingsController.data)
364 ).encode('utf-8')
365
366 r = app.get('/things/foo')
367 assert r.status_int == 200
368 assert r.body == b'ID: foo'
369
370 def test_simple_nested_rest(self):
371
372 class BarController(RestController):
373
374 @expose()
375 def post(self):
376 return "BAR-POST"
377
378 @expose()
379 def delete(self, id_):
380 return "BAR-%s" % id_
381
382 class FooController(RestController):
383
384 bar = BarController()
385
386 @expose()
387 def post(self):
388 return "FOO-POST"
389
390 @expose()
391 def delete(self, id_):
392 return "FOO-%s" % id_
393
394 class RootController(object):
395 foo = FooController()
396
397 # create the app
398 app = TestApp(make_app(RootController()))
399
400 r = app.post('/foo')
401 assert r.status_int == 200
402 assert r.body == b"FOO-POST"
403
404 r = app.delete('/foo/1')
405 assert r.status_int == 200
406 assert r.body == b"FOO-1"
407
408 r = app.post('/foo/bar')
409 assert r.status_int == 200
410 assert r.body == b"BAR-POST"
411
412 r = app.delete('/foo/bar/2')
413 assert r.status_int == 200
414 assert r.body == b"BAR-2"
415
416 def test_complicated_nested_rest(self):
417
418 class BarsController(RestController):
419
420 data = [['zero-zero', 'zero-one'], ['one-zero', 'one-one']]
421
422 @expose()
423 def get_one(self, foo_id, id):
424 return self.data[int(foo_id)][int(id)]
425
426 @expose('json')
427 def get_all(self, foo_id):
428 return dict(items=self.data[int(foo_id)])
429
430 @expose()
431 def new(self, foo_id):
432 return 'NEW FOR %s' % foo_id
433
434 @expose()
435 def post(self, foo_id, value):
436 foo_id = int(foo_id)
437 if len(self.data) < foo_id + 1:
438 self.data.extend([[]] * (foo_id - len(self.data) + 1))
439 self.data[foo_id].append(value)
440 response.status = 302
441 return 'CREATED FOR %s' % foo_id
442
443 @expose()
444 def edit(self, foo_id, id):
445 return 'EDIT %s' % self.data[int(foo_id)][int(id)]
446
447 @expose()
448 def put(self, foo_id, id, value):
449 self.data[int(foo_id)][int(id)] = value
450 return 'UPDATED'
451
452 @expose()
453 def get_delete(self, foo_id, id):
454 return 'DELETE %s' % self.data[int(foo_id)][int(id)]
455
456 @expose()
457 def delete(self, foo_id, id):
458 del self.data[int(foo_id)][int(id)]
459 return 'DELETED'
460
461 class FoosController(RestController):
462
463 data = ['zero', 'one']
464
465 bars = BarsController()
466
467 @expose()
468 def get_one(self, id):
469 return self.data[int(id)]
470
471 @expose('json')
472 def get_all(self):
473 return dict(items=self.data)
474
475 @expose()
476 def new(self):
477 return 'NEW'
478
479 @expose()
480 def edit(self, id):
481 return 'EDIT %s' % self.data[int(id)]
482
483 @expose()
484 def post(self, value):
485 self.data.append(value)
486 response.status = 302
487 return 'CREATED'
488
489 @expose()
490 def put(self, id, value):
491 self.data[int(id)] = value
492 return 'UPDATED'
493
494 @expose()
495 def get_delete(self, id):
496 return 'DELETE %s' % self.data[int(id)]
497
498 @expose()
499 def delete(self, id):
500 del self.data[int(id)]
501 return 'DELETED'
502
503 class RootController(object):
504 foos = FoosController()
505
506 # create the app
507 app = TestApp(make_app(RootController()))
508
509 # test get_all
510 r = app.get('/foos')
511 assert r.status_int == 200
512 assert r.body == dumps(
513 dict(items=FoosController.data)
514 ).encode('utf-8')
515
516 # test nested get_all
517 r = app.get('/foos/1/bars')
518 assert r.status_int == 200
519 assert r.body == dumps(
520 dict(items=BarsController.data[1])
521 ).encode('utf-8')
522
523 # test get_one
524 for i, value in enumerate(FoosController.data):
525 r = app.get('/foos/%d' % i)
526 assert r.status_int == 200
527 assert r.body == value.encode('utf-8')
528
529 # test nested get_one
530 for i, value in enumerate(FoosController.data):
531 for j, value in enumerate(BarsController.data[i]):
532 r = app.get('/foos/%s/bars/%s' % (i, j))
533 assert r.status_int == 200
534 assert r.body == value.encode('utf-8')
535
536 # test post
537 r = app.post('/foos', {'value': 'two'})
538 assert r.status_int == 302
539 assert r.body == b'CREATED'
540
541 # make sure it works
542 r = app.get('/foos/2')
543 assert r.status_int == 200
544 assert r.body == b'two'
545
546 # test nested post
547 r = app.post('/foos/2/bars', {'value': 'two-zero'})
548 assert r.status_int == 302
549 assert r.body == b'CREATED FOR 2'
550
551 # make sure it works
552 r = app.get('/foos/2/bars/0')
553 assert r.status_int == 200
554 assert r.body == b'two-zero'
555
556 # test edit
557 r = app.get('/foos/1/edit')
558 assert r.status_int == 200
559 assert r.body == b'EDIT one'
560
561 # test nested edit
562 r = app.get('/foos/1/bars/1/edit')
563 assert r.status_int == 200
564 assert r.body == b'EDIT one-one'
565
566 # test put
567 r = app.put('/foos/2', {'value': 'TWO'})
568 assert r.status_int == 200
569 assert r.body == b'UPDATED'
570
571 # make sure it works
572 r = app.get('/foos/2')
573 assert r.status_int == 200
574 assert r.body == b'TWO'
575
576 # test nested put
577 r = app.put('/foos/2/bars/0', {'value': 'TWO-ZERO'})
578 assert r.status_int == 200
579 assert r.body == b'UPDATED'
580
581 # make sure it works
582 r = app.get('/foos/2/bars/0')
583 assert r.status_int == 200
584 assert r.body == b'TWO-ZERO'
585
586 # test put with _method parameter and GET
587 r = app.get('/foos/2?_method=put', {'value': 'TWO!'}, status=405)
588 assert r.status_int == 405
589
590 # make sure it works
591 r = app.get('/foos/2')
592 assert r.status_int == 200
593 assert r.body == b'TWO'
594
595 # test nested put with _method parameter and GET
596 r = app.get(
597 '/foos/2/bars/0?_method=put',
598 {'value': 'ZERO-TWO!'}, status=405
599 )
600 assert r.status_int == 405
601
602 # make sure it works
603 r = app.get('/foos/2/bars/0')
604 assert r.status_int == 200
605 assert r.body == b'TWO-ZERO'
606
607 # test put with _method parameter and POST
608 r = app.post('/foos/2?_method=put', {'value': 'TWO!'})
609 assert r.status_int == 200
610 assert r.body == b'UPDATED'
611
612 # make sure it works
613 r = app.get('/foos/2')
614 assert r.status_int == 200
615 assert r.body == b'TWO!'
616
617 # test nested put with _method parameter and POST
618 r = app.post('/foos/2/bars/0?_method=put', {'value': 'TWO-ZERO!'})
619 assert r.status_int == 200
620 assert r.body == b'UPDATED'
621
622 # make sure it works
623 r = app.get('/foos/2/bars/0')
624 assert r.status_int == 200
625 assert r.body == b'TWO-ZERO!'
626
627 # test get delete
628 r = app.get('/foos/2/delete')
629 assert r.status_int == 200
630 assert r.body == b'DELETE TWO!'
631
632 # test nested get delete
633 r = app.get('/foos/2/bars/0/delete')
634 assert r.status_int == 200
635 assert r.body == b'DELETE TWO-ZERO!'
636
637 # test nested delete
638 r = app.delete('/foos/2/bars/0')
639 assert r.status_int == 200
640 assert r.body == b'DELETED'
641
642 # make sure it works
643 r = app.get('/foos/2/bars')
644 assert r.status_int == 200
645 assert len(loads(r.body.decode())['items']) == 0
646
647 # test delete
648 r = app.delete('/foos/2')
649 assert r.status_int == 200
650 assert r.body == b'DELETED'
651
652 # make sure it works
653 r = app.get('/foos')
654 assert r.status_int == 200
655 assert len(loads(r.body.decode())['items']) == 2
656
657 # test nested delete with _method parameter and GET
658 r = app.get('/foos/1/bars/1?_method=DELETE', status=405)
659 assert r.status_int == 405
660
661 # make sure it works
662 r = app.get('/foos/1/bars')
663 assert r.status_int == 200
664 assert len(loads(r.body.decode())['items']) == 2
665
666 # test delete with _method parameter and GET
667 r = app.get('/foos/1?_method=DELETE', status=405)
668 assert r.status_int == 405
669
670 # make sure it works
671 r = app.get('/foos')
672 assert r.status_int == 200
673 assert len(loads(r.body.decode())['items']) == 2
674
675 # test nested delete with _method parameter and POST
676 r = app.post('/foos/1/bars/1?_method=DELETE')
677 assert r.status_int == 200
678 assert r.body == b'DELETED'
679
680 # make sure it works
681 r = app.get('/foos/1/bars')
682 assert r.status_int == 200
683 assert len(loads(r.body.decode())['items']) == 1
684
685 # test delete with _method parameter and POST
686 r = app.post('/foos/1?_method=DELETE')
687 assert r.status_int == 200
688 assert r.body == b'DELETED'
689
690 # make sure it works
691 r = app.get('/foos')
692 assert r.status_int == 200
693 assert len(loads(r.body.decode())['items']) == 1
694
695 def test_nested_get_all(self):
696
697 class BarsController(RestController):
698
699 @expose()
700 def get_one(self, foo_id, id):
701 return '4'
702
703 @expose()
704 def get_all(self, foo_id):
705 return '3'
706
707 class FoosController(RestController):
708
709 bars = BarsController()
710
711 @expose()
712 def get_one(self, id):
713 return '2'
714
715 @expose()
716 def get_all(self):
717 return '1'
718
719 class RootController(object):
720 foos = FoosController()
721
722 # create the app
723 app = TestApp(make_app(RootController()))
724
725 r = app.get('/foos/')
726 assert r.status_int == 200
727 assert r.body == b'1'
728
729 r = app.get('/foos/1/')
730 assert r.status_int == 200
731 assert r.body == b'2'
732
733 r = app.get('/foos/1/bars/')
734 assert r.status_int == 200
735 assert r.body == b'3'
736
737 r = app.get('/foos/1/bars/2/')
738 assert r.status_int == 200
739 assert r.body == b'4'
740
741 r = app.get('/foos/bars/', status=404)
742 assert r.status_int == 404
743
744 r = app.get('/foos/bars/1', status=404)
745 assert r.status_int == 404
746
747 def test_nested_get_all_with_lookup(self):
748
749 class BarsController(RestController):
750
751 @expose()
752 def get_one(self, foo_id, id):
753 return '4'
754
755 @expose()
756 def get_all(self, foo_id):
757 return '3'
758
759 @expose('json')
760 def _lookup(self, id, *remainder):
761 redirect('/lookup-hit/')
762
763 class FoosController(RestController):
764
765 bars = BarsController()
766
767 @expose()
768 def get_one(self, id):
769 return '2'
770
771 @expose()
772 def get_all(self):
773 return '1'
774
775 class RootController(object):
776 foos = FoosController()
777
778 # create the app
779 app = TestApp(make_app(RootController()))
780
781 r = app.get('/foos/')
782 assert r.status_int == 200
783 assert r.body == b'1'
784
785 r = app.get('/foos/1/')
786 assert r.status_int == 200
787 assert r.body == b'2'
788
789 r = app.get('/foos/1/bars/')
790 assert r.status_int == 200
791 assert r.body == b'3'
792
793 r = app.get('/foos/1/bars/2/')
794 assert r.status_int == 200
795 assert r.body == b'4'
796
797 r = app.get('/foos/bars/')
798 assert r.status_int == 302
799 assert r.headers['Location'].endswith('/lookup-hit/')
800
801 r = app.get('/foos/bars/1')
802 assert r.status_int == 302
803 assert r.headers['Location'].endswith('/lookup-hit/')
804
805 def test_bad_rest(self):
806
807 class ThingsController(RestController):
808 pass
809
810 class RootController(object):
811 things = ThingsController()
812
813 # create the app
814 app = TestApp(make_app(RootController()))
815
816 # test get_all
817 r = app.get('/things', status=405)
818 assert r.status_int == 405
819
820 # test get_one
821 r = app.get('/things/1', status=405)
822 assert r.status_int == 405
823
824 # test post
825 r = app.post('/things', {'value': 'one'}, status=405)
826 assert r.status_int == 405
827
828 # test edit
829 r = app.get('/things/1/edit', status=405)
830 assert r.status_int == 405
831
832 # test put
833 r = app.put('/things/1', {'value': 'ONE'}, status=405)
834
835 # test put with _method parameter and GET
836 r = app.get('/things/1?_method=put', {'value': 'ONE!'}, status=405)
837 assert r.status_int == 405
838
839 # test put with _method parameter and POST
840 r = app.post('/things/1?_method=put', {'value': 'ONE!'}, status=405)
841 assert r.status_int == 405
842
843 # test get delete
844 r = app.get('/things/1/delete', status=405)
845 assert r.status_int == 405
846
847 # test delete
848 r = app.delete('/things/1', status=405)
849 assert r.status_int == 405
850
851 # test delete with _method parameter and GET
852 r = app.get('/things/1?_method=DELETE', status=405)
853 assert r.status_int == 405
854
855 # test delete with _method parameter and POST
856 r = app.post('/things/1?_method=DELETE', status=405)
857 assert r.status_int == 405
858
859 # test "TRACE" custom action
860 with warnings.catch_warnings():
861 warnings.simplefilter("ignore")
862 r = app.request('/things', method='TRACE', status=405)
863 assert r.status_int == 405
864
865 def test_nested_rest_with_missing_intermediate_id(self):
866
867 class BarsController(RestController):
868
869 data = [['zero-zero', 'zero-one'], ['one-zero', 'one-one']]
870
871 @expose('json')
872 def get_all(self, foo_id):
873 return dict(items=self.data[int(foo_id)])
874
875 class FoosController(RestController):
876
877 data = ['zero', 'one']
878
879 bars = BarsController()
880
881 @expose()
882 def get_one(self, id):
883 return self.data[int(id)]
884
885 @expose('json')
886 def get_all(self):
887 return dict(items=self.data)
888
889 class RootController(object):
890 foos = FoosController()
891
892 # create the app
893 app = TestApp(make_app(RootController()))
894
895 # test get_all
896 r = app.get('/foos')
897 self.assertEqual(r.status_int, 200)
898 self.assertEqual(r.body, dumps(
899 dict(items=FoosController.data)
900 ).encode('utf-8'))
901
902 # test nested get_all
903 r = app.get('/foos/1/bars')
904 self.assertEqual(r.status_int, 200)
905 self.assertEqual(r.body, dumps(
906 dict(items=BarsController.data[1])
907 ).encode('utf-8'))
908
909 r = app.get('/foos/bars', expect_errors=True)
910 self.assertEqual(r.status_int, 404)
911
912 def test_custom_with_trailing_slash(self):
913
914 class CustomController(RestController):
915
916 _custom_actions = {
917 'detail': ['GET'],
918 'create': ['POST'],
919 'update': ['PUT'],
920 'remove': ['DELETE'],
921 }
922
923 @expose()
924 def detail(self):
925 return 'DETAIL'
926
927 @expose()
928 def create(self):
929 return 'CREATE'
930
931 @expose()
932 def update(self, id):
933 return id
934
935 @expose()
936 def remove(self, id):
937 return id
938
939 app = TestApp(make_app(CustomController()))
940
941 r = app.get('/detail')
942 assert r.status_int == 200
943 assert r.body == b'DETAIL'
944
945 r = app.get('/detail/')
946 assert r.status_int == 200
947 assert r.body == b'DETAIL'
948
949 r = app.post('/create')
950 assert r.status_int == 200
951 assert r.body == b'CREATE'
952
953 r = app.post('/create/')
954 assert r.status_int == 200
955 assert r.body == b'CREATE'
956
957 r = app.put('/update/123')
958 assert r.status_int == 200
959 assert r.body == b'123'
960
961 r = app.put('/update/123/')
962 assert r.status_int == 200
963 assert r.body == b'123'
964
965 r = app.delete('/remove/456')
966 assert r.status_int == 200
967 assert r.body == b'456'
968
969 r = app.delete('/remove/456/')
970 assert r.status_int == 200
971 assert r.body == b'456'
972
973 def test_custom_delete(self):
974
975 class OthersController(object):
976
977 @expose()
978 def index(self):
979 return 'DELETE'
980
981 @expose()
982 def reset(self, id):
983 return str(id)
984
985 class ThingsController(RestController):
986
987 others = OthersController()
988
989 @expose()
990 def delete_fail(self):
991 abort(500)
992
993 class RootController(object):
994 things = ThingsController()
995
996 # create the app
997 app = TestApp(make_app(RootController()))
998
999 # test bad delete
1000 r = app.delete('/things/delete_fail', status=405)
1001 assert r.status_int == 405
1002
1003 # test bad delete with _method parameter and GET
1004 r = app.get('/things/delete_fail?_method=delete', status=405)
1005 assert r.status_int == 405
1006
1007 # test bad delete with _method parameter and POST
1008 r = app.post('/things/delete_fail', {'_method': 'delete'}, status=405)
1009 assert r.status_int == 405
1010
1011 # test custom delete without ID
1012 r = app.delete('/things/others/')
1013 assert r.status_int == 200
1014 assert r.body == b'DELETE'
1015
1016 # test custom delete without ID with _method parameter and GET
1017 r = app.get('/things/others/?_method=delete', status=405)
1018 assert r.status_int == 405
1019
1020 # test custom delete without ID with _method parameter and POST
1021 r = app.post('/things/others/', {'_method': 'delete'})
1022 assert r.status_int == 200
1023 assert r.body == b'DELETE'
1024
1025 # test custom delete with ID
1026 r = app.delete('/things/others/reset/1')
1027 assert r.status_int == 200
1028 assert r.body == b'1'
1029
1030 # test custom delete with ID with _method parameter and GET
1031 r = app.get('/things/others/reset/1?_method=delete', status=405)
1032 assert r.status_int == 405
1033
1034 # test custom delete with ID with _method parameter and POST
1035 r = app.post('/things/others/reset/1', {'_method': 'delete'})
1036 assert r.status_int == 200
1037 assert r.body == b'1'
1038
1039 def test_get_with_var_args(self):
1040
1041 class OthersController(object):
1042
1043 @expose()
1044 def index(self, one, two, three):
1045 return 'NESTED: %s, %s, %s' % (one, two, three)
1046
1047 class ThingsController(RestController):
1048
1049 others = OthersController()
1050
1051 @expose()
1052 def get_one(self, *args):
1053 return ', '.join(args)
1054
1055 class RootController(object):
1056 things = ThingsController()
1057
1058 # create the app
1059 app = TestApp(make_app(RootController()))
1060
1061 # test get request
1062 r = app.get('/things/one/two/three')
1063 assert r.status_int == 200
1064 assert r.body == b'one, two, three'
1065
1066 # test nested get request
1067 r = app.get('/things/one/two/three/others/')
1068 assert r.status_int == 200
1069 assert r.body == b'NESTED: one, two, three'
1070
1071 def test_sub_nested_rest(self):
1072
1073 class BazsController(RestController):
1074
1075 data = [[['zero-zero-zero']]]
1076
1077 @expose()
1078 def get_one(self, foo_id, bar_id, id):
1079 return self.data[int(foo_id)][int(bar_id)][int(id)]
1080
1081 class BarsController(RestController):
1082
1083 data = [['zero-zero']]
1084
1085 bazs = BazsController()
1086
1087 @expose()
1088 def get_one(self, foo_id, id):
1089 return self.data[int(foo_id)][int(id)]
1090
1091 class FoosController(RestController):
1092
1093 data = ['zero']
1094
1095 bars = BarsController()
1096
1097 @expose()
1098 def get_one(self, id):
1099 return self.data[int(id)]
1100
1101 class RootController(object):
1102 foos = FoosController()
1103
1104 # create the app
1105 app = TestApp(make_app(RootController()))
1106
1107 # test sub-nested get_one
1108 r = app.get('/foos/0/bars/0/bazs/0')
1109 assert r.status_int == 200
1110 assert r.body == b'zero-zero-zero'
1111
1112 def test_sub_nested_rest_with_overwrites(self):
1113
1114 class FinalController(object):
1115
1116 @expose()
1117 def index(self):
1118 return 'FINAL'
1119
1120 @expose()
1121 def named(self):
1122 return 'NAMED'
1123
1124 class BazsController(RestController):
1125
1126 data = [[['zero-zero-zero']]]
1127
1128 final = FinalController()
1129
1130 @expose()
1131 def get_one(self, foo_id, bar_id, id):
1132 return self.data[int(foo_id)][int(bar_id)][int(id)]
1133
1134 @expose()
1135 def post(self):
1136 return 'POST-GRAND-CHILD'
1137
1138 @expose()
1139 def put(self, id):
1140 return 'PUT-GRAND-CHILD'
1141
1142 class BarsController(RestController):
1143
1144 data = [['zero-zero']]
1145
1146 bazs = BazsController()
1147
1148 @expose()
1149 def get_one(self, foo_id, id):
1150 return self.data[int(foo_id)][int(id)]
1151
1152 @expose()
1153 def post(self):
1154 return 'POST-CHILD'
1155
1156 @expose()
1157 def put(self, id):
1158 return 'PUT-CHILD'
1159
1160 class FoosController(RestController):
1161
1162 data = ['zero']
1163
1164 bars = BarsController()
1165
1166 @expose()
1167 def get_one(self, id):
1168 return self.data[int(id)]
1169
1170 @expose()
1171 def post(self):
1172 return 'POST'
1173
1174 @expose()
1175 def put(self, id):
1176 return 'PUT'
1177
1178 class RootController(object):
1179 foos = FoosController()
1180
1181 # create the app
1182 app = TestApp(make_app(RootController()))
1183
1184 r = app.post('/foos')
1185 assert r.status_int == 200
1186 assert r.body == b'POST'
1187
1188 r = app.put('/foos/0')
1189 assert r.status_int == 200
1190 assert r.body == b'PUT'
1191
1192 r = app.post('/foos/bars')
1193 assert r.status_int == 200
1194 assert r.body == b'POST-CHILD'
1195
1196 r = app.put('/foos/bars/0')
1197 assert r.status_int == 200
1198 assert r.body == b'PUT-CHILD'
1199
1200 r = app.post('/foos/bars/bazs')
1201 assert r.status_int == 200
1202 assert r.body == b'POST-GRAND-CHILD'
1203
1204 r = app.put('/foos/bars/bazs/0')
1205 assert r.status_int == 200
1206 assert r.body == b'PUT-GRAND-CHILD'
1207
1208 r = app.get('/foos/bars/bazs/final/')
1209 assert r.status_int == 200
1210 assert r.body == b'FINAL'
1211
1212 r = app.get('/foos/bars/bazs/final/named')
1213 assert r.status_int == 200
1214 assert r.body == b'NAMED'
1215
1216 def test_post_with_kwargs_only(self):
1217
1218 class RootController(RestController):
1219
1220 @expose()
1221 def get_all(self):
1222 return 'INDEX'
1223
1224 @expose('json')
1225 def post(self, **kw):
1226 return kw
1227
1228 # create the app
1229 app = TestApp(make_app(RootController()))
1230
1231 r = app.get('/')
1232 assert r.status_int == 200
1233 assert r.body == b'INDEX'
1234
1235 kwargs = {'foo': 'bar', 'spam': 'eggs'}
1236 r = app.post('/', kwargs)
1237 assert r.status_int == 200
1238 assert r.namespace['foo'] == 'bar'
1239 assert r.namespace['spam'] == 'eggs'
1240
1241 def test_nested_rest_with_lookup(self):
1242
1243 class SubController(RestController):
1244
1245 @expose()
1246 def get_all(self):
1247 return "SUB"
1248
1249 class FinalController(RestController):
1250
1251 def __init__(self, id_):
1252 self.id_ = id_
1253
1254 @expose()
1255 def get_all(self):
1256 return "FINAL-%s" % self.id_
1257
1258 @expose()
1259 def post(self):
1260 return "POST-%s" % self.id_
1261
1262 class LookupController(RestController):
1263
1264 sub = SubController()
1265
1266 def __init__(self, id_):
1267 self.id_ = id_
1268
1269 @expose()
1270 def _lookup(self, id_, *remainder):
1271 return FinalController(id_), remainder
1272
1273 @expose()
1274 def get_all(self):
1275 raise AssertionError("Never Reached")
1276
1277 @expose()
1278 def post(self):
1279 return "POST-LOOKUP-%s" % self.id_
1280
1281 @expose()
1282 def put(self, id_):
1283 return "PUT-LOOKUP-%s-%s" % (self.id_, id_)
1284
1285 @expose()
1286 def delete(self, id_):
1287 return "DELETE-LOOKUP-%s-%s" % (self.id_, id_)
1288
1289 class FooController(RestController):
1290
1291 @expose()
1292 def _lookup(self, id_, *remainder):
1293 return LookupController(id_), remainder
1294
1295 @expose()
1296 def get_one(self, id_):
1297 return "GET ONE"
1298
1299 @expose()
1300 def get_all(self):
1301 return "INDEX"
1302
1303 @expose()
1304 def post(self):
1305 return "POST"
1306
1307 @expose()
1308 def put(self, id_):
1309 return "PUT-%s" % id_
1310
1311 @expose()
1312 def delete(self, id_):
1313 return "DELETE-%s" % id_
1314
1315 class RootController(RestController):
1316 foo = FooController()
1317
1318 app = TestApp(make_app(RootController()))
1319
1320 r = app.get('/foo')
1321 assert r.status_int == 200
1322 assert r.body == b'INDEX'
1323
1324 r = app.post('/foo')
1325 assert r.status_int == 200
1326 assert r.body == b'POST'
1327
1328 r = app.get('/foo/1')
1329 assert r.status_int == 200
1330 assert r.body == b'GET ONE'
1331
1332 r = app.post('/foo/1')
1333 assert r.status_int == 200
1334 assert r.body == b'POST-LOOKUP-1'
1335
1336 r = app.put('/foo/1')
1337 assert r.status_int == 200
1338 assert r.body == b'PUT-1'
1339
1340 r = app.delete('/foo/1')
1341 assert r.status_int == 200
1342 assert r.body == b'DELETE-1'
1343
1344 r = app.put('/foo/1/2')
1345 assert r.status_int == 200
1346 assert r.body == b'PUT-LOOKUP-1-2'
1347
1348 r = app.delete('/foo/1/2')
1349 assert r.status_int == 200
1350 assert r.body == b'DELETE-LOOKUP-1-2'
1351
1352 r = app.get('/foo/1/2')
1353 assert r.status_int == 200
1354 assert r.body == b'FINAL-2'
1355
1356 r = app.post('/foo/1/2')
1357 assert r.status_int == 200
1358 assert r.body == b'POST-2'
1359
1360 def test_nested_rest_with_default(self):
1361
1362 class FooController(RestController):
1363
1364 @expose()
1365 def _default(self, *remainder):
1366 return "DEFAULT %s" % remainder
1367
1368 class RootController(RestController):
1369 foo = FooController()
1370
1371 app = TestApp(make_app(RootController()))
1372
1373 r = app.get('/foo/missing')
1374 assert r.status_int == 200
1375 assert r.body == b"DEFAULT missing"
1376
1377 def test_dynamic_rest_lookup(self):
1378 class BarController(RestController):
1379 @expose()
1380 def get_all(self):
1381 return "BAR"
1382
1383 @expose()
1384 def put(self):
1385 return "PUT_BAR"
1386
1387 @expose()
1388 def delete(self):
1389 return "DELETE_BAR"
1390
1391 class BarsController(RestController):
1392 @expose()
1393 def _lookup(self, id_, *remainder):
1394 return BarController(), remainder
1395
1396 @expose()
1397 def get_all(self):
1398 return "BARS"
1399
1400 @expose()
1401 def post(self):
1402 return "POST_BARS"
1403
1404 class FooController(RestController):
1405 bars = BarsController()
1406
1407 @expose()
1408 def get_all(self):
1409 return "FOO"
1410
1411 @expose()
1412 def put(self):
1413 return "PUT_FOO"
1414
1415 @expose()
1416 def delete(self):
1417 return "DELETE_FOO"
1418
1419 class FoosController(RestController):
1420 @expose()
1421 def _lookup(self, id_, *remainder):
1422 return FooController(), remainder
1423
1424 @expose()
1425 def get_all(self):
1426 return "FOOS"
1427
1428 @expose()
1429 def post(self):
1430 return "POST_FOOS"
1431
1432 class RootController(RestController):
1433 foos = FoosController()
1434
1435 app = TestApp(make_app(RootController()))
1436
1437 r = app.get('/foos')
1438 assert r.status_int == 200
1439 assert r.body == b'FOOS'
1440
1441 r = app.post('/foos')
1442 assert r.status_int == 200
1443 assert r.body == b'POST_FOOS'
1444
1445 r = app.get('/foos/foo')
1446 assert r.status_int == 200
1447 assert r.body == b'FOO'
1448
1449 r = app.put('/foos/foo')
1450 assert r.status_int == 200
1451 assert r.body == b'PUT_FOO'
1452
1453 r = app.delete('/foos/foo')
1454 assert r.status_int == 200
1455 assert r.body == b'DELETE_FOO'
1456
1457 r = app.get('/foos/foo/bars')
1458 assert r.status_int == 200
1459 assert r.body == b'BARS'
1460
1461 r = app.post('/foos/foo/bars')
1462 assert r.status_int == 200
1463 assert r.body == b'POST_BARS'
1464
1465 r = app.get('/foos/foo/bars/bar')
1466 assert r.status_int == 200
1467 assert r.body == b'BAR'
1468
1469 r = app.put('/foos/foo/bars/bar')
1470 assert r.status_int == 200
1471 assert r.body == b'PUT_BAR'
1472
1473 r = app.delete('/foos/foo/bars/bar')
1474 assert r.status_int == 200
1475 assert r.body == b'DELETE_BAR'
1476
1477 def test_method_not_allowed_get(self):
1478 class ThingsController(RestController):
1479
1480 @expose()
1481 def put(self, id_, value):
1482 response.status = 200
1483
1484 @expose()
1485 def delete(self, id_):
1486 response.status = 200
1487
1488 app = TestApp(make_app(ThingsController()))
1489 r = app.get('/', status=405)
1490 assert r.status_int == 405
1491 assert r.headers['Allow'] == 'DELETE, PUT'
1492
1493 def test_method_not_allowed_post(self):
1494 class ThingsController(RestController):
1495
1496 @expose()
1497 def get_one(self):
1498 return dict()
1499
1500 app = TestApp(make_app(ThingsController()))
1501 r = app.post('/', {'foo': 'bar'}, status=405)
1502 assert r.status_int == 405
1503 assert r.headers['Allow'] == 'GET'
1504
1505 def test_method_not_allowed_put(self):
1506 class ThingsController(RestController):
1507
1508 @expose()
1509 def get_one(self):
1510 return dict()
1511
1512 app = TestApp(make_app(ThingsController()))
1513 r = app.put('/123', status=405)
1514 assert r.status_int == 405
1515 assert r.headers['Allow'] == 'GET'
1516
1517 def test_method_not_allowed_delete(self):
1518 class ThingsController(RestController):
1519
1520 @expose()
1521 def get_one(self):
1522 return dict()
1523
1524 app = TestApp(make_app(ThingsController()))
1525 r = app.delete('/123', status=405)
1526 assert r.status_int == 405
1527 assert r.headers['Allow'] == 'GET'
1528
1529 def test_proper_allow_header_multiple_gets(self):
1530 class ThingsController(RestController):
1531
1532 @expose()
1533 def get_all(self):
1534 return dict()
1535
1536 @expose()
1537 def get(self):
1538 return dict()
1539
1540 app = TestApp(make_app(ThingsController()))
1541 r = app.put('/123', status=405)
1542 assert r.status_int == 405
1543 assert r.headers['Allow'] == 'GET'
1544
1545 @unittest.skipIf(sys.maxunicode <= 65536, 'narrow python build with UCS-2')
1546 def test_rest_with_utf8_uri(self):
1547
1548 class FooController(RestController):
1549 key = chr(0x1F330)
1550 data = {key: 'Success!'}
1551
1552 @expose()
1553 def get_one(self, id_):
1554 return self.data[id_]
1555
1556 @expose()
1557 def get_all(self):
1558 return "Hello, World!"
1559
1560 @expose()
1561 def put(self, id_, value):
1562 return self.data[id_]
1563
1564 @expose()
1565 def delete(self, id_):
1566 return self.data[id_]
1567
1568 class RootController(RestController):
1569 foo = FooController()
1570
1571 app = TestApp(make_app(RootController()))
1572
1573 r = app.get('/foo/%F0%9F%8C%B0')
1574 assert r.status_int == 200
1575 assert r.body == b'Success!'
1576
1577 r = app.put('/foo/%F0%9F%8C%B0', {'value': 'pecans'})
1578 assert r.status_int == 200
1579 assert r.body == b'Success!'
1580
1581 r = app.delete('/foo/%F0%9F%8C%B0')
1582 assert r.status_int == 200
1583 assert r.body == b'Success!'
1584
1585 r = app.get('/foo/')
1586 assert r.status_int == 200
1587 assert r.body == b'Hello, World!'
1588
1589 def test_rest_with_utf8_endpoint(self):
1590 class ChildController(object):
1591 @expose()
1592 def index(self):
1593 return 'Hello, World!'
1594
1595 class FooController(RestController):
1596 pass
1597
1598 # okay, so it's technically a chestnut, but close enough...
1599 setattr(FooController, '🌰', ChildController())
1600
1601 class RootController(RestController):
1602 foo = FooController()
1603
1604 app = TestApp(make_app(RootController()))
1605
1606 r = app.get('/foo/%F0%9F%8C%B0/')
1607 assert r.status_int == 200
1608 assert r.body == b'Hello, World!'
1609
1610
1611 class TestExplicitRoute(PecanTestCase):
1612
1613 def test_alternate_route(self):
1614
1615 class RootController(RestController):
1616
1617 @expose(route='some-path')
1618 def get_all(self):
1619 return "Hello, World!"
1620
1621 self.assertRaises(
1622 ValueError,
1623 RootController
1624 )