File: test_headers.py

package info (click to toggle)
python-falcon 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 33,608; javascript: 92; sh: 50; makefile: 50
file content (1063 lines) | stat: -rw-r--r-- 37,505 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
from collections import defaultdict
from datetime import datetime

import pytest

import falcon
from falcon import testing
from falcon.util.deprecation import DeprecatedWarning
from falcon.util.misc import _utcnow

SAMPLE_BODY = testing.rand_string(0, 128 * 1024)


@pytest.fixture
def client(asgi, util):
    app = util.create_app(asgi)
    return testing.TestClient(app)


class XmlResource:
    def __init__(self, content_type):
        self.content_type = content_type

    def on_get(self, req, resp):
        resp.set_header('content-type', self.content_type)


class HeaderHelpersResource:
    def __init__(self, last_modified=None):
        if last_modified is not None:
            self.last_modified = last_modified
        else:
            self.last_modified = _utcnow()

    def _overwrite_headers(self, req, resp):
        resp.content_type = 'x-falcon/peregrine'
        resp.cache_control = ['no-store']

    def on_get(self, req, resp):
        resp.text = '{}'
        resp.content_type = 'x-falcon/peregrine'
        resp.cache_control = [
            'public',
            'private',
            'no-cache',
            'no-store',
            'must-revalidate',
            'proxy-revalidate',
            'max-age=3600',
            's-maxage=60',
            'no-transform',
        ]

        resp.etag = None  # Header not set yet, so should be a noop
        resp.etag = 'fa0d1a60ef6616bb28038515c8ea4cb2'
        resp.last_modified = self.last_modified
        resp.retry_after = 3601

        # Relative URI's are OK per
        # https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.2
        resp.location = '/things/87'
        resp.content_location = '/things/78'

        resp.downloadable_as = None  # Header not set yet, so should be a noop
        resp.downloadable_as = 'Some File.zip'

        if req.range_unit is None or req.range_unit == 'bytes':
            # bytes 0-499/10240
            resp.content_range = (0, 499, 10 * 1024)
        else:
            resp.content_range = (0, 25, 100, req.range_unit)

        resp.accept_ranges = None  # Header not set yet, so should be a noop
        resp.accept_ranges = 'bytes'

        # Test the removal of custom headers
        resp.set_header('X-Client-Should-Never-See-This', 'abc')
        assert resp.get_header('x-client-should-never-see-this') == 'abc'
        resp.delete_header('x-client-should-never-see-this')

        self.req = req
        self.resp = resp

    def on_head(self, req, resp):
        resp.set_header('Content-Type', 'x-swallow/unladen')
        resp.set_header('X-Auth-Token', 'setecastronomy')
        resp.set_header('X-AUTH-TOKEN', 'toomanysecrets')

        resp.location = '/things/87'
        del resp.location

        self._overwrite_headers(req, resp)

        self.resp = resp

    def on_post(self, req, resp):
        resp.set_headers(
            [
                ('CONTENT-TYPE', 'x-swallow/unladen'),
                ('X-Auth-Token', 'setecastronomy'),
                ('X-AUTH-TOKEN', 'toomanysecrets'),
            ]
        )

        self._overwrite_headers(req, resp)

        self.resp = resp

    def on_put(self, req, resp):
        resp.set_headers(
            {'CONTENT-TYPE': 'x-swallow/unladen', 'X-aUTH-tOKEN': 'toomanysecrets'}
        )

        self._overwrite_headers(req, resp)

        self.resp = resp


class LocationHeaderUnicodeResource:
    URL1 = '/\u00e7runchy/bacon'
    URL2 = 'ab\u00e7'

    def on_get(self, req, resp):
        resp.location = self.URL1
        resp.content_location = self.URL2

    def on_head(self, req, resp):
        resp.location = self.URL2
        resp.content_location = self.URL1


class UnicodeHeaderResource:
    def on_connect(self, req, resp):
        # A way to CONNECT with people.
        resp.set_header('X-Clinking-Beer-Mugs', 'đŸș')

    def on_get(self, req, resp):
        resp.set_headers(
            [
                ('X-auTH-toKEN', 'toomanysecrets'),
                ('Content-TYpE', 'application/json'),
                ('X-symbOl', '@'),
            ]
        )

    def on_patch(self, req, resp):
        resp.set_headers(
            [
                ('X-Thing', '\x01\x02\xff'),
            ]
        )

    def on_post(self, req, resp):
        resp.set_headers(
            [
                ('X-symb\u00f6l', 'thing'),
            ]
        )

    def on_put(self, req, resp):
        resp.set_headers(
            [
                ('X-Thing', '\u00ff'),
            ]
        )


