1
1
from __future__ import annotations
2
2
3
3
import datetime as dt
4
- import os
5
4
import time
6
5
from http .cookies import SimpleCookie
7
6
from pathlib import Path
@@ -215,11 +214,10 @@ def test_response_phrase(test_client_factory: TestClientFactory) -> None:
215
214
assert response .reason_phrase == ""
216
215
217
216
218
- def test_file_response (tmpdir : Path , test_client_factory : TestClientFactory ) -> None :
219
- path = os . path . join ( tmpdir , "xyz" )
217
+ def test_file_response (tmp_path : Path , test_client_factory : TestClientFactory ) -> None :
218
+ path = tmp_path / "xyz"
220
219
content = b"<file content>" * 1000
221
- with open (path , "wb" ) as file :
222
- file .write (content )
220
+ path .write_bytes (content )
223
221
224
222
filled_by_bg_task = ""
225
223
@@ -258,11 +256,10 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
258
256
259
257
260
258
@pytest .mark .anyio
261
- async def test_file_response_on_head_method (tmpdir : Path ) -> None :
262
- path = os . path . join ( tmpdir , "xyz" )
259
+ async def test_file_response_on_head_method (tmp_path : Path ) -> None :
260
+ path = tmp_path / "xyz"
263
261
content = b"<file content>" * 1000
264
- with open (path , "wb" ) as file :
265
- file .write (content )
262
+ path .write_bytes (content )
266
263
267
264
app = FileResponse (path = path , filename = "example.png" )
268
265
@@ -287,20 +284,34 @@ async def send(message: Message) -> None:
287
284
await app ({"type" : "http" , "method" : "head" }, receive , send )
288
285
289
286
287
+ def test_file_response_set_media_type (
288
+ tmp_path : Path , test_client_factory : TestClientFactory
289
+ ) -> None :
290
+ path = tmp_path / "xyz"
291
+ path .write_bytes (b"<file content>" )
292
+
293
+ # By default, FileResponse will determine the `content-type` based on
294
+ # the filename or path, unless a specific `media_type` is provided.
295
+ app = FileResponse (path = path , filename = "example.png" , media_type = "image/jpeg" )
296
+ client : TestClient = test_client_factory (app )
297
+ response = client .get ("/" )
298
+ assert response .headers ["content-type" ] == "image/jpeg"
299
+
300
+
290
301
def test_file_response_with_directory_raises_error (
291
- tmpdir : Path , test_client_factory : TestClientFactory
302
+ tmp_path : Path , test_client_factory : TestClientFactory
292
303
) -> None :
293
- app = FileResponse (path = tmpdir , filename = "example.png" )
304
+ app = FileResponse (path = tmp_path , filename = "example.png" )
294
305
client = test_client_factory (app )
295
306
with pytest .raises (RuntimeError ) as exc_info :
296
307
client .get ("/" )
297
308
assert "is not a file" in str (exc_info .value )
298
309
299
310
300
311
def test_file_response_with_missing_file_raises_error (
301
- tmpdir : Path , test_client_factory : TestClientFactory
312
+ tmp_path : Path , test_client_factory : TestClientFactory
302
313
) -> None :
303
- path = os . path . join ( tmpdir , "404.txt" )
314
+ path = tmp_path / "404.txt"
304
315
app = FileResponse (path = path , filename = "404.txt" )
305
316
client = test_client_factory (app )
306
317
with pytest .raises (RuntimeError ) as exc_info :
@@ -309,13 +320,12 @@ def test_file_response_with_missing_file_raises_error(
309
320
310
321
311
322
def test_file_response_with_chinese_filename (
312
- tmpdir : Path , test_client_factory : TestClientFactory
323
+ tmp_path : Path , test_client_factory : TestClientFactory
313
324
) -> None :
314
325
content = b"file content"
315
326
filename = "你好.txt" # probably "Hello.txt" in Chinese
316
- path = os .path .join (tmpdir , filename )
317
- with open (path , "wb" ) as f :
318
- f .write (content )
327
+ path = tmp_path / filename
328
+ path .write_bytes (content )
319
329
app = FileResponse (path = path , filename = filename )
320
330
client = test_client_factory (app )
321
331
response = client .get ("/" )
@@ -326,13 +336,12 @@ def test_file_response_with_chinese_filename(
326
336
327
337
328
338
def test_file_response_with_inline_disposition (
329
- tmpdir : Path , test_client_factory : TestClientFactory
339
+ tmp_path : Path , test_client_factory : TestClientFactory
330
340
) -> None :
331
341
content = b"file content"
332
342
filename = "hello.txt"
333
- path = os .path .join (tmpdir , filename )
334
- with open (path , "wb" ) as f :
335
- f .write (content )
343
+ path = tmp_path / filename
344
+ path .write_bytes (content )
336
345
app = FileResponse (path = path , filename = filename , content_disposition_type = "inline" )
337
346
client = test_client_factory (app )
338
347
response = client .get ("/" )
@@ -342,11 +351,9 @@ def test_file_response_with_inline_disposition(
342
351
assert response .headers ["content-disposition" ] == expected_disposition
343
352
344
353
345
- def test_file_response_with_method_warns (
346
- tmpdir : Path , test_client_factory : TestClientFactory
347
- ) -> None :
354
+ def test_file_response_with_method_warns (tmp_path : Path ) -> None :
348
355
with pytest .warns (DeprecationWarning ):
349
- FileResponse (path = tmpdir , filename = "example.png" , method = "GET" )
356
+ FileResponse (path = tmp_path , filename = "example.png" , method = "GET" )
350
357
351
358
352
359
def test_set_cookie (
@@ -381,6 +388,18 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
381
388
)
382
389
383
390
391
+ def test_set_cookie_path_none (test_client_factory : TestClientFactory ) -> None :
392
+ async def app (scope : Scope , receive : Receive , send : Send ) -> None :
393
+ response = Response ("Hello, world!" , media_type = "text/plain" )
394
+ response .set_cookie ("mycookie" , "myvalue" , path = None )
395
+ await response (scope , receive , send )
396
+
397
+ client = test_client_factory (app )
398
+ response = client .get ("/" )
399
+ assert response .text == "Hello, world!"
400
+ assert response .headers ["set-cookie" ] == "mycookie=myvalue; SameSite=lax"
401
+
402
+
384
403
@pytest .mark .parametrize (
385
404
"expires" ,
386
405
[
@@ -477,12 +496,11 @@ def test_response_do_not_add_redundant_charset(
477
496
478
497
479
498
def test_file_response_known_size (
480
- tmpdir : Path , test_client_factory : TestClientFactory
499
+ tmp_path : Path , test_client_factory : TestClientFactory
481
500
) -> None :
482
- path = os . path . join ( tmpdir , "xyz" )
501
+ path = tmp_path / "xyz"
483
502
content = b"<file content>" * 1000
484
- with open (path , "wb" ) as file :
485
- file .write (content )
503
+ path .write_bytes (content )
486
504
487
505
app = FileResponse (path = path , filename = "example.png" )
488
506
client : TestClient = test_client_factory (app )
0 commit comments