Skip to content

Commit b849a0b

Browse files
committed
Add support for embedded headers; include tests for valid and malformed headers
1 parent 5b604c3 commit b849a0b

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

httpie/cli/requestitems.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,16 @@ def from_args(
109109
processor_func, target_dict = rules[arg.sep]
110110
value = processor_func(arg)
111111

112+
# NEW: handle multi-header files
113+
if isinstance(value, list):
114+
# value is a list of (key, value) pairs
115+
for k, v in value:
116+
target_dict.add(k, v)
117+
continue # Skip normal handling
118+
119+
# Normal case
112120
if arg.sep in SEPARATORS_GROUP_MULTIPART:
113121
instance.multipart_data[arg.key] = value
114-
115122
if isinstance(target_dict, BaseMultiDict):
116123
target_dict.add(arg.key, value)
117124
else:
@@ -127,8 +134,21 @@ def process_header_arg(arg: KeyValueArg) -> Optional[str]:
127134
return arg.value or None
128135

129136

130-
def process_embed_header_arg(arg: KeyValueArg) -> str:
131-
return load_text_file(arg).rstrip('\n')
137+
def process_embed_header_arg(arg: KeyValueArg):
138+
text = load_text_file(arg).rstrip('\n')
139+
lines = [line.strip() for line in text.splitlines() if line.strip()]
140+
141+
header_pairs = []
142+
143+
for i, line in enumerate(lines, start=1):
144+
if ':' not in line:
145+
raise ParseError(f"{arg.orig!r}: Malformed header on line {i}: {line!r}")
146+
147+
key, value = line.split(':', 1)
148+
header_pairs.append((key.strip(), value.strip()))
149+
150+
return header_pairs
151+
132152

133153

134154
def process_empty_header_arg(arg: KeyValueArg) -> str:

test_headers.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
from httpie.cli.requestitems import RequestItems
3+
from httpie.cli.argtypes import KeyValueArg
4+
from httpie.cli.constants import SEPARATOR_HEADER_EMBED
5+
from httpie.cli.exceptions import ParseError
6+
7+
8+
def load_request_items(file_name: str):
9+
"""Helper to create RequestItems from a header file."""
10+
arg = KeyValueArg(
11+
key=None,
12+
value=file_name,
13+
sep=SEPARATOR_HEADER_EMBED,
14+
orig=f"@{file_name}"
15+
)
16+
return RequestItems.from_args([arg])
17+
18+
19+
def test_headers_txt():
20+
req_items = load_request_items('headers.txt')
21+
expected = {
22+
"X-API-Key": "12345",
23+
"User-Agent": "MyApp/1.0",
24+
"Accept": "application/json",
25+
}
26+
for k, v in expected.items():
27+
assert req_items.headers.get(k) == v
28+
29+
30+
def test_headers_with_empty_lines_txt():
31+
req_items = load_request_items('headers_with_empty_lines.txt')
32+
expected = {
33+
"X-API-Key": "12345",
34+
"User-Agent": "MyApp/1.0",
35+
"Accept": "application/json",
36+
}
37+
for k, v in expected.items():
38+
assert req_items.headers.get(k) == v
39+
40+
41+
def test_headers_malformed_txt():
42+
# This file intentionally has a malformed header; should raise ParseError
43+
with pytest.raises(ParseError):
44+
load_request_items('headers_malformed.txt')

0 commit comments

Comments
 (0)