Skip to content

Commit 8704fb0

Browse files
committed
respond_with_data: fix headers type hinting
This can accept a mapping or a list of 2-element tuples. In this latter case, multiple values can be specified for the same header. A testcase is added for this. Fixes csernazs#132
1 parent 5626883 commit 8704fb0

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

pytest_httpserver/httpserver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from enum import Enum
99
from contextlib import suppress, contextmanager
1010
from copy import copy
11-
from typing import Any, Callable, List, Mapping, MutableMapping, Optional, Tuple, Union, Pattern
11+
from typing import Any, Callable, Iterable, List, Mapping, MutableMapping, Optional, Tuple, Union, Pattern
1212
from ssl import SSLContext
1313
import abc
1414

@@ -23,6 +23,11 @@
2323
URI_DEFAULT = ""
2424
METHOD_ALL = "__ALL"
2525

26+
HEADERS_T = Union[
27+
Mapping[str, Union[str, int, Iterable[Union[str, int]]]],
28+
Iterable[Tuple[str, Union[str, int]]],
29+
]
30+
2631

2732
class Undefined:
2833
def __repr__(self):
@@ -475,7 +480,7 @@ def respond_with_data(
475480
self,
476481
response_data: Union[str, bytes] = "",
477482
status: int = 200,
478-
headers: Optional[Mapping[str, str]] = None,
483+
headers: Optional[HEADERS_T] = None,
479484
mimetype: Optional[str] = None,
480485
content_type: Optional[str] = None):
481486
"""

tests/test_headers.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import http.client
2+
13
import requests
24
from pytest_httpserver import HTTPServer
3-
from werkzeug.http import parse_dict_header
4-
55
from pytest_httpserver.httpserver import HeaderValueMatcher
6+
from werkzeug.http import parse_dict_header
7+
from werkzeug.datastructures import Headers
68

79

810
def test_custom_headers(httpserver: HTTPServer):
@@ -72,3 +74,24 @@ def test_authorization_headers(httpserver: HTTPServer):
7274
response = requests.get(httpserver.url_for('/'), headers=headers_with_values_in_modified_order)
7375
assert response.status_code == 200
7476
assert response.text == 'OK'
77+
78+
79+
def test_header_one_key_multiple_values(httpserver: HTTPServer):
80+
httpserver.expect_request(uri="/t1").respond_with_data(headers=[("X-Foo", "123"), ("X-Foo", "456")])
81+
httpserver.expect_request(uri="/t2").respond_with_data(headers={"X-Foo": ["123", "456"]})
82+
httpserver.expect_request(uri="/t3").respond_with_data(headers={"X-Foo": [123, 456]})
83+
84+
headers = Headers()
85+
headers.add("X-Foo", "123")
86+
headers.add("X-Foo", "456")
87+
88+
httpserver.expect_request(uri="/t4").respond_with_data(headers=headers)
89+
90+
for uri in ("/t1", "/t2", "/t3", "/t4"):
91+
conn = http.client.HTTPConnection("localhost:{}".format(httpserver.port))
92+
conn.request("GET", uri)
93+
response = conn.getresponse()
94+
conn.close()
95+
96+
assert response.status == 200
97+
assert response.headers.get_all("X-Foo") == ["123", "456"]

0 commit comments

Comments
 (0)