|
1 | 1 | import os |
| 2 | +import re |
2 | 3 | from urllib.parse import urlencode |
3 | 4 |
|
4 | 5 | from python.lib.testcase import WebServerAutoTestCase |
5 | 6 |
|
| 7 | +def get_multipart_temp_files(): |
| 8 | + """Get files that look like multipart temp files (6 random alphanumeric chars in /tmp/, optionally with tmp. prefix).""" |
| 9 | + pattern = re.compile(r'^(tmp\.)?[a-zA-Z0-9]{6}$') |
| 10 | + return set(f for f in os.listdir("/tmp/") if pattern.match(f) and os.path.isfile(f"/tmp/{f}")) |
| 11 | + |
6 | 12 |
|
7 | 13 | class TestMultipartContentType(WebServerAutoTestCase): |
8 | 14 |
|
@@ -190,7 +196,7 @@ def test_multipart_non_terminating_boundary(self): |
190 | 196 |
|
191 | 197 | def test_multipart_filename_attribute(self): |
192 | 198 |
|
193 | | - tmp_files = os.listdir("/tmp/") |
| 199 | + tmp_files = get_multipart_temp_files() |
194 | 200 | boundary = "------------------------d74496d66958873e" |
195 | 201 |
|
196 | 202 | file_bytes = b"Hello from test.txt\nSecond line\n" |
@@ -221,12 +227,12 @@ def test_multipart_filename_attribute(self): |
221 | 227 | self.assertTrue(response.content.find(b"filename : test.txt") != -1) |
222 | 228 | self.assertTrue(response.content.find(b"Hello from test.txt") != -1) |
223 | 229 |
|
224 | | - tmp_files_after_script = os.listdir("/tmp/") |
| 230 | + tmp_files_after_script = get_multipart_temp_files() |
225 | 231 | # check that script delete tmp files at the end |
226 | 232 | self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script)) |
227 | 233 |
|
228 | 234 | def test_multipart_filename_array_attribute(self): |
229 | | - tmp_files = os.listdir("/tmp/") |
| 235 | + tmp_files = get_multipart_temp_files() |
230 | 236 |
|
231 | 237 | boundary = "------------------------d74496d66958873e" |
232 | 238 |
|
@@ -270,13 +276,13 @@ def test_multipart_filename_array_attribute(self): |
270 | 276 | self.assertTrue(response.content.find(b"Hello from a.txt") != -1) |
271 | 277 | self.assertTrue(response.content.find(b"Hello from b.txt") != -1) |
272 | 278 |
|
273 | | - tmp_files_after_script = os.listdir("/tmp/") |
| 279 | + tmp_files_after_script = get_multipart_temp_files() |
274 | 280 | # check that script delete tmp files at the end |
275 | 281 | self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script)) |
276 | 282 |
|
277 | 283 | def test_multipart_superglobal_modify(self): |
278 | 284 |
|
279 | | - tmp_files = os.listdir("/tmp/") |
| 285 | + tmp_files = get_multipart_temp_files() |
280 | 286 | boundary = "------------------------d74496d66958873e" |
281 | 287 |
|
282 | 288 | file_bytes = b"Hello from test.txt\nSecond line\n" |
@@ -305,13 +311,13 @@ def test_multipart_superglobal_modify(self): |
305 | 311 |
|
306 | 312 | self.assertEqual(200, response.status_code) |
307 | 313 |
|
308 | | - tmp_files_after_script = os.listdir("/tmp/") |
| 314 | + tmp_files_after_script = get_multipart_temp_files() |
309 | 315 | # check that script delete tmp files at the end |
310 | 316 | self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script)) |
311 | 317 |
|
312 | 318 | def test_multipart_mixed_files_and_fields(self): |
313 | 319 | """Test mixing files and regular form fields in the same request.""" |
314 | | - tmp_files = os.listdir("/tmp/") |
| 320 | + tmp_files = get_multipart_temp_files() |
315 | 321 | boundary = '------------------------d74496d66958873e' |
316 | 322 |
|
317 | 323 | file_bytes = b'File content here\n' |
@@ -350,13 +356,13 @@ def test_multipart_mixed_files_and_fields(self): |
350 | 356 | self.assertTrue(response.content.find(b'filename : test.txt') != -1) |
351 | 357 | self.assertTrue(response.content.find(b'another : another value') != -1) |
352 | 358 |
|
353 | | - tmp_files_after_script = os.listdir("/tmp/") |
| 359 | + tmp_files_after_script = get_multipart_temp_files() |
354 | 360 | # check that script delete tmp files at the end |
355 | 361 | self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script)) |
356 | 362 |
|
357 | 363 | def test_multipart_file_without_content_type(self): |
358 | 364 | """Test file upload without explicit Content-Type header.""" |
359 | | - tmp_files = os.listdir("/tmp/") |
| 365 | + tmp_files = get_multipart_temp_files() |
360 | 366 | boundary = '------------------------d74496d66958873e' |
361 | 367 |
|
362 | 368 | file_bytes = b'File without content type\n' |
@@ -387,13 +393,13 @@ def test_multipart_file_without_content_type(self): |
387 | 393 | # Should default to text/plain |
388 | 394 | self.assertTrue(response.content.find(b'type : text/plain') != -1) |
389 | 395 |
|
390 | | - tmp_files_after_script = os.listdir("/tmp/") |
| 396 | + tmp_files_after_script = get_multipart_temp_files() |
391 | 397 | # check that script delete tmp files at the end |
392 | 398 | self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script)) |
393 | 399 |
|
394 | 400 | def test_multipart_binary_file(self): |
395 | 401 | """Test uploading binary file content.""" |
396 | | - tmp_files = os.listdir("/tmp/") |
| 402 | + tmp_files = get_multipart_temp_files() |
397 | 403 | boundary = '------------------------d74496d66958873e' |
398 | 404 |
|
399 | 405 | # Binary content with null bytes |
@@ -424,7 +430,7 @@ def test_multipart_binary_file(self): |
424 | 430 | self.assertTrue(response.content.find(b'size : 8') != -1) |
425 | 431 | self.assertTrue(response.content.find(b'filename : data.bin') != -1) |
426 | 432 |
|
427 | | - tmp_files_after_script = os.listdir("/tmp/") |
| 433 | + tmp_files_after_script = get_multipart_temp_files() |
428 | 434 | # check that script delete tmp files at the end |
429 | 435 | self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script)) |
430 | 436 |
|
|
0 commit comments