class VaryHeaderResource:
    def __init__(self, vary):
        self.vary = vary

    def on_get(self, req, resp):
        resp.text = '{}'
        resp.vary = self.vary


class LinkHeaderResource:
    def __init__(self):
        self._links = []

    def add_link(self, *args, **kwargs):
        self._links.append(('add_link', args, kwargs))

    def append_link(self, *args, **kwargs):
        self._links.append(('append_link', args, kwargs))

    def on_get(self, req, resp):
        resp.text = '{}'

        for method_name, args, kwargs in self._links:
            append_method = getattr(resp, method_name)
            if method_name == 'append_link':
                append_method(*args, **kwargs)
            else:
                with pytest.warns(
                    DeprecatedWarning,
                    match='Call to deprecated function add_link(...)',
                ):
                    append_method(*args, **kwargs)


class AppendHeaderResource:
    def on_get(self, req, resp):
        resp.append_header('X-Things', 'thing-1')
        resp.append_header('X-THINGS', 'thing-2')
        resp.append_header('x-thiNgs', 'thing-3')

    def on_head(self, req, resp):
        resp.set_header('X-things', 'thing-1')
        resp.append_header('X-THINGS', 'thing-2')
        resp.append_header('x-thiNgs', 'thing-3')

    def on_post(self, req, resp):
        resp.append_header('X-Things', 'thing-1')

        c1 = (
            'ut_existing_user=1; expires=Mon, 14-Jan-2019 21:20:08 GMT;'
            ' Max-Age=600; path=/'
        )
        resp.append_header('Set-Cookie', c1)
        c2 = (
            'partner_source=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT;'
            ' Max-Age=0'
        )
        resp.append_header('seT-cookie', c2)


class RemoveHeaderResource:
    def __init__(self, with_double_quotes):
        self.with_double_quotes = with_double_quotes

    def on_get(self, req, resp):
        etag = 'fa0d1a60ef6616bb28038515c8ea4cb2'
        if self.with_double_quotes:
            etag = '"' + etag + '"'

        resp.etag = etag
        assert resp.etag == '"fa0d1a60ef6616bb28038515c8ea4cb2"'
        resp.etag = None

        resp.downloadable_as = 'foo.zip'
        assert resp.downloadable_as == 'attachment; filename="foo.zip"'
        resp.downloadable_as = None


class DownloadableResource:
    def __init__(self, filename):
        self.filename = filename

    def on_get(self, req, resp):
        resp.text = 'Hello, World!\n'
        resp.content_type = falcon.MEDIA_TEXT
        resp.downloadable_as = self.filename


class ViewableResource:
    def __init__(self, filename):
        self.filename = filename

    def on_get(self, req, resp):
        resp.text = 'Hello, World!\n'
        resp.content_type = falcon.MEDIA_TEXT
        resp.viewable_as = self.filename


class ContentLengthHeaderResource:
    def __init__(self, content_length, body=None, data=None):
        self._content_length = content_length
        self._body = body
        self._data = data

    def on_get(self, req, resp):
        resp.content_length = self._content_length

        if self._body:
            resp.text = self._body

        if self._data:
            resp.data = self._data

    def on_head(self, req, resp):
        resp.content_length = self._content_length


class ExpiresHeaderResource:
    def __init__(self, expires):
        self._expires = expires

    def on_get(self, req, resp):
        resp.expires = self._expires


class CustomHeaders:
    def items(self):
        return [('test-header', 'test-value')]


class CustomHeadersNotCallable:
    def __init__(self):
        self.items = {'test-header': 'test-value'}


class CustomHeadersResource:
    def on_get(self, req, resp):
        headers = CustomHeaders()
        resp.set_headers(headers)

    def on_post(self, req, resp):
        resp.set_headers(CustomHeadersNotCallable())


class HeadersDebugResource:
    def on_get(self, req, resp):
        resp.media = {
            'raw': req.headers,
            'lower': req.headers_lower,
        }

    def on_get_header(self, req, resp, header):
        resp.media = {header.lower(): req.get_header(header)}


