Skip to content

Commit 1eaa1a8

Browse files
committed
Use actual content length too
1 parent 7c8d2b2 commit 1eaa1a8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

tests/files_test.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ def httpclient_log(*args):
3737

3838
def get_actual_request_data(log_stream):
3939
boundary_pattern = r"(?<=boundary=)(.\w*)"
40+
content_length_pattern = r"(?<=Content-Length: )(.\d*)"
4041

4142
boundary = ""
4243
actual_request_post = ""
44+
content_length = ""
4345
for log in log_stream:
4446
for log_arg in log:
4547
boundary_search = re.findall(boundary_pattern, log_arg)
48+
content_length_search = re.findall(content_length_pattern, log_arg)
4649
if boundary_search:
4750
boundary = boundary_search[0]
48-
elif "Content-Disposition" in log_arg:
51+
if content_length_search:
52+
content_length = content_length_search[0]
53+
if "Content-Disposition" in log_arg:
4954
# this will look like:
5055
# b\'{content}\'
5156
# and escapped characters look like:
@@ -55,7 +60,7 @@ def get_actual_request_data(log_stream):
5560
post_unescaped = post_bytes.decode("unicode_escape")
5661
actual_request_post = post_unescaped.encode("latin1")
5762

58-
return boundary, actual_request_post
63+
return boundary, content_length, actual_request_post
5964

6065

6166
def test_post_files_text( # pylint: disable=unused-argument
@@ -66,7 +71,7 @@ def test_post_files_text( # pylint: disable=unused-argument
6671
}
6772

6873
python_requests.post(post_url, files=file_data)
69-
boundary, actual_request_post = get_actual_request_data(log_stream)
74+
boundary, content_length, actual_request_post = get_actual_request_data(log_stream)
7075

7176
requests._build_boundary_string = mock.Mock(return_value=boundary)
7277
requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data)
@@ -84,7 +89,7 @@ def test_post_files_text( # pylint: disable=unused-argument
8489
[
8590
mock.call(b"Content-Length"),
8691
mock.call(b": "),
87-
mock.call(b"131"),
92+
mock.call(content_length.encode()),
8893
mock.call(b"\r\n"),
8994
]
9095
)
@@ -111,7 +116,9 @@ def test_post_files_file( # pylint: disable=unused-argument
111116
}
112117

113118
python_requests.post(post_url, files=file_data)
114-
boundary, actual_request_post = get_actual_request_data(log_stream)
119+
boundary, content_length, actual_request_post = get_actual_request_data(
120+
log_stream
121+
)
115122

116123
requests._build_boundary_string = mock.Mock(return_value=boundary)
117124
requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data)
@@ -129,7 +136,7 @@ def test_post_files_file( # pylint: disable=unused-argument
129136
[
130137
mock.call(b"Content-Length"),
131138
mock.call(b": "),
132-
mock.call(b"347"),
139+
mock.call(content_length.encode()),
133140
mock.call(b"\r\n"),
134141
]
135142
)
@@ -164,7 +171,9 @@ def test_post_files_complex( # pylint: disable=unused-argument
164171
}
165172

166173
python_requests.post(post_url, files=file_data)
167-
boundary, actual_request_post = get_actual_request_data(log_stream)
174+
boundary, content_length, actual_request_post = get_actual_request_data(
175+
log_stream
176+
)
168177

169178
requests._build_boundary_string = mock.Mock(return_value=boundary)
170179
requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data)
@@ -182,7 +191,7 @@ def test_post_files_complex( # pylint: disable=unused-argument
182191
[
183192
mock.call(b"Content-Length"),
184193
mock.call(b": "),
185-
mock.call(b"796"),
194+
mock.call(content_length.encode()),
186195
mock.call(b"\r\n"),
187196
]
188197
)

0 commit comments

Comments
 (0)