forked from pecan/pecan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_base.py
2334 lines (1830 loc) · 72.2 KB
/
test_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import sys
import os
import json
import traceback
import warnings
import webob
from webob.exc import HTTPNotFound
import mock
from webtest import TestApp
import six
from six import b as b_
from six import u as u_
from six.moves import cStringIO as StringIO
from pecan import (
Pecan, Request, Response, expose, request, response, redirect,
abort, make_app, override_template, render, route
)
from pecan.templating import (
_builtin_renderers as builtin_renderers, error_formatters, MakoRenderer
)
from pecan.decorators import accept_noncanonical
from pecan.tests import PecanTestCase
if sys.version_info < (2, 7):
import unittest2 as unittest # pragma: nocover
else:
import unittest # pragma: nocover
class SampleRootController(object):
pass
class TestAppRoot(PecanTestCase):
def test_controller_lookup_by_string_path(self):
app = Pecan('pecan.tests.test_base.SampleRootController')
assert app.root and isinstance(app.root, SampleRootController)
class TestEmptyContent(PecanTestCase):
@property
def app_(self):
class RootController(object):
@expose()
def index(self):
pass
@expose()
def explicit_body(self):
response.body = b_('Hello, World!')
@expose()
def empty_body(self):
response.body = b_('')
@expose()
def explicit_text(self):
response.text = six.text_type('Hello, World!')
@expose()
def empty_text(self):
response.text = six.text_type('')
@expose()
def explicit_json(self):
response.json = {'foo': 'bar'}
@expose()
def explicit_json_body(self):
response.json_body = {'foo': 'bar'}
@expose()
def non_unicode(self):
return chr(0xc0)
return TestApp(Pecan(RootController()))
def test_empty_index(self):
r = self.app_.get('/')
self.assertEqual(r.status_int, 204)
self.assertNotIn('Content-Type', r.headers)
self.assertEqual(r.headers['Content-Length'], '0')
self.assertEqual(len(r.body), 0)
def test_index_with_non_unicode(self):
r = self.app_.get('/non_unicode/')
self.assertEqual(r.status_int, 200)
def test_explicit_body(self):
r = self.app_.get('/explicit_body/')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b_('Hello, World!'))
def test_empty_body(self):
r = self.app_.get('/empty_body/')
self.assertEqual(r.status_int, 204)
self.assertEqual(r.body, b_(''))
def test_explicit_text(self):
r = self.app_.get('/explicit_text/')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b_('Hello, World!'))
def test_empty_text(self):
r = self.app_.get('/empty_text/')
self.assertEqual(r.status_int, 204)
self.assertEqual(r.body, b_(''))
def test_explicit_json(self):
r = self.app_.get('/explicit_json/')
self.assertEqual(r.status_int, 200)
json_resp = json.loads(r.body.decode())
assert json_resp == {'foo': 'bar'}
def test_explicit_json_body(self):
r = self.app_.get('/explicit_json_body/')
self.assertEqual(r.status_int, 200)
json_resp = json.loads(r.body.decode())
assert json_resp == {'foo': 'bar'}
class TestAppIterFile(PecanTestCase):
@property
def app_(self):
class RootController(object):
@expose()
def index(self):
body = six.BytesIO(b_('Hello, World!'))
response.body_file = body
@expose()
def empty(self):
body = six.BytesIO(b_(''))
response.body_file = body
return TestApp(Pecan(RootController()))
def test_body_generator(self):
r = self.app_.get('/')
self.assertEqual(r.status_int, 200)
assert r.body == b_('Hello, World!')
def test_empty_body_generator(self):
r = self.app_.get('/empty')
self.assertEqual(r.status_int, 204)
assert len(r.body) == 0
class TestInvalidURLEncoding(PecanTestCase):
@property
def app_(self):
class RootController(object):
@expose()
def _route(self, args, request):
assert request.path
return TestApp(Pecan(RootController()))
def test_rest_with_non_utf_8_body(self):
r = self.app_.get('/%aa/', expect_errors=True)
assert r.status_int == 400
class TestIndexRouting(PecanTestCase):
@property
def app_(self):
class RootController(object):
@expose()
def index(self):
return 'Hello, World!'
return TestApp(Pecan(RootController()))
def test_empty_root(self):
r = self.app_.get('/')
assert r.status_int == 200
assert r.body == b_('Hello, World!')
def test_index(self):
r = self.app_.get('/index')
assert r.status_int == 200
assert r.body == b_('Hello, World!')
def test_index_html(self):
r = self.app_.get('/index.html')
assert r.status_int == 200
assert r.body == b_('Hello, World!')
class TestObjectDispatch(PecanTestCase):
@property
def app_(self):
class SubSubController(object):
@expose()
def index(self):
return '/sub/sub/'
@expose()
def deeper(self):
return '/sub/sub/deeper'
class SubController(object):
@expose()
def index(self):
return '/sub/'
@expose()
def deeper(self):
return '/sub/deeper'
sub = SubSubController()
class RootController(object):
@expose()
def index(self):
return '/'
@expose()
def deeper(self):
return '/deeper'
sub = SubController()
return TestApp(Pecan(RootController()))
def test_index(self):
r = self.app_.get('/')
assert r.status_int == 200
assert r.body == b_('/')
def test_one_level(self):
r = self.app_.get('/deeper')
assert r.status_int == 200
assert r.body == b_('/deeper')
def test_one_level_with_trailing(self):
r = self.app_.get('/sub/')
assert r.status_int == 200
assert r.body == b_('/sub/')
def test_two_levels(self):
r = self.app_.get('/sub/deeper')
assert r.status_int == 200
assert r.body == b_('/sub/deeper')
def test_two_levels_with_trailing(self):
r = self.app_.get('/sub/sub/')
assert r.status_int == 200
def test_three_levels(self):
r = self.app_.get('/sub/sub/deeper')
assert r.status_int == 200
assert r.body == b_('/sub/sub/deeper')
@unittest.skipIf(not six.PY3, "tests are Python3 specific")
class TestUnicodePathSegments(PecanTestCase):
def test_unicode_methods(self):
class RootController(object):
pass
setattr(RootController, '🌰', expose()(lambda self: 'Hello, World!'))
app = TestApp(Pecan(RootController()))
resp = app.get('/%F0%9F%8C%B0/')
assert resp.status_int == 200
assert resp.body == b_('Hello, World!')
def test_unicode_child(self):
class ChildController(object):
@expose()
def index(self):
return 'Hello, World!'
class RootController(object):
pass
setattr(RootController, '🌰', ChildController())
app = TestApp(Pecan(RootController()))
resp = app.get('/%F0%9F%8C%B0/')
assert resp.status_int == 200
assert resp.body == b_('Hello, World!')
class TestLookups(PecanTestCase):
@property
def app_(self):
class LookupController(object):
def __init__(self, someID):
self.someID = someID
@expose()
def index(self):
return '/%s' % self.someID
@expose()
def name(self):
return '/%s/name' % self.someID
class RootController(object):
@expose()
def index(self):
return '/'
@expose()
def _lookup(self, someID, *remainder):
return LookupController(someID), remainder
return TestApp(Pecan(RootController()))
def test_index(self):
r = self.app_.get('/')
assert r.status_int == 200
assert r.body == b_('/')
def test_lookup(self):
r = self.app_.get('/100/')
assert r.status_int == 200
assert r.body == b_('/100')
def test_lookup_with_method(self):
r = self.app_.get('/100/name')
assert r.status_int == 200
assert r.body == b_('/100/name')
def test_lookup_with_wrong_argspec(self):
class RootController(object):
@expose()
def _lookup(self, someID):
return 'Bad arg spec' # pragma: nocover
with warnings.catch_warnings():
warnings.simplefilter("ignore")
app = TestApp(Pecan(RootController()))
r = app.get('/foo/bar', expect_errors=True)
assert r.status_int == 404
class TestCanonicalLookups(PecanTestCase):
@property
def app_(self):
class LookupController(object):
def __init__(self, someID):
self.someID = someID
@expose()
def index(self):
return self.someID
class UserController(object):
@expose()
def _lookup(self, someID, *remainder):
return LookupController(someID), remainder
class RootController(object):
users = UserController()
return TestApp(Pecan(RootController()))
def test_canonical_lookup(self):
assert self.app_.get('/users', expect_errors=404).status_int == 404
assert self.app_.get('/users/', expect_errors=404).status_int == 404
assert self.app_.get('/users/100').status_int == 302
assert self.app_.get('/users/100/').body == b_('100')
class TestControllerArguments(PecanTestCase):
@property
def app_(self):
class RootController(object):
@expose()
def index(self, id):
return 'index: %s' % id
@expose()
def multiple(self, one, two):
return 'multiple: %s, %s' % (one, two)
@expose()
def optional(self, id=None):
return 'optional: %s' % str(id)
@expose()
def multiple_optional(self, one=None, two=None, three=None):
return 'multiple_optional: %s, %s, %s' % (one, two, three)
@expose()
def variable_args(self, *args):
return 'variable_args: %s' % ', '.join(args)
@expose()
def variable_kwargs(self, **kwargs):
data = [
'%s=%s' % (key, kwargs[key])
for key in sorted(kwargs.keys())
]
return 'variable_kwargs: %s' % ', '.join(data)
@expose()
def variable_all(self, *args, **kwargs):
data = [
'%s=%s' % (key, kwargs[key])
for key in sorted(kwargs.keys())
]
return 'variable_all: %s' % ', '.join(list(args) + data)
@expose()
def eater(self, id, dummy=None, *args, **kwargs):
data = [
'%s=%s' % (key, kwargs[key])
for key in sorted(kwargs.keys())
]
return 'eater: %s, %s, %s' % (
id,
dummy,
', '.join(list(args) + data)
)
@staticmethod
@expose()
def static(id):
return "id is %s" % id
@expose()
def _route(self, args, request):
if hasattr(self, args[0]):
return getattr(self, args[0]), args[1:]
else:
return self.index, args
return TestApp(Pecan(RootController()))
def test_required_argument(self):
try:
r = self.app_.get('/')
assert r.status_int != 200 # pragma: nocover
except Exception as ex:
assert type(ex) == TypeError
assert ex.args[0] in (
"index() takes exactly 2 arguments (1 given)",
"index() missing 1 required positional argument: 'id'"
) # this messaging changed in Python 3.3
def test_single_argument(self):
r = self.app_.get('/1')
assert r.status_int == 200
assert r.body == b_('index: 1')
def test_single_argument_with_encoded_url(self):
r = self.app_.get('/This%20is%20a%20test%21')
assert r.status_int == 200
assert r.body == b_('index: This is a test!')
def test_single_argument_with_plus(self):
r = self.app_.get('/foo+bar')
assert r.status_int == 200
assert r.body == b_('index: foo+bar')
def test_single_argument_with_encoded_plus(self):
r = self.app_.get('/foo%2Bbar')
assert r.status_int == 200
assert r.body == b_('index: foo+bar')
def test_two_arguments(self):
r = self.app_.get('/1/dummy', status=404)
assert r.status_int == 404
def test_keyword_argument(self):
r = self.app_.get('/?id=2')
assert r.status_int == 200
assert r.body == b_('index: 2')
def test_keyword_argument_with_encoded_url(self):
r = self.app_.get('/?id=This%20is%20a%20test%21')
assert r.status_int == 200
assert r.body == b_('index: This is a test!')
def test_keyword_argument_with_plus(self):
r = self.app_.get('/?id=foo+bar')
assert r.status_int == 200
assert r.body == b_('index: foo bar')
def test_keyword_argument_with_encoded_plus(self):
r = self.app_.get('/?id=foo%2Bbar')
assert r.status_int == 200
assert r.body == b_('index: foo+bar')
def test_argument_and_keyword_argument(self):
r = self.app_.get('/3?id=three')
assert r.status_int == 200
assert r.body == b_('index: 3')
def test_encoded_argument_and_keyword_argument(self):
r = self.app_.get('/This%20is%20a%20test%21?id=three')
assert r.status_int == 200
assert r.body == b_('index: This is a test!')
def test_explicit_kwargs(self):
r = self.app_.post('/', {'id': '4'})
assert r.status_int == 200
assert r.body == b_('index: 4')
def test_path_with_explicit_kwargs(self):
r = self.app_.post('/4', {'id': 'four'})
assert r.status_int == 200
assert r.body == b_('index: 4')
def test_explicit_json_kwargs(self):
r = self.app_.post_json('/', {'id': '4'})
assert r.status_int == 200
assert r.body == b_('index: 4')
def test_path_with_explicit_json_kwargs(self):
r = self.app_.post_json('/4', {'id': 'four'})
assert r.status_int == 200
assert r.body == b_('index: 4')
def test_multiple_kwargs(self):
r = self.app_.get('/?id=5&dummy=dummy')
assert r.status_int == 200
assert r.body == b_('index: 5')
def test_kwargs_from_root(self):
r = self.app_.post('/', {'id': '6', 'dummy': 'dummy'})
assert r.status_int == 200
assert r.body == b_('index: 6')
def test_json_kwargs_from_root(self):
r = self.app_.post_json('/', {'id': '6', 'dummy': 'dummy'})
assert r.status_int == 200
assert r.body == b_('index: 6')
# multiple args
def test_multiple_positional_arguments(self):
r = self.app_.get('/multiple/one/two')
assert r.status_int == 200
assert r.body == b_('multiple: one, two')
def test_multiple_positional_arguments_with_url_encode(self):
r = self.app_.get('/multiple/One%20/Two%21')
assert r.status_int == 200
assert r.body == b_('multiple: One , Two!')
def test_multiple_positional_arguments_with_kwargs(self):
r = self.app_.get('/multiple?one=three&two=four')
assert r.status_int == 200
assert r.body == b_('multiple: three, four')
def test_multiple_positional_arguments_with_url_encoded_kwargs(self):
r = self.app_.get('/multiple?one=Three%20&two=Four%20%21')
assert r.status_int == 200
assert r.body == b_('multiple: Three , Four !')
def test_positional_args_with_dictionary_kwargs(self):
r = self.app_.post('/multiple', {'one': 'five', 'two': 'six'})
assert r.status_int == 200
assert r.body == b_('multiple: five, six')
def test_positional_args_with_json_kwargs(self):
r = self.app_.post_json('/multiple', {'one': 'five', 'two': 'six'})
assert r.status_int == 200
assert r.body == b_('multiple: five, six')
def test_positional_args_with_url_encoded_dictionary_kwargs(self):
r = self.app_.post('/multiple', {'one': 'Five%20', 'two': 'Six%20%21'})
assert r.status_int == 200
assert r.body == b_('multiple: Five%20, Six%20%21')
# optional arg
def test_optional_arg(self):
r = self.app_.get('/optional')
assert r.status_int == 200
assert r.body == b_('optional: None')
def test_multiple_optional(self):
r = self.app_.get('/optional/1')
assert r.status_int == 200
assert r.body == b_('optional: 1')
def test_multiple_optional_url_encoded(self):
r = self.app_.get('/optional/Some%20Number')
assert r.status_int == 200
assert r.body == b_('optional: Some Number')
def test_multiple_optional_missing(self):
r = self.app_.get('/optional/2/dummy', status=404)
assert r.status_int == 404
def test_multiple_with_kwargs(self):
r = self.app_.get('/optional?id=2')
assert r.status_int == 200
assert r.body == b_('optional: 2')
def test_multiple_with_url_encoded_kwargs(self):
r = self.app_.get('/optional?id=Some%20Number')
assert r.status_int == 200
assert r.body == b_('optional: Some Number')
def test_multiple_args_with_url_encoded_kwargs(self):
r = self.app_.get('/optional/3?id=three')
assert r.status_int == 200
assert r.body == b_('optional: 3')
def test_url_encoded_positional_args(self):
r = self.app_.get('/optional/Some%20Number?id=three')
assert r.status_int == 200
assert r.body == b_('optional: Some Number')
def test_optional_arg_with_kwargs(self):
r = self.app_.post('/optional', {'id': '4'})
assert r.status_int == 200
assert r.body == b_('optional: 4')
def test_optional_arg_with_json_kwargs(self):
r = self.app_.post_json('/optional', {'id': '4'})
assert r.status_int == 200
assert r.body == b_('optional: 4')
def test_optional_arg_with_url_encoded_kwargs(self):
r = self.app_.post('/optional', {'id': 'Some%20Number'})
assert r.status_int == 200
assert r.body == b_('optional: Some%20Number')
def test_multiple_positional_arguments_with_dictionary_kwargs(self):
r = self.app_.post('/optional/5', {'id': 'five'})
assert r.status_int == 200
assert r.body == b_('optional: 5')
def test_multiple_positional_arguments_with_json_kwargs(self):
r = self.app_.post_json('/optional/5', {'id': 'five'})
assert r.status_int == 200
assert r.body == b_('optional: 5')
def test_multiple_positional_url_encoded_arguments_with_kwargs(self):
r = self.app_.post('/optional/Some%20Number', {'id': 'five'})
assert r.status_int == 200
assert r.body == b_('optional: Some Number')
def test_optional_arg_with_multiple_kwargs(self):
r = self.app_.get('/optional?id=6&dummy=dummy')
assert r.status_int == 200
assert r.body == b_('optional: 6')
def test_optional_arg_with_multiple_url_encoded_kwargs(self):
r = self.app_.get('/optional?id=Some%20Number&dummy=dummy')
assert r.status_int == 200
assert r.body == b_('optional: Some Number')
def test_optional_arg_with_multiple_dictionary_kwargs(self):
r = self.app_.post('/optional', {'id': '7', 'dummy': 'dummy'})
assert r.status_int == 200
assert r.body == b_('optional: 7')
def test_optional_arg_with_multiple_json_kwargs(self):
r = self.app_.post_json('/optional', {'id': '7', 'dummy': 'dummy'})
assert r.status_int == 200
assert r.body == b_('optional: 7')
def test_optional_arg_with_multiple_url_encoded_dictionary_kwargs(self):
r = self.app_.post('/optional', {
'id': 'Some%20Number',
'dummy': 'dummy'
})
assert r.status_int == 200
assert r.body == b_('optional: Some%20Number')
# multiple optional args
def test_multiple_optional_positional_args(self):
r = self.app_.get('/multiple_optional')
assert r.status_int == 200
assert r.body == b_('multiple_optional: None, None, None')
def test_multiple_optional_positional_args_one_arg(self):
r = self.app_.get('/multiple_optional/1')
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_positional_args_one_url_encoded_arg(self):
r = self.app_.get('/multiple_optional/One%21')
assert r.status_int == 200
assert r.body == b_('multiple_optional: One!, None, None')
def test_multiple_optional_positional_args_all_args(self):
r = self.app_.get('/multiple_optional/1/2/3')
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, 2, 3')
def test_multiple_optional_positional_args_all_url_encoded_args(self):
r = self.app_.get('/multiple_optional/One%21/Two%21/Three%21')
assert r.status_int == 200
assert r.body == b_('multiple_optional: One!, Two!, Three!')
def test_multiple_optional_positional_args_too_many_args(self):
r = self.app_.get('/multiple_optional/1/2/3/dummy', status=404)
assert r.status_int == 404
def test_multiple_optional_positional_args_with_kwargs(self):
r = self.app_.get('/multiple_optional?one=1')
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_positional_args_with_url_encoded_kwargs(self):
r = self.app_.get('/multiple_optional?one=One%21')
assert r.status_int == 200
assert r.body == b_('multiple_optional: One!, None, None')
def test_multiple_optional_positional_args_with_string_kwargs(self):
r = self.app_.get('/multiple_optional/1?one=one')
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_positional_args_with_encoded_str_kwargs(self):
r = self.app_.get('/multiple_optional/One%21?one=one')
assert r.status_int == 200
assert r.body == b_('multiple_optional: One!, None, None')
def test_multiple_optional_positional_args_with_dict_kwargs(self):
r = self.app_.post('/multiple_optional', {'one': '1'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_positional_args_with_json_kwargs(self):
r = self.app_.post_json('/multiple_optional', {'one': '1'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_positional_args_with_encoded_dict_kwargs(self):
r = self.app_.post('/multiple_optional', {'one': 'One%21'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: One%21, None, None')
def test_multiple_optional_positional_args_and_dict_kwargs(self):
r = self.app_.post('/multiple_optional/1', {'one': 'one'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_positional_args_and_json_kwargs(self):
r = self.app_.post_json('/multiple_optional/1', {'one': 'one'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, None, None')
def test_multiple_optional_encoded_positional_args_and_dict_kwargs(self):
r = self.app_.post('/multiple_optional/One%21', {'one': 'one'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: One!, None, None')
def test_multiple_optional_args_with_multiple_kwargs(self):
r = self.app_.get('/multiple_optional?one=1&two=2&three=3&four=4')
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, 2, 3')
def test_multiple_optional_args_with_multiple_encoded_kwargs(self):
r = self.app_.get(
'/multiple_optional?one=One%21&two=Two%21&three=Three%21&four=4'
)
assert r.status_int == 200
assert r.body == b_('multiple_optional: One!, Two!, Three!')
def test_multiple_optional_args_with_multiple_dict_kwargs(self):
r = self.app_.post(
'/multiple_optional',
{'one': '1', 'two': '2', 'three': '3', 'four': '4'}
)
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, 2, 3')
def test_multiple_optional_args_with_multiple_json_kwargs(self):
r = self.app_.post_json(
'/multiple_optional',
{'one': '1', 'two': '2', 'three': '3', 'four': '4'}
)
assert r.status_int == 200
assert r.body == b_('multiple_optional: 1, 2, 3')
def test_multiple_optional_args_with_multiple_encoded_dict_kwargs(self):
r = self.app_.post(
'/multiple_optional',
{
'one': 'One%21',
'two': 'Two%21',
'three': 'Three%21',
'four': '4'
}
)
assert r.status_int == 200
assert r.body == b_('multiple_optional: One%21, Two%21, Three%21')
def test_multiple_optional_args_with_last_kwarg(self):
r = self.app_.get('/multiple_optional?three=3')
assert r.status_int == 200
assert r.body == b_('multiple_optional: None, None, 3')
def test_multiple_optional_args_with_last_encoded_kwarg(self):
r = self.app_.get('/multiple_optional?three=Three%21')
assert r.status_int == 200
assert r.body == b_('multiple_optional: None, None, Three!')
def test_multiple_optional_args_with_middle_arg(self):
r = self.app_.get('/multiple_optional', {'two': '2'})
assert r.status_int == 200
assert r.body == b_('multiple_optional: None, 2, None')
def test_variable_args(self):
r = self.app_.get('/variable_args')
assert r.status_int == 200
assert r.body == b_('variable_args: ')
def test_multiple_variable_args(self):
r = self.app_.get('/variable_args/1/dummy')
assert r.status_int == 200
assert r.body == b_('variable_args: 1, dummy')
def test_multiple_encoded_variable_args(self):
r = self.app_.get('/variable_args/Testing%20One%20Two/Three%21')
assert r.status_int == 200
assert r.body == b_('variable_args: Testing One Two, Three!')
def test_variable_args_with_kwargs(self):
r = self.app_.get('/variable_args?id=2&dummy=dummy')
assert r.status_int == 200
assert r.body == b_('variable_args: ')
def test_variable_args_with_dict_kwargs(self):
r = self.app_.post('/variable_args', {'id': '3', 'dummy': 'dummy'})
assert r.status_int == 200
assert r.body == b_('variable_args: ')
def test_variable_args_with_json_kwargs(self):
r = self.app_.post_json(
'/variable_args',
{'id': '3', 'dummy': 'dummy'}
)
assert r.status_int == 200
assert r.body == b_('variable_args: ')
def test_variable_kwargs(self):
r = self.app_.get('/variable_kwargs')
assert r.status_int == 200
assert r.body == b_('variable_kwargs: ')
def test_multiple_variable_kwargs(self):
r = self.app_.get('/variable_kwargs/1/dummy', status=404)
assert r.status_int == 404
def test_multiple_variable_kwargs_with_explicit_kwargs(self):
r = self.app_.get('/variable_kwargs?id=2&dummy=dummy')
assert r.status_int == 200
assert r.body == b_('variable_kwargs: dummy=dummy, id=2')
def test_multiple_variable_kwargs_with_explicit_encoded_kwargs(self):
r = self.app_.get(
'/variable_kwargs?id=Two%21&dummy=This%20is%20a%20test'
)
assert r.status_int == 200
assert r.body == b_('variable_kwargs: dummy=This is a test, id=Two!')
def test_multiple_variable_kwargs_with_dict_kwargs(self):
r = self.app_.post('/variable_kwargs', {'id': '3', 'dummy': 'dummy'})
assert r.status_int == 200
assert r.body == b_('variable_kwargs: dummy=dummy, id=3')
def test_multiple_variable_kwargs_with_json_kwargs(self):
r = self.app_.post_json(
'/variable_kwargs',
{'id': '3', 'dummy': 'dummy'}
)
assert r.status_int == 200
assert r.body == b_('variable_kwargs: dummy=dummy, id=3')
def test_multiple_variable_kwargs_with_encoded_dict_kwargs(self):
r = self.app_.post(
'/variable_kwargs',
{'id': 'Three%21', 'dummy': 'This%20is%20a%20test'}
)
assert r.status_int == 200
result = 'variable_kwargs: dummy=This%20is%20a%20test, id=Three%21'
assert r.body == b_(result)
def test_variable_all(self):
r = self.app_.get('/variable_all')
assert r.status_int == 200
assert r.body == b_('variable_all: ')
def test_variable_all_with_one_extra(self):
r = self.app_.get('/variable_all/1')
assert r.status_int == 200
assert r.body == b_('variable_all: 1')
def test_variable_all_with_two_extras(self):
r = self.app_.get('/variable_all/2/dummy')
assert r.status_int == 200
assert r.body == b_('variable_all: 2, dummy')
def test_variable_mixed(self):
r = self.app_.get('/variable_all/3?month=1&day=12')
assert r.status_int == 200
assert r.body == b_('variable_all: 3, day=12, month=1')
def test_variable_mixed_explicit(self):
r = self.app_.get('/variable_all/4?id=four&month=1&day=12')
assert r.status_int == 200
assert r.body == b_('variable_all: 4, day=12, id=four, month=1')
def test_variable_post(self):
r = self.app_.post('/variable_all/5/dummy')
assert r.status_int == 200
assert r.body == b_('variable_all: 5, dummy')
def test_variable_post_with_kwargs(self):
r = self.app_.post('/variable_all/6', {'month': '1', 'day': '12'})
assert r.status_int == 200
assert r.body == b_('variable_all: 6, day=12, month=1')
def test_variable_post_with_json_kwargs(self):
r = self.app_.post_json(
'/variable_all/6',
{'month': '1', 'day': '12'}
)
assert r.status_int == 200
assert r.body == b_('variable_all: 6, day=12, month=1')
def test_variable_post_mixed(self):
r = self.app_.post(
'/variable_all/7',
{'id': 'seven', 'month': '1', 'day': '12'}
)
assert r.status_int == 200
assert r.body == b_('variable_all: 7, day=12, id=seven, month=1')
def test_variable_post_mixed_with_json(self):
r = self.app_.post_json(
'/variable_all/7',
{'id': 'seven', 'month': '1', 'day': '12'}
)
assert r.status_int == 200
assert r.body == b_('variable_all: 7, day=12, id=seven, month=1')
def test_duplicate_query_parameters_GET(self):
r = self.app_.get('/variable_kwargs?list=1&list=2')
l = [u_('1'), u_('2')]
assert r.status_int == 200
assert r.body == b_('variable_kwargs: list=%s' % l)
def test_duplicate_query_parameters_POST(self):
r = self.app_.post('/variable_kwargs',
{'list': ['1', '2']})
l = [u_('1'), u_('2')]
assert r.status_int == 200
assert r.body == b_('variable_kwargs: list=%s' % l)
def test_duplicate_query_parameters_POST_mixed(self):
r = self.app_.post('/variable_kwargs?list=1&list=2',
{'list': ['3', '4']})
l = [u_('1'), u_('2'), u_('3'), u_('4')]
assert r.status_int == 200
assert r.body == b_('variable_kwargs: list=%s' % l)
def test_duplicate_query_parameters_POST_mixed_json(self):
r = self.app_.post('/variable_kwargs?list=1&list=2',
{'list': 3})
l = [u_('1'), u_('2'), u_('3')]
assert r.status_int == 200
assert r.body == b_('variable_kwargs: list=%s' % l)
def test_staticmethod(self):
r = self.app_.get('/static/foobar')
assert r.status_int == 200
assert r.body == b_('id is foobar')
def test_no_remainder(self):
try:
r = self.app_.get('/eater')
assert r.status_int != 200 # pragma: nocover
except Exception as ex:
assert type(ex) == TypeError
assert ex.args[0] in (
"eater() takes at least 2 arguments (1 given)",
"eater() missing 1 required positional argument: 'id'"
) # this messaging changed in Python 3.3
def test_one_remainder(self):
r = self.app_.get('/eater/1')
assert r.status_int == 200
assert r.body == b_('eater: 1, None, ')
def test_two_remainders(self):