class TestHeaders:
    def test_content_length(self, client):
        resource = testing.SimpleTestResource(body=SAMPLE_BODY)
        client.app.add_route('/', resource)
        result = client.simulate_get()

        content_length = str(len(SAMPLE_BODY))
        assert result.headers['Content-Length'] == content_length

    def test_declared_content_length_on_head(self, client):
        client.app.add_route('/', ContentLengthHeaderResource(42))
        result = client.simulate_head()
        assert result.headers['Content-Length'] == '42'

    def test_declared_content_length_overridden_by_no_body(self, client):
        client.app.add_route('/', ContentLengthHeaderResource(42))
        result = client.simulate_get()
        assert result.headers['Content-Length'] == '0'

    def test_declared_content_length_overriden_by_body_length(self, client):
        resource = ContentLengthHeaderResource(42, body=SAMPLE_BODY)
        client.app.add_route('/', resource)
        result = client.simulate_get()

        assert result.headers['Content-Length'] == str(len(SAMPLE_BODY))

    def test_declared_content_length_overriden_by_data_length(self, client):
        data = SAMPLE_BODY.encode()

        resource = ContentLengthHeaderResource(42, data=data)
        client.app.add_route('/', resource)
        result = client.simulate_get()

        assert result.headers['Content-Length'] == str(len(data))

    def test_expires_header(self, client):
        expires = datetime(2013, 1, 1, 10, 30, 30)
        client.app.add_route('/', ExpiresHeaderResource(expires))
        result = client.simulate_get()

        assert result.headers['Expires'] == 'Tue, 01 Jan 2013 10:30:30 GMT'

    def test_get_header_as_int(self, client):
        resource = testing.SimpleTestResource(body=SAMPLE_BODY)
        client.app.add_route('/', resource)
        request_headers = {
            'X-Int-Val': '42',
            'X-Str-Val': 'test-val',
            'X-Float-Val': '3.14',
        }
        client.simulate_get(headers=request_headers)

        req = resource.captured_req
        value = req.get_header_as_int('X-Int-Val')
        assert value == 42

        value = req.get_header_as_int('X-Not-Found')
        assert value is None

        with pytest.raises(falcon.HTTPInvalidHeader) as exc_info:
            req.get_header_as_int('X-Float-Val')
        assert exc_info.value.args[0] == 'The value of the header must be an integer.'

        with pytest.raises(falcon.HTTPInvalidHeader) as exc_info:
            req.get_header_as_int('X-Str-Val')
        assert exc_info.value.args[0] == 'The value of the header must be an integer.'

        with pytest.raises(falcon.HTTPMissingHeader) as exc_info:
            req.get_header_as_int('X-Not-Found', required=True)

    def test_default_value(self, client):
        resource = testing.SimpleTestResource(body=SAMPLE_BODY)
        client.app.add_route('/', resource)
        client.simulate_get()

        req = resource.captured_req
        value = req.get_header('X-Not-Found') or '876'
        assert value == '876'

        value = req.get_header('X-Not-Found', default='some-value')
        assert value == 'some-value'

        # Exercise any result caching and associated abuse mitigations
        for i in range(10000):
            assert req.get_header('X-Not-Found-{0}'.format(i)) is None

    @pytest.mark.parametrize('with_double_quotes', [True, False])
    def test_unset_header(self, client, with_double_quotes):
        client.app.add_route('/', RemoveHeaderResource(with_double_quotes))
        result = client.simulate_get()

        assert 'Etag' not in result.headers
        assert 'Content-Disposition' not in result.headers

    def test_required_header(self, client):
        resource = testing.SimpleTestResource(body=SAMPLE_BODY)
        client.app.add_route('/', resource)
        client.simulate_get()

        try:
            req = resource.captured_req
            req.get_header('X-Not-Found', required=True)
            pytest.fail('falcon.HTTPMissingHeader not raised')
        except falcon.HTTPMissingHeader as ex:
            assert isinstance(ex, falcon.HTTPBadRequest)
            assert ex.title == 'Missing header value'
            expected_desc = 'The "X-Not-Found" header is required.'
            assert ex.description == expected_desc

    @pytest.mark.parametrize('status', (falcon.HTTP_204, falcon.HTTP_304))
    def test_no_content_length(self, client, status):
        client.app.add_route('/xxx', testing.SimpleTestResource(status=status))

        result = client.simulate_get('/xxx')
        assert 'Content-Length' not in result.headers
        assert not result.content

    def test_content_header_missing(self, client):
        environ = testing.create_environ()
        req = falcon.Request(environ)
        for header in ('Content-Type', 'Content-Length'):
            assert req.get_header(header) is None

    def test_passthrough_request_headers(self, client):
        resource = testing.SimpleTestResource(body=SAMPLE_BODY)
        client.app.add_route('/', resource)
        request_headers = {
            'X-Auth-Token': 'Setec Astronomy',
            'Content-Type': 'text/plain; charset=utf-8',
        }
        client.simulate_get(headers=request_headers)

        for name, expected_value in request_headers.items():
            actual_value = resource.captured_req.get_header(name)
            assert actual_value == expected_value

        client.simulate_get(headers=resource.captured_req.headers)

        # Validate that the property has been cached
        assert resource.captured_req.headers is resource.captured_req.headers

        # Compare the request HTTP headers with the original headers
        for name, expected_value in request_headers.items():
            actual_value = resource.captured_req.get_header(name)
            assert actual_value == expected_value

    def test_headers_as_list(self, client):
        headers = [
            ('Client-ID', '692ba466-74bb-11e3-bf3f-7567c531c7ca'),
            ('Accept', 'audio/*; q=0.2, audio/basic'),
        ]

        # Unit test
        environ = testing.create_environ(headers=headers)
        req = falcon.Request(environ)

        for name, value in headers:
            assert (name.upper(), value) in req.headers.items()
            assert (name.lower(), value) in req.headers_lower.items()

        # Functional test
        client.app.add_route('/', testing.SimpleTestResource(headers=headers))
        result = client.simulate_get()

        for name, value in headers:
            assert result.headers[name] == value

    def test_default_media_type(self, client):
        resource = testing.SimpleTestResource(body='Hello world!')
        self._check_header(client, resource, 'Content-Type', falcon.DEFAULT_MEDIA_TYPE)

    @pytest.mark.parametrize('asgi', [True, False])
    @pytest.mark.parametrize(
        'content_type,body',
        [
            ('text/plain; charset=UTF-8', 'Hello Unicode! \U0001f638'),
            # NOTE(kgriffs): This only works because the client defaults to
            # ISO-8859-1 IFF the media type is 'text'.
            ('text/plain', 'Hello ISO-8859-1!'),
        ],
    )
    def test_override_default_media_type(self, asgi, util, client, content_type, body):
        client.app = util.create_app(asgi=asgi, media_type=content_type)
        client.app.add_route('/', testing.SimpleTestResource(body=body))
        result = client.simulate_get()

        assert result.text == body
        assert result.headers['Content-Type'] == content_type

    @pytest.mark.parametrize('asgi', [True, False])
    def test_override_default_media_type_missing_encoding(self, asgi, util, client):
        body = '{"msg": "Hello Unicode! \U0001f638"}'

        client.app = util.create_app(asgi=asgi, media_type='application/json')
        client.app.add_route('/', testing.SimpleTestResource(body=body))
        result = client.simulate_get()

        assert result.content == body.encode('utf-8')
        assert isinstance(result.text, str)
        assert result.text == body
        assert result.json == {'msg': 'Hello Unicode! \U0001f638'}

    def test_response_header_helpers_on_get(self, client):
        last_modified = datetime(2013, 1, 1, 10, 30, 30)
        resource = HeaderHelpersResource(last_modified)
        client.app.add_route('/', resource)
        result = client.simulate_get()

        resp = resource.resp

        content_type = 'x-falcon/peregrine'
        assert resp.content_type == content_type
        assert result.headers['Content-Type'] == content_type
        assert (
            result.headers['Content-Disposition']
            == 'attachment; filename="Some File.zip"'
        )

        cache_control = (
            'public, private, no-cache, no-store, '
            'must-revalidate, proxy-revalidate, max-age=3600, '
            's-maxage=60, no-transform'
        )

        assert resp.cache_control == cache_control
        assert result.headers['Cache-Control'] == cache_control

        etag = '"fa0d1a60ef6616bb28038515c8ea4cb2"'
        assert resp.etag == etag
        assert result.headers['Etag'] == etag

        lm_date = 'Tue, 01 Jan 2013 10:30:30 GMT'
        assert resp.last_modified == lm_date
        assert result.headers['Last-Modified'] == lm_date

        assert resp.retry_after == '3601'
        assert result.headers['Retry-After'] == '3601'

        assert resp.location == '/things/87'
        assert result.headers['Location'] == '/things/87'

        assert resp.content_location == '/things/78'
        assert result.headers['Content-Location'] == '/things/78'

        content_range = 'bytes 0-499/10240'
        assert resp.content_range == content_range
        assert result.headers['Content-Range'] == content_range

        resp.content_range = (1, 499, 10 * 1024, 'bytes')
        assert isinstance(resp.content_range, str)
        assert resp.content_range == 'bytes 1-499/10240'

        assert resp.accept_ranges == 'bytes'
        assert result.headers['Accept-Ranges'] == 'bytes'

        req_headers = {'Range': 'items=0-25'}
        result = client.simulate_get(headers=req_headers)
        assert result.headers['Content-Range'] == 'items 0-25/100'

        # Check for duplicate headers
        hist = defaultdict(lambda: 0)
        for name, value in result.headers.items():
            hist[name] += 1
            assert 1 == hist[name]

    @pytest.mark.parametrize(
        'filename,expected',
        [
            ('report.csv', 'attachment; filename="report.csv"'),
            ('Hello World.txt', 'attachment; filename="Hello World.txt"'),
            (
                'Bold Digit 𝟏.txt',
                'attachment; filename=Bold_Digit_1.txt; '
                "filename*=UTF-8''Bold%20Digit%20%F0%9D%9F%8F.txt",
            ),
            (
                'Ångström unit.txt',
                'attachment; filename=A_ngstro_m_unit.txt; '
                "filename*=UTF-8''%C3%85ngstr%C3%B6m%20unit.txt",
            ),
            ('one,two.txt', 'attachment; filename="one,two.txt"'),
            (
                'œ,ÂČ⁄₂.txt',
                'attachment; filename=1_2_2_2.txt; '
                "filename*=UTF-8''%C2%BD%2C%C2%B2%E2%81%84%E2%82%82.txt",
            ),
            ('[foo] @ bar.txt', 'attachment; filename="[foo] @ bar.txt"'),
            (
                '[fĂČĂł]@bĂ r,bĂ€z.txt',
                'attachment; filename=_fo_o___ba_r_ba_z.txt; '
                "filename*=UTF-8''%5Bf%C3%B2%C3%B3%5D%40b%C3%A0r%2Cb%C3%A4z.txt",
            ),
        ],
    )
    def test_content_disposition_attachment_header(self, client, filename, expected):
        resource = DownloadableResource(filename)
        client.app.add_route('/', resource)
        resp = client.simulate_get()

        assert resp.status_code == 200
        assert resp.headers['Content-Disposition'] == expected

    @pytest.mark.parametrize(
        'filename,expected',
        [
            ('report.csv', 'inline; filename="report.csv"'),
            ('Hello World.txt', 'inline; filename="Hello World.txt"'),
            (
                'Bold Digit 𝟏.txt',
                'inline; filename=Bold_Digit_1.txt; '
                "filename*=UTF-8''Bold%20Digit%20%F0%9D%9F%8F.txt",
            ),
        ],
    )
    def test_content_disposition_inline_header(self, client, filename, expected):
        resource = ViewableResource(filename)
        client.app.add_route('/', resource)
        resp = client.simulate_get()

        assert resp.status_code == 200
        assert resp.headers['Content-Disposition'] == expected

    def test_request_latin1_headers(self, client):
        client.app.add_route('/headers', HeadersDebugResource())
        client.app.add_route(
            '/headers/{header}', HeadersDebugResource(), suffix='header'
        )

        headers = {
            'User-Agent': 'Mosaic/0.9',
            'X-Latin1-Header': 'FörmÄnsrÀtt',
            'X-Size': 'groß',
        }
        resp = client.simulate_get('/headers', headers=headers)
        assert resp.status_code == 200

        headers_lower = {
            'host': 'falconframework.org',
            'user-agent': 'Mosaic/0.9',
            'x-latin1-header': 'FörmÄnsrÀtt',
            'x-size': 'groß',
        }

        headers_upper = {key.upper(): value for key, value in headers_lower.items()}

        headers_received = resp.json

        if client.app._ASGI:
            assert resp.json['raw'] == headers_lower
        else:
            assert resp.json['raw'] == headers_upper

        assert headers_received['lower'] == headers_lower

        resp = client.simulate_get('/headers/X-Latin1-Header', headers=headers)
        assert resp.json == {'x-latin1-header': 'FörmÄnsrÀtt'}
        resp = client.simulate_get('/headers/X-Size', headers=headers)
        assert resp.json == {'x-size': 'groß'}

    def test_unicode_location_headers(self, client):
        client.app.add_route('/', LocationHeaderUnicodeResource())

        result = client.simulate_get()
        assert result.headers['Location'] == '/%C3%A7runchy/bacon'
        assert result.headers['Content-Location'] == 'ab%C3%A7'

        # Test with the values swapped
        result = client.simulate_head()
        assert result.headers['Content-Location'] == '/%C3%A7runchy/bacon'
        assert result.headers['Location'] == 'ab%C3%A7'

    def test_unicode_headers_contain_only_ascii(self, client):
        client.app.add_route('/', UnicodeHeaderResource())

        result = client.simulate_get('/')

        assert result.headers['Content-Type'] == 'application/json'
        assert result.headers['X-Auth-Token'] == 'toomanysecrets'
        assert result.headers['X-Symbol'] == '@'

    @pytest.mark.parametrize('method', ['CONNECT', 'PATCH', 'POST', 'PUT'])
    def test_unicode_headers_contain_non_ascii(self, method, client):
        app = client.app
        app.add_route('/', UnicodeHeaderResource())

        if method == 'CONNECT':
            # NOTE(vytas): Response headers cannot be encoded to Latin-1.
            if not app._ASGI:
                pytest.skip('wsgiref.validate sees no evil here')

            # NOTE(vytas): Shouldn't this result in an HTTP 500 instead of
            #   bubbling up a ValueError to the app server?
            with pytest.raises(ValueError):
                client.simulate_request(method, '/')
        elif method == 'PUT':
            # NOTE(vytas): Latin-1 header values are allowed.
            resp = client.simulate_request(method, '/')
            assert resp.headers
        else:
            if app._ASGI:
                # NOTE(kgriffs): Unlike PEP-3333, the ASGI spec requires the
                #   app to encode header names and values to a byte string. This
                #   gives Falcon the opportunity to verify the character set
                #   in the process and raise an error as appropriate.
                error_type = ValueError
            else:
                # NOTE(kgriffs): The wsgiref validator that is integrated into
                #   Falcon's testing framework will catch this. However, Falcon
                #   itself does not do the check to avoid potential overhead
                #   in a production deployment.
                error_type = AssertionError

            pattern = 'Bad header name' if method == 'POST' else 'Bad header value'

            with pytest.raises(error_type, match=pattern):
                client.simulate_request(method, '/')

    def test_response_set_and_get_header(self, client):
        resource = HeaderHelpersResource()
        client.app.add_route('/', resource)

        for method in ('HEAD', 'POST', 'PUT'):
            result = client.simulate_request(method=method)

            content_type = 'x-falcon/peregrine'
            assert result.headers['Content-Type'] == content_type
            assert resource.resp.get_header('content-TyPe') == content_type

            content_type_alt = 'x-falcon/merlin'
            value = resource.resp.get_header('Content-Type', default=content_type_alt)
            assert value == content_type

            assert result.headers['Cache-Control'] == 'no-store'
            assert result.headers['X-Auth-Token'] == 'toomanysecrets'

            assert resource.resp.location is None
            assert resource.resp.get_header('X-Header-Not-Set') is None
            assert resource.resp.get_header('X-Header-Not-Set', 'Yes') == 'Yes'
            assert resource.resp.get_header('X-Header-Not-Set', default='') == ''

            value = resource.resp.get_header(
                'X-Header-Not-Set', default=content_type_alt
            )
            assert value == content_type_alt

            # Check for duplicate headers
            hist = defaultdict(int)
            for name, value in result.headers.items():
                hist[name] += 1
                assert hist[name] == 1

            # Ensure that deleted headers were not sent
            assert resource.resp.get_header('x-client-should-never-see-this') is None

    def test_response_append_header(self, client):
        client.app.add_route('/', AppendHeaderResource())

        for method in ('HEAD', 'GET'):
            result = client.simulate_request(method=method)
            value = result.headers['x-things']
            assert value == 'thing-1, thing-2, thing-3'

        result = client.simulate_request(method='POST')
        assert result.headers['x-things'] == 'thing-1'
        assert result.cookies['ut_existing_user'].max_age == 600
        assert result.cookies['partner_source'].max_age == 0

    @pytest.mark.parametrize('header_name', ['Set-Cookie', 'set-cookie', 'seT-cookie'])
    @pytest.mark.parametrize('error_type', [ValueError, falcon.HeaderNotSupported])
    def test_set_cookie_disallowed(self, client, header_name, error_type):
        resp = falcon.Response()

        cookie = (
            'ut_existing_user=1; expires=Mon, 14-Jan-2019 21:20:08 GMT; '
            'Max-Age=600; path=/'
        )

        with pytest.raises(error_type):
            resp.set_header(header_name, cookie)

        with pytest.raises(error_type):
            resp.set_headers([(header_name, cookie)])

        with pytest.raises(error_type):
            resp.get_header(header_name)

        with pytest.raises(error_type):
            resp.delete_header(header_name)

    def test_vary_star(self, client):
        client.app.add_route('/', VaryHeaderResource(['*']))
        result = client.simulate_get()
        assert result.headers['vary'] == '*'

    @pytest.mark.parametrize(
        'vary,expected_value',
        [
            (['accept-encoding'], 'accept-encoding'),
            (('accept-encoding', 'x-auth-token'), 'accept-encoding, x-auth-token'),
        ],
    )
    def test_vary_header(self, client, vary, expected_value):
        resource = VaryHeaderResource(vary)
        self._check_header(client, resource, 'Vary', expected_value)

    def test_content_type_no_body(self, client):
        client.app.add_route('/', testing.SimpleTestResource())
        result = client.simulate_get()

        # NOTE(kgriffs): Even when there is no body, Content-Type
        # should still be included per wsgiref.validate
        assert 'Content-Type' in result.headers
        assert result.headers['Content-Length'] == '0'

    @pytest.mark.parametrize('status', (falcon.HTTP_204, falcon.HTTP_304))
    def test_no_content_type(self, client, status):
        client.app.add_route('/', testing.SimpleTestResource(status=status))

        result = client.simulate_get()
        assert 'Content-Type' not in result.headers

    def test_custom_content_type(self, client):
        content_type = 'application/xml; charset=utf-8'
        resource = XmlResource(content_type)
        self._check_header(client, resource, 'Content-Type', content_type)

    def test_append_link_single(self, client):
        expected_value = '</things/2842>; rel=next'

        resource = LinkHeaderResource()
        resource.append_link('/things/2842', 'next')

        self._check_link_header(client, resource, expected_value)

    def test_append_link_multiple(self, client):
        expected_value = (
            '</things/2842>; rel=next, '
            + '<http://%C3%A7runchy/bacon>; rel=contents, '
            + '<ab%C3%A7>; rel="http://example.com/ext-type", '
            + '<ab%C3%A7>; rel="http://example.com/%C3%A7runchy", '
            + '<ab%C3%A7>; rel="https://example.com/too-%C3%A7runchy", '
            + '</alt-thing>; rel="alternate http://example.com/%C3%A7runchy"'
        )

        uri = 'ab\u00e7'

        resource = LinkHeaderResource()
        resource.append_link('/things/2842', 'next')
        resource.append_link('http://\u00e7runchy/bacon', 'contents')
        resource.append_link(uri, 'http://example.com/ext-type')
        resource.append_link(uri, 'http://example.com/\u00e7runchy')
        resource.append_link(uri, 'https://example.com/too-\u00e7runchy')
        resource.append_link('/alt-thing', 'alternate http://example.com/\u00e7runchy')

        self._check_link_header(client, resource, expected_value)

    def test_append_link_with_title(self, client):
        expected_value = '</related/thing>; rel=item; title="A related thing"'

        resource = LinkHeaderResource()
        resource.append_link('/related/thing', 'item', title='A related thing')

        self._check_link_header(client, resource, expected_value)

    def test_append_link_with_title_star(self, client):
        expected_value = (
            '</related/thing>; rel=item; '
            "title*=UTF-8''A%20related%20thing, "
            '</%C3%A7runchy/thing>; rel=item; '
            "title*=UTF-8'en'A%20%C3%A7runchy%20thing"
        )

        resource = LinkHeaderResource()
        resource.append_link(
            '/related/thing', 'item', title_star=('', 'A related thing')
        )

        resource.append_link(
            '/\u00e7runchy/thing', 'item', title_star=('en', 'A \u00e7runchy thing')
        )

        self._check_link_header(client, resource, expected_value)

    def test_append_link_with_anchor(self, client):
        expected_value = '</related/thing>; rel=item; anchor="/some%20thing/or-other"'

        resource = LinkHeaderResource()
        resource.append_link('/related/thing', 'item', anchor='/some thing/or-other')

        self._check_link_header(client, resource, expected_value)

    def test_append_link_with_hreflang(self, client):
        expected_value = '</related/thing>; rel=about; hreflang=en'

        resource = LinkHeaderResource()
        resource.append_link('/related/thing', 'about', hreflang='en')

        self._check_link_header(client, resource, expected_value)

    def test_append_link_with_hreflang_multi(self, client):
        expected_value = '</related/thing>; rel=about; hreflang=en-GB; hreflang=de'

        resource = LinkHeaderResource()
        resource.append_link('/related/thing', 'about', hreflang=('en-GB', 'de'))

        self._check_link_header(client, resource, expected_value)

    def test_append_link_with_type_hint(self, client):
        expected_value = (
            '</related/thing>; rel=alternate; type="video/mp4; codecs=avc1.640028"'
        )

        resource = LinkHeaderResource()
        resource.append_link(
            '/related/thing', 'alternate', type_hint='video/mp4; codecs=avc1.640028'
        )

        self._check_link_header(client, resource, expected_value)

    def test_append_link_complex(self, client):
        expected_value = (
            '</related/thing>; rel=alternate; '
            'title="A related thing"; '
            "title*=UTF-8'en'A%20%C3%A7runchy%20thing; "
            'type="application/json"; '
            'hreflang=en-GB; hreflang=de'
        )

        resource = LinkHeaderResource()
        resource.append_link(
            '/related/thing',
            'alternate',
            title='A related thing',
            hreflang=('en-GB', 'de'),
            type_hint='application/json',
            title_star=('en', 'A \u00e7runchy thing'),
        )

        self._check_link_header(client, resource, expected_value)

    @pytest.mark.parametrize(
        'crossorigin,expected_value',
        [
            (None, '</related/thing>; rel=alternate'),
            ('anonymous', '</related/thing>; rel=alternate; crossorigin'),
            ('Anonymous', '</related/thing>; rel=alternate; crossorigin'),
            ('AnOnYmOUs', '</related/thing>; rel=alternate; crossorigin'),
            (
                'Use-Credentials',
                '</related/thing>; rel=alternate; crossorigin="use-credentials"',
            ),
            (
                'use-credentials',
                '</related/thing>; rel=alternate; crossorigin="use-credentials"',
            ),
        ],
    )
    def test_append_link_crossorigin(self, client, crossorigin, expected_value):
        resource = LinkHeaderResource()
        resource.append_link('/related/thing', 'alternate', crossorigin=crossorigin)

        self._check_link_header(client, resource, expected_value)

    @pytest.mark.parametrize(
        'crossorigin',
        [
            '*',
            'Allow-all',
            'Lax',
            'MUST-REVALIDATE',
            'Strict',
            'deny',
        ],
    )
    def test_append_link_invalid_crossorigin_value(self, crossorigin):
        resp = falcon.Response()

        with pytest.raises(ValueError):
            resp.append_link('/related/resource', 'next', crossorigin=crossorigin)

    def test_append_link_with_link_extension(self, client):
        expected_value = '</related/thing>; rel=item; sizes=72x72'

        resource = LinkHeaderResource()
        resource.append_link(
            '/related/thing', 'item', link_extension=[('sizes', '72x72')]
        )

        self._check_link_header(client, resource, expected_value)

    def test_content_length_options(self, client):
        result = client.simulate_options()

        content_length = str(len(falcon.HTTPNotFound().to_json()))
        assert result.headers['Content-Length'] == content_length

    def test_set_headers_with_custom_class(self, client):
        client.app.add_route('/', CustomHeadersResource())

        result = client.simulate_get('/')

        assert 'test-header' in result.headers
        assert result.headers['test-header'] == 'test-value'

    def test_headers_with_custom_class_not_callable(self, client):
        client.app.add_route('/', CustomHeadersResource())

        result = client.simulate_post('/')

        assert 'test-header' not in result.headers

    def test_request_multiple_header(self, client):
        resource = HeaderHelpersResource()
        client.app.add_route('/', resource)

        client.simulate_request(
            headers=[
                # Singletone header; last one wins
                ('Content-Type', 'text/plain'),
                ('Content-Type', 'image/jpeg'),
                # Should be concatenated
                ('X-Thing', '1'),
                ('X-Thing', '2'),
            ]
        )

        assert resource.req.content_type == 'image/jpeg'
        assert resource.req.get_header('X-Thing') == '1,2'

    # ----------------------------------------------------------------------
    # Helpers
    # ----------------------------------------------------------------------

    def _check_link_header(self, client, resource, expected_value):
        self._check_header(client, resource, 'Link', expected_value)

    def _check_header(self, client, resource, header, expected_value):
        client.app.add_route('/', resource)

        result = client.simulate_get()
        assert result.headers[header] == expected_value