2
2
Comprehensive tests for File parameter multipart parsing and validation.
3
3
"""
4
4
5
+ from __future__ import annotations
6
+
5
7
import base64
6
8
import json
7
9
from typing import Annotated
8
10
9
- import pytest
10
-
11
11
from aws_lambda_powertools .event_handler import APIGatewayRestResolver
12
12
from aws_lambda_powertools .event_handler .openapi .params import File , Form
13
13
@@ -23,7 +23,7 @@ def make_multipart_event(boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW", body_
23
23
body_lines .append (f"--{ boundary } " )
24
24
body_lines .append (
25
25
f'Content-Disposition: form-data; name="{ part ["name" ]} "'
26
- + (f'; filename="{ part ["filename" ]} "' if part .get ("filename" ) else "" )
26
+ + (f'; filename="{ part ["filename" ]} "' if part .get ("filename" ) else "" ),
27
27
)
28
28
if part .get ("content_type" ):
29
29
body_lines .append (f"Content-Type: { part ['content_type' ]} " )
@@ -75,7 +75,7 @@ def upload_file(file: Annotated[bytes, File(description="File to upload")]):
75
75
76
76
# Create a simple file upload
77
77
event = make_multipart_event (
78
- body_parts = [{"name" : "file" , "filename" : "test.txt" , "content_type" : "text/plain" , "content" : "Hello, world!" }]
78
+ body_parts = [{"name" : "file" , "filename" : "test.txt" , "content_type" : "text/plain" , "content" : "Hello, world!" }],
79
79
)
80
80
81
81
response = app .resolve (event , {})
@@ -109,7 +109,7 @@ def upload_with_metadata(
109
109
},
110
110
{"name" : "title" , "content" : "Important Document" },
111
111
{"name" : "description" , "content" : "This is a test document upload" },
112
- ]
112
+ ],
113
113
)
114
114
115
115
response = app .resolve (event , {})
@@ -134,7 +134,7 @@ def upload_file(file: Annotated[bytes, File()]):
134
134
event = make_multipart_event (
135
135
boundary = webkit_boundary ,
136
136
body_parts = [
137
- {"name" : "file" , "filename" : "test.jpg" , "content_type" : "image/jpeg" , "content" : "fake image data" }
137
+ {"name" : "file" , "filename" : "test.jpg" , "content_type" : "image/jpeg" , "content" : "fake image data" },
138
138
],
139
139
)
140
140
@@ -180,7 +180,7 @@ def upload_files(file1: Annotated[bytes, File(alias="file1")], file2: Annotated[
180
180
body_parts = [
181
181
{"name" : "file1" , "filename" : "first.txt" , "content" : "First file content" },
182
182
{"name" : "file2" , "filename" : "second.txt" , "content" : "Second file content is longer" },
183
- ]
183
+ ],
184
184
)
185
185
186
186
response = app .resolve (event , {})
@@ -263,8 +263,12 @@ def upload_file(file: Annotated[bytes, File(description="Small file", max_length
263
263
# Test file that's too large
264
264
event = make_multipart_event (
265
265
body_parts = [
266
- {"name" : "file" , "filename" : "large.txt" , "content" : "This file content is way too long for the constraint" }
267
- ]
266
+ {
267
+ "name" : "file" ,
268
+ "filename" : "large.txt" ,
269
+ "content" : "This file content is way too long for the constraint" ,
270
+ },
271
+ ],
268
272
)
269
273
270
274
response = app .resolve (event , {})
@@ -312,8 +316,8 @@ def upload_file(file: Annotated[bytes, File()]):
312
316
"name" : "file" ,
313
317
"filename" : "empty.txt" ,
314
318
"content" : "" , # Empty file
315
- }
316
- ]
319
+ },
320
+ ],
317
321
)
318
322
319
323
response = app .resolve (event , {})
0 commit comments