|
1 | 1 | import http.client |
| 2 | +from collections import defaultdict |
2 | 3 |
|
| 4 | +import pytest |
3 | 5 | import requests |
4 | 6 | from werkzeug.datastructures import Headers |
5 | 7 | from werkzeug.http import parse_dict_header |
6 | 8 |
|
7 | 9 | from pytest_httpserver import HTTPServer |
8 | 10 | from pytest_httpserver.httpserver import HeaderValueMatcher |
| 11 | +from pytest_httpserver.httpserver import NoMethodFoundForMatchingHeaderValueError |
9 | 12 |
|
10 | 13 |
|
11 | | -def test_custom_headers(httpserver: HTTPServer): |
12 | | - headers_with_values_in_direct_order = {"Custom": 'Scheme key0="value0", key1="value1"'} |
| 14 | +def test_header_matcher_fallback_does_not_cache_header_names(): |
| 15 | + matchers = defaultdict(lambda: HeaderValueMatcher.default_header_value_matcher) |
| 16 | + matcher = HeaderValueMatcher(matchers) |
| 17 | + |
| 18 | + assert matcher("x-custom", "value", "value") |
| 19 | + assert not matcher("x-custom", "VALUE", "value") |
| 20 | + assert not matchers |
| 21 | + |
| 22 | + matchers["X-Custom"] = lambda actual, expected: actual == expected.upper() |
| 23 | + assert matcher("x-custom", "VALUE", "value") |
| 24 | + |
| 25 | + |
| 26 | +def test_header_matcher_without_default(): |
| 27 | + matcher = HeaderValueMatcher({"x-custom": HeaderValueMatcher.default_header_value_matcher}) |
| 28 | + |
| 29 | + assert matcher("X-CUSTOM", "value", "value") |
| 30 | + assert not matcher("X-CUSTOM", "VALUE", "value") |
| 31 | + with pytest.raises(NoMethodFoundForMatchingHeaderValueError, match="X-Missing"): |
| 32 | + matcher("X-Missing", "value", "value") |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("header_name", ["Custom", "custom", "CUSTOM"]) |
| 36 | +def test_custom_headers(httpserver: HTTPServer, header_name: str): |
| 37 | + headers_with_values_in_direct_order = {header_name: 'Scheme key0="value0", key1="value1"'} |
13 | 38 | httpserver.expect_request(uri="/", headers=headers_with_values_in_direct_order).respond_with_data("OK") |
14 | 39 | response = requests.get(httpserver.url_for("/"), headers=headers_with_values_in_direct_order) |
15 | 40 | assert response.status_code == 200 |
@@ -42,9 +67,10 @@ def custom_header_value_matcher(actual: str, expected: str) -> bool: |
42 | 67 |
|
43 | 68 |
|
44 | 69 | # See https://en.wikipedia.org/wiki/Digest_access_authentication |
45 | | -def test_authorization_headers(httpserver: HTTPServer): |
| 70 | +@pytest.mark.parametrize("header_name", ["Authorization", "authorization", "AUTHORIZATION", "aUtHoRiZaTiOn"]) |
| 71 | +def test_authorization_headers(httpserver: HTTPServer, header_name: str): |
46 | 72 | headers_with_values_in_direct_order = { |
47 | | - "Authorization": ( |
| 73 | + header_name: ( |
48 | 74 | 'Digest username="Mufasa",' |
49 | 75 | 'realm="testrealm@host.com",' |
50 | 76 | 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",' |
|
0 commit comments