-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_datastructures.py
1154 lines (931 loc) · 36.8 KB
/
test_datastructures.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 -*-
"""
tests.datastructures
~~~~~~~~~~~~~~~~~~~~
Tests the functionality of the provided Werkzeug
datastructures.
Classes prefixed with an underscore are mixins and are not discovered by
the test runner.
TODO:
- FileMultiDict
- Immutable types undertested
- Split up dict tests
:copyright: 2007 Pallets
:license: BSD-3-Clause
"""
import io
import pickle
import tempfile
from contextlib import contextmanager
from copy import copy
from copy import deepcopy
import pytest
from . import strict_eq
from werkzeug import datastructures
from werkzeug import http
from werkzeug._compat import iteritems
from werkzeug._compat import iterkeys
from werkzeug._compat import iterlists
from werkzeug._compat import iterlistvalues
from werkzeug._compat import itervalues
from werkzeug._compat import PY2
from werkzeug._compat import text_type
from werkzeug.datastructures import Range
from werkzeug.exceptions import BadRequestKeyError
class TestNativeItermethods(object):
def test_basic(self):
@datastructures.native_itermethods(["keys", "values", "items"])
class StupidDict(object):
def keys(self, multi=1):
return iter(["a", "b", "c"] * multi)
def values(self, multi=1):
return iter([1, 2, 3] * multi)
def items(self, multi=1):
return iter(
zip(iterkeys(self, multi=multi), itervalues(self, multi=multi))
)
d = StupidDict()
expected_keys = ["a", "b", "c"]
expected_values = [1, 2, 3]
expected_items = list(zip(expected_keys, expected_values))
assert list(iterkeys(d)) == expected_keys
assert list(itervalues(d)) == expected_values
assert list(iteritems(d)) == expected_items
assert list(iterkeys(d, 2)) == expected_keys * 2
assert list(itervalues(d, 2)) == expected_values * 2
assert list(iteritems(d, 2)) == expected_items * 2
class _MutableMultiDictTests(object):
storage_class = None
def test_pickle(self):
cls = self.storage_class
def create_instance(module=None):
if module is None:
d = cls()
else:
old = cls.__module__
cls.__module__ = module
d = cls()
cls.__module__ = old
d.setlist(b"foo", [1, 2, 3, 4])
d.setlist(b"bar", b"foo bar baz".split())
return d
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
d = create_instance()
s = pickle.dumps(d, protocol)
ud = pickle.loads(s)
assert type(ud) == type(d)
assert ud == d
alternative = pickle.dumps(create_instance("werkzeug"), protocol)
assert pickle.loads(alternative) == d
ud[b"newkey"] = b"bla"
assert ud != d
def test_basic_interface(self):
md = self.storage_class()
assert isinstance(md, dict)
mapping = [
("a", 1),
("b", 2),
("a", 2),
("d", 3),
("a", 1),
("a", 3),
("d", 4),
("c", 3),
]
md = self.storage_class(mapping)
# simple getitem gives the first value
assert md["a"] == 1
assert md["c"] == 3
with pytest.raises(KeyError):
md["e"]
assert md.get("a") == 1
# list getitem
assert md.getlist("a") == [1, 2, 1, 3]
assert md.getlist("d") == [3, 4]
# do not raise if key not found
assert md.getlist("x") == []
# simple setitem overwrites all values
md["a"] = 42
assert md.getlist("a") == [42]
# list setitem
md.setlist("a", [1, 2, 3])
assert md["a"] == 1
assert md.getlist("a") == [1, 2, 3]
# verify that it does not change original lists
l1 = [1, 2, 3]
md.setlist("a", l1)
del l1[:]
assert md["a"] == 1
# setdefault, setlistdefault
assert md.setdefault("u", 23) == 23
assert md.getlist("u") == [23]
del md["u"]
md.setlist("u", [-1, -2])
# delitem
del md["u"]
with pytest.raises(KeyError):
md["u"]
del md["d"]
assert md.getlist("d") == []
# keys, values, items, lists
assert list(sorted(md.keys())) == ["a", "b", "c"]
assert list(sorted(iterkeys(md))) == ["a", "b", "c"]
assert list(sorted(itervalues(md))) == [1, 2, 3]
assert list(sorted(itervalues(md))) == [1, 2, 3]
assert list(sorted(md.items())) == [("a", 1), ("b", 2), ("c", 3)]
assert list(sorted(md.items(multi=True))) == [
("a", 1),
("a", 2),
("a", 3),
("b", 2),
("c", 3),
]
assert list(sorted(iteritems(md))) == [("a", 1), ("b", 2), ("c", 3)]
assert list(sorted(iteritems(md, multi=True))) == [
("a", 1),
("a", 2),
("a", 3),
("b", 2),
("c", 3),
]
assert list(sorted(md.lists())) == [("a", [1, 2, 3]), ("b", [2]), ("c", [3])]
assert list(sorted(iterlists(md))) == [("a", [1, 2, 3]), ("b", [2]), ("c", [3])]
# copy method
c = md.copy()
assert c["a"] == 1
assert c.getlist("a") == [1, 2, 3]
# copy method 2
c = copy(md)
assert c["a"] == 1
assert c.getlist("a") == [1, 2, 3]
# deepcopy method
c = md.deepcopy()
assert c["a"] == 1
assert c.getlist("a") == [1, 2, 3]
# deepcopy method 2
c = deepcopy(md)
assert c["a"] == 1
assert c.getlist("a") == [1, 2, 3]
# update with a multidict
od = self.storage_class([("a", 4), ("a", 5), ("y", 0)])
md.update(od)
assert md.getlist("a") == [1, 2, 3, 4, 5]
assert md.getlist("y") == [0]
# update with a regular dict
md = c
od = {"a": 4, "y": 0}
md.update(od)
assert md.getlist("a") == [1, 2, 3, 4]
assert md.getlist("y") == [0]
# pop, poplist, popitem, popitemlist
assert md.pop("y") == 0
assert "y" not in md
assert md.poplist("a") == [1, 2, 3, 4]
assert "a" not in md
assert md.poplist("missing") == []
# remaining: b=2, c=3
popped = md.popitem()
assert popped in [("b", 2), ("c", 3)]
popped = md.popitemlist()
assert popped in [("b", [2]), ("c", [3])]
# type conversion
md = self.storage_class({"a": "4", "b": ["2", "3"]})
assert md.get("a", type=int) == 4
assert md.getlist("b", type=int) == [2, 3]
# repr
md = self.storage_class([("a", 1), ("a", 2), ("b", 3)])
assert "('a', 1)" in repr(md)
assert "('a', 2)" in repr(md)
assert "('b', 3)" in repr(md)
# add and getlist
md.add("c", "42")
md.add("c", "23")
assert md.getlist("c") == ["42", "23"]
md.add("c", "blah")
assert md.getlist("c", type=int) == [42, 23]
# setdefault
md = self.storage_class()
md.setdefault("x", []).append(42)
md.setdefault("x", []).append(23)
assert md["x"] == [42, 23]
# to dict
md = self.storage_class()
md["foo"] = 42
md.add("bar", 1)
md.add("bar", 2)
assert md.to_dict() == {"foo": 42, "bar": 1}
assert md.to_dict(flat=False) == {"foo": [42], "bar": [1, 2]}
# popitem from empty dict
with pytest.raises(KeyError):
self.storage_class().popitem()
with pytest.raises(KeyError):
self.storage_class().popitemlist()
# key errors are of a special type
with pytest.raises(BadRequestKeyError):
self.storage_class()[42]
# setlist works
md = self.storage_class()
md["foo"] = 42
md.setlist("foo", [1, 2])
assert md.getlist("foo") == [1, 2]
class _ImmutableDictTests(object):
storage_class = None
def test_follows_dict_interface(self):
cls = self.storage_class
data = {"foo": 1, "bar": 2, "baz": 3}
d = cls(data)
assert d["foo"] == 1
assert d["bar"] == 2
assert d["baz"] == 3
assert sorted(d.keys()) == ["bar", "baz", "foo"]
assert "foo" in d
assert "foox" not in d
assert len(d) == 3
def test_copies_are_mutable(self):
cls = self.storage_class
immutable = cls({"a": 1})
with pytest.raises(TypeError):
immutable.pop("a")
mutable = immutable.copy()
mutable.pop("a")
assert "a" in immutable
assert mutable is not immutable
assert copy(immutable) is immutable
def test_dict_is_hashable(self):
cls = self.storage_class
immutable = cls({"a": 1, "b": 2})
immutable2 = cls({"a": 2, "b": 2})
x = set([immutable])
assert immutable in x
assert immutable2 not in x
x.discard(immutable)
assert immutable not in x
assert immutable2 not in x
x.add(immutable2)
assert immutable not in x
assert immutable2 in x
x.add(immutable)
assert immutable in x
assert immutable2 in x
class TestImmutableTypeConversionDict(_ImmutableDictTests):
storage_class = datastructures.ImmutableTypeConversionDict
class TestImmutableMultiDict(_ImmutableDictTests):
storage_class = datastructures.ImmutableMultiDict
def test_multidict_is_hashable(self):
cls = self.storage_class
immutable = cls({"a": [1, 2], "b": 2})
immutable2 = cls({"a": [1], "b": 2})
x = set([immutable])
assert immutable in x
assert immutable2 not in x
x.discard(immutable)
assert immutable not in x
assert immutable2 not in x
x.add(immutable2)
assert immutable not in x
assert immutable2 in x
x.add(immutable)
assert immutable in x
assert immutable2 in x
class TestImmutableDict(_ImmutableDictTests):
storage_class = datastructures.ImmutableDict
class TestImmutableOrderedMultiDict(_ImmutableDictTests):
storage_class = datastructures.ImmutableOrderedMultiDict
def test_ordered_multidict_is_hashable(self):
a = self.storage_class([("a", 1), ("b", 1), ("a", 2)])
b = self.storage_class([("a", 1), ("a", 2), ("b", 1)])
assert hash(a) != hash(b)
class TestMultiDict(_MutableMultiDictTests):
storage_class = datastructures.MultiDict
def test_multidict_pop(self):
def make_d():
return self.storage_class({"foo": [1, 2, 3, 4]})
d = make_d()
assert d.pop("foo") == 1
assert not d
d = make_d()
assert d.pop("foo", 32) == 1
assert not d
d = make_d()
assert d.pop("foos", 32) == 32
assert d
with pytest.raises(KeyError):
d.pop("foos")
def test_multidict_pop_raise_badrequestkeyerror_for_empty_list_value(self):
mapping = [("a", "b"), ("a", "c")]
md = self.storage_class(mapping)
md.setlistdefault("empty", [])
with pytest.raises(KeyError):
md.pop("empty")
def test_multidict_popitem_raise_badrequestkeyerror_for_empty_list_value(self):
mapping = []
md = self.storage_class(mapping)
md.setlistdefault("empty", [])
with pytest.raises(BadRequestKeyError):
md.popitem()
def test_setlistdefault(self):
md = self.storage_class()
assert md.setlistdefault("u", [-1, -2]) == [-1, -2]
assert md.getlist("u") == [-1, -2]
assert md["u"] == -1
def test_iter_interfaces(self):
mapping = [
("a", 1),
("b", 2),
("a", 2),
("d", 3),
("a", 1),
("a", 3),
("d", 4),
("c", 3),
]
md = self.storage_class(mapping)
assert list(zip(md.keys(), md.listvalues())) == list(md.lists())
assert list(zip(md, iterlistvalues(md))) == list(iterlists(md))
assert list(zip(iterkeys(md), iterlistvalues(md))) == list(iterlists(md))
@pytest.mark.skipif(not PY2, reason="viewmethods work only for the 2-nd version.")
def test_view_methods(self):
mapping = [("a", "b"), ("a", "c")]
md = self.storage_class(mapping)
vi = md.viewitems() # noqa: B302
vk = md.viewkeys() # noqa: B302
vv = md.viewvalues() # noqa: B302
assert list(vi) == list(md.items())
assert list(vk) == list(md.keys())
assert list(vv) == list(md.values())
md["k"] = "n"
assert list(vi) == list(md.items())
assert list(vk) == list(md.keys())
assert list(vv) == list(md.values())
@pytest.mark.skipif(not PY2, reason="viewmethods work only for the 2-nd version.")
def test_viewitems_with_multi(self):
mapping = [("a", "b"), ("a", "c")]
md = self.storage_class(mapping)
vi = md.viewitems(multi=True) # noqa: B302
assert list(vi) == list(md.items(multi=True))
md["k"] = "n"
assert list(vi) == list(md.items(multi=True))
def test_getitem_raise_badrequestkeyerror_for_empty_list_value(self):
mapping = [("a", "b"), ("a", "c")]
md = self.storage_class(mapping)
md.setlistdefault("empty", [])
with pytest.raises(KeyError):
md["empty"]
class TestOrderedMultiDict(_MutableMultiDictTests):
storage_class = datastructures.OrderedMultiDict
def test_ordered_interface(self):
cls = self.storage_class
d = cls()
assert not d
d.add("foo", "bar")
assert len(d) == 1
d.add("foo", "baz")
assert len(d) == 1
assert list(iteritems(d)) == [("foo", "bar")]
assert list(d) == ["foo"]
assert list(iteritems(d, multi=True)) == [("foo", "bar"), ("foo", "baz")]
del d["foo"]
assert not d
assert len(d) == 0
assert list(d) == []
d.update([("foo", 1), ("foo", 2), ("bar", 42)])
d.add("foo", 3)
assert d.getlist("foo") == [1, 2, 3]
assert d.getlist("bar") == [42]
assert list(iteritems(d)) == [("foo", 1), ("bar", 42)]
expected = ["foo", "bar"]
assert list(d.keys()) == expected
assert list(d) == expected
assert list(iterkeys(d)) == expected
assert list(iteritems(d, multi=True)) == [
("foo", 1),
("foo", 2),
("bar", 42),
("foo", 3),
]
assert len(d) == 2
assert d.pop("foo") == 1
assert d.pop("blafasel", None) is None
assert d.pop("blafasel", 42) == 42
assert len(d) == 1
assert d.poplist("bar") == [42]
assert not d
d.get("missingkey") is None
d.add("foo", 42)
d.add("foo", 23)
d.add("bar", 2)
d.add("foo", 42)
assert d == datastructures.MultiDict(d)
id = self.storage_class(d)
assert d == id
d.add("foo", 2)
assert d != id
d.update({"blah": [1, 2, 3]})
assert d["blah"] == 1
assert d.getlist("blah") == [1, 2, 3]
# setlist works
d = self.storage_class()
d["foo"] = 42
d.setlist("foo", [1, 2])
assert d.getlist("foo") == [1, 2]
with pytest.raises(BadRequestKeyError):
d.pop("missing")
with pytest.raises(BadRequestKeyError):
d["missing"]
# popping
d = self.storage_class()
d.add("foo", 23)
d.add("foo", 42)
d.add("foo", 1)
assert d.popitem() == ("foo", 23)
with pytest.raises(BadRequestKeyError):
d.popitem()
assert not d
d.add("foo", 23)
d.add("foo", 42)
d.add("foo", 1)
assert d.popitemlist() == ("foo", [23, 42, 1])
with pytest.raises(BadRequestKeyError):
d.popitemlist()
# Unhashable
d = self.storage_class()
d.add("foo", 23)
pytest.raises(TypeError, hash, d)
def test_iterables(self):
a = datastructures.MultiDict((("key_a", "value_a"),))
b = datastructures.MultiDict((("key_b", "value_b"),))
ab = datastructures.CombinedMultiDict((a, b))
assert sorted(ab.lists()) == [("key_a", ["value_a"]), ("key_b", ["value_b"])]
assert sorted(ab.listvalues()) == [["value_a"], ["value_b"]]
assert sorted(ab.keys()) == ["key_a", "key_b"]
assert sorted(iterlists(ab)) == [("key_a", ["value_a"]), ("key_b", ["value_b"])]
assert sorted(iterlistvalues(ab)) == [["value_a"], ["value_b"]]
assert sorted(iterkeys(ab)) == ["key_a", "key_b"]
def test_get_description(self):
data = datastructures.OrderedMultiDict()
with pytest.raises(BadRequestKeyError) as exc_info:
data["baz"]
assert "baz" not in exc_info.value.get_description()
exc_info.value.show_exception = True
assert "baz" in exc_info.value.get_description()
with pytest.raises(BadRequestKeyError) as exc_info:
data.pop("baz")
exc_info.value.show_exception = True
assert "baz" in exc_info.value.get_description()
exc_info.value.args = ()
assert "baz" not in exc_info.value.get_description()
class TestTypeConversionDict(object):
storage_class = datastructures.TypeConversionDict
def test_value_conversion(self):
d = self.storage_class(foo="1")
assert d.get("foo", type=int) == 1
def test_return_default_when_conversion_is_not_possible(self):
d = self.storage_class(foo="bar")
assert d.get("foo", default=-1, type=int) == -1
def test_propagate_exceptions_in_conversion(self):
d = self.storage_class(foo="bar")
switch = {"a": 1}
with pytest.raises(KeyError):
d.get("foo", type=lambda x: switch[x])
class TestCombinedMultiDict(object):
storage_class = datastructures.CombinedMultiDict
def test_basic_interface(self):
d1 = datastructures.MultiDict([("foo", "1")])
d2 = datastructures.MultiDict([("bar", "2"), ("bar", "3")])
d = self.storage_class([d1, d2])
# lookup
assert d["foo"] == "1"
assert d["bar"] == "2"
assert d.getlist("bar") == ["2", "3"]
assert sorted(d.items()) == [("bar", "2"), ("foo", "1")]
assert sorted(d.items(multi=True)) == [("bar", "2"), ("bar", "3"), ("foo", "1")]
assert "missingkey" not in d
assert "foo" in d
# type lookup
assert d.get("foo", type=int) == 1
assert d.getlist("bar", type=int) == [2, 3]
# get key errors for missing stuff
with pytest.raises(KeyError):
d["missing"]
# make sure that they are immutable
with pytest.raises(TypeError):
d["foo"] = "blub"
# copies are mutable
d = d.copy()
d["foo"] = "blub"
# make sure lists merges
md1 = datastructures.MultiDict((("foo", "bar"),))
md2 = datastructures.MultiDict((("foo", "blafasel"),))
x = self.storage_class((md1, md2))
assert list(iterlists(x)) == [("foo", ["bar", "blafasel"])]
def test_length(self):
d1 = datastructures.MultiDict([("foo", "1")])
d2 = datastructures.MultiDict([("bar", "2")])
assert len(d1) == len(d2) == 1
d = self.storage_class([d1, d2])
assert len(d) == 2
d1.clear()
assert len(d1) == 0
assert len(d) == 1
class TestHeaders(object):
storage_class = datastructures.Headers
def test_basic_interface(self):
headers = self.storage_class()
headers.add("Content-Type", "text/plain")
headers.add("X-Foo", "bar")
assert "x-Foo" in headers
assert "Content-type" in headers
headers["Content-Type"] = "foo/bar"
assert headers["Content-Type"] == "foo/bar"
assert len(headers.getlist("Content-Type")) == 1
# list conversion
assert headers.to_wsgi_list() == [("Content-Type", "foo/bar"), ("X-Foo", "bar")]
assert str(headers) == "Content-Type: foo/bar\r\nX-Foo: bar\r\n\r\n"
assert str(self.storage_class()) == "\r\n"
# extended add
headers.add("Content-Disposition", "attachment", filename="foo")
assert headers["Content-Disposition"] == "attachment; filename=foo"
headers.add("x", "y", z='"')
assert headers["x"] == r'y; z="\""'
def test_defaults_and_conversion(self):
# defaults
headers = self.storage_class(
[
("Content-Type", "text/plain"),
("X-Foo", "bar"),
("X-Bar", "1"),
("X-Bar", "2"),
]
)
assert headers.getlist("x-bar") == ["1", "2"]
assert headers.get("x-Bar") == "1"
assert headers.get("Content-Type") == "text/plain"
assert headers.setdefault("X-Foo", "nope") == "bar"
assert headers.setdefault("X-Bar", "nope") == "1"
assert headers.setdefault("X-Baz", "quux") == "quux"
assert headers.setdefault("X-Baz", "nope") == "quux"
headers.pop("X-Baz")
# type conversion
assert headers.get("x-bar", type=int) == 1
assert headers.getlist("x-bar", type=int) == [1, 2]
# list like operations
assert headers[0] == ("Content-Type", "text/plain")
assert headers[:1] == self.storage_class([("Content-Type", "text/plain")])
del headers[:2]
del headers[-1]
assert headers == self.storage_class([("X-Bar", "1")])
def test_copying(self):
a = self.storage_class([("foo", "bar")])
b = a.copy()
a.add("foo", "baz")
assert a.getlist("foo") == ["bar", "baz"]
assert b.getlist("foo") == ["bar"]
def test_popping(self):
headers = self.storage_class([("a", 1)])
assert headers.pop("a") == 1
assert headers.pop("b", 2) == 2
with pytest.raises(KeyError):
headers.pop("c")
def test_set_arguments(self):
a = self.storage_class()
a.set("Content-Disposition", "useless")
a.set("Content-Disposition", "attachment", filename="foo")
assert a["Content-Disposition"] == "attachment; filename=foo"
def test_reject_newlines(self):
h = self.storage_class()
for variation in "foo\nbar", "foo\r\nbar", "foo\rbar":
with pytest.raises(ValueError):
h["foo"] = variation
with pytest.raises(ValueError):
h.add("foo", variation)
with pytest.raises(ValueError):
h.add("foo", "test", option=variation)
with pytest.raises(ValueError):
h.set("foo", variation)
with pytest.raises(ValueError):
h.set("foo", "test", option=variation)
def test_slicing(self):
# there's nothing wrong with these being native strings
# Headers doesn't care about the data types
h = self.storage_class()
h.set("X-Foo-Poo", "bleh")
h.set("Content-Type", "application/whocares")
h.set("X-Forwarded-For", "192.168.0.123")
h[:] = [(k, v) for k, v in h if k.startswith(u"X-")]
assert list(h) == [("X-Foo-Poo", "bleh"), ("X-Forwarded-For", "192.168.0.123")]
def test_bytes_operations(self):
h = self.storage_class()
h.set("X-Foo-Poo", "bleh")
h.set("X-Whoops", b"\xff")
h.set(b"X-Bytes", b"something")
assert h.get("x-foo-poo", as_bytes=True) == b"bleh"
assert h.get("x-whoops", as_bytes=True) == b"\xff"
assert h.get("x-bytes") == "something"
def test_to_wsgi_list(self):
h = self.storage_class()
h.set(u"Key", u"Value")
for key, value in h.to_wsgi_list():
if PY2:
strict_eq(key, b"Key")
strict_eq(value, b"Value")
else:
strict_eq(key, u"Key")
strict_eq(value, u"Value")
def test_to_wsgi_list_bytes(self):
h = self.storage_class()
h.set(b"Key", b"Value")
for key, value in h.to_wsgi_list():
if PY2:
strict_eq(key, b"Key")
strict_eq(value, b"Value")
else:
strict_eq(key, u"Key")
strict_eq(value, u"Value")
def test_equality(self):
# test equality, given keys are case insensitive
h1 = self.storage_class()
h1.add("X-Foo", "foo")
h1.add("X-Bar", "bah")
h1.add("X-Bar", "humbug")
h2 = self.storage_class()
h2.add("x-foo", "foo")
h2.add("x-bar", "bah")
h2.add("x-bar", "humbug")
assert h1 == h2
class TestEnvironHeaders(object):
storage_class = datastructures.EnvironHeaders
def test_basic_interface(self):
# this happens in multiple WSGI servers because they
# use a vary naive way to convert the headers;
broken_env = {
"HTTP_CONTENT_TYPE": "text/html",
"CONTENT_TYPE": "text/html",
"HTTP_CONTENT_LENGTH": "0",
"CONTENT_LENGTH": "0",
"HTTP_ACCEPT": "*",
"wsgi.version": (1, 0),
}
headers = self.storage_class(broken_env)
assert headers
assert len(headers) == 3
assert sorted(headers) == [
("Accept", "*"),
("Content-Length", "0"),
("Content-Type", "text/html"),
]
assert not self.storage_class({"wsgi.version": (1, 0)})
assert len(self.storage_class({"wsgi.version": (1, 0)})) == 0
assert 42 not in headers
def test_skip_empty_special_vars(self):
env = {"HTTP_X_FOO": "42", "CONTENT_TYPE": "", "CONTENT_LENGTH": ""}
headers = self.storage_class(env)
assert dict(headers) == {"X-Foo": "42"}
env = {"HTTP_X_FOO": "42", "CONTENT_TYPE": "", "CONTENT_LENGTH": "0"}
headers = self.storage_class(env)
assert dict(headers) == {"X-Foo": "42", "Content-Length": "0"}
def test_return_type_is_unicode(self):
# environ contains native strings; we return unicode
headers = self.storage_class(
{"HTTP_FOO": "\xe2\x9c\x93", "CONTENT_TYPE": "text/plain"}
)
assert headers["Foo"] == u"\xe2\x9c\x93"
assert isinstance(headers["Foo"], text_type)
assert isinstance(headers["Content-Type"], text_type)
iter_output = dict(iter(headers))
assert iter_output["Foo"] == u"\xe2\x9c\x93"
assert isinstance(iter_output["Foo"], text_type)
assert isinstance(iter_output["Content-Type"], text_type)
def test_bytes_operations(self):
foo_val = "\xff"
h = self.storage_class({"HTTP_X_FOO": foo_val})
assert h.get("x-foo", as_bytes=True) == b"\xff"
assert h.get("x-foo") == u"\xff"
class TestHeaderSet(object):
storage_class = datastructures.HeaderSet
def test_basic_interface(self):
hs = self.storage_class()
hs.add("foo")
hs.add("bar")
assert "Bar" in hs
assert hs.find("foo") == 0
assert hs.find("BAR") == 1
assert hs.find("baz") < 0
hs.discard("missing")
hs.discard("foo")
assert hs.find("foo") < 0
assert hs.find("bar") == 0
with pytest.raises(IndexError):
hs.index("missing")
assert hs.index("bar") == 0
assert hs
hs.clear()
assert not hs
class TestImmutableList(object):
storage_class = datastructures.ImmutableList
def test_list_hashable(self):
data = (1, 2, 3, 4)
store = self.storage_class(data)
assert hash(data) == hash(store)
assert data != store
def make_call_asserter(func=None):
"""Utility to assert a certain number of function calls.
:param func: Additional callback for each function call.
>>> assert_calls, func = make_call_asserter()
>>> with assert_calls(2):
... func()
... func()
"""
calls = [0]
@contextmanager
def asserter(count, msg=None):
calls[0] = 0
yield
assert calls[0] == count
def wrapped(*args, **kwargs):
calls[0] += 1
if func is not None:
return func(*args, **kwargs)
return asserter, wrapped
class TestCallbackDict(object):
storage_class = datastructures.CallbackDict
def test_callback_dict_reads(self):
assert_calls, func = make_call_asserter()
initial = {"a": "foo", "b": "bar"}
dct = self.storage_class(initial=initial, on_update=func)
with assert_calls(0, "callback triggered by read-only method"):
# read-only methods
dct["a"]
dct.get("a")
pytest.raises(KeyError, lambda: dct["x"])
"a" in dct
list(iter(dct))
dct.copy()
with assert_calls(0, "callback triggered without modification"):
# methods that may write but don't
dct.pop("z", None)
dct.setdefault("a")
def test_callback_dict_writes(self):
assert_calls, func = make_call_asserter()
initial = {"a": "foo", "b": "bar"}
dct = self.storage_class(initial=initial, on_update=func)
with assert_calls(8, "callback not triggered by write method"):
# always-write methods
dct["z"] = 123
dct["z"] = 123 # must trigger again
del dct["z"]
dct.pop("b", None)
dct.setdefault("x")
dct.popitem()
dct.update([])
dct.clear()
with assert_calls(0, "callback triggered by failed del"):
pytest.raises(KeyError, lambda: dct.__delitem__("x"))
with assert_calls(0, "callback triggered by failed pop"):
pytest.raises(KeyError, lambda: dct.pop("x"))
class TestCacheControl(object):
def test_repr(self):
cc = datastructures.RequestCacheControl([("max-age", "0"), ("private", "True")])
assert repr(cc) == "<RequestCacheControl max-age='0' private='True'>"
class TestAccept(object):
storage_class = datastructures.Accept
def test_accept_basic(self):
accept = self.storage_class(
[("tinker", 0), ("tailor", 0.333), ("soldier", 0.667), ("sailor", 1)]
)
# check __getitem__ on indices
assert accept[3] == ("tinker", 0)
assert accept[2] == ("tailor", 0.333)
assert accept[1] == ("soldier", 0.667)
assert accept[0], ("sailor", 1)
# check __getitem__ on string
assert accept["tinker"] == 0
assert accept["tailor"] == 0.333
assert accept["soldier"] == 0.667
assert accept["sailor"] == 1
assert accept["spy"] == 0
# check quality method