From 45ca76b124d5a67dd08059e843c380683825443a Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sat, 11 Jun 2022 09:56:40 +0200 Subject: [PATCH 1/6] Add more TestClient examples --- docs/.DS_Store | Bin 0 -> 6148 bytes docs/testclient.md | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 docs/.DS_Store diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3945d792be1833c5e254f19cb4ecdcfa7deae4a4 GIT binary patch literal 6148 zcmeHKu};H44E41|3b1rRV$3geX9!i8SU~-Os06ivhD1~_=N}Lo13RC=z!xC#4{Uq^ z&%PtgHK8LyU`OY7@!4nRNr`hrWX3n?h-gSe0S4n>2dYDOokvT0&ceZl_ZUsiio6u- zpyjOwWk4DDZ4B_c+oz0jnk{DD`+K=s$%{CiE{Z8E!OLFt@bUV5*j!CFtd{fUuAGi~ zbU{lm8y&xY*_*ktgF2VQMp)UE|6q#cVC>)?2q%vxcYBx`vMZ zniXyK^y?EI0$Ngu8n1u_YFw-Cj3YWx!{kZ*Pa(|Bt>u|NB9Drwk|qe~JM! zh$rzFkK}r5>*2W9h8PJ33;R_TB?QW~W0`O(-p3HZTF4V%hNU E2|25d$^ZZW literal 0 HcmV?d00001 diff --git a/docs/testclient.md b/docs/testclient.md index a214715fe..eef4d55b8 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -26,6 +26,30 @@ function calls, not awaitables. You can use any of `requests` standard API, such as authentication, session cookies handling, or file uploads. +For example, to set headers on the client you can do: + +```python +client = TestClient(app) + +# Set the headers on the client for future requests +client.headers = {"Authorization": "..."} +response = client.gete("/") + +# Set headers for each request separately +response = client.get("/", headers={"Authorization": "..."}) +``` + +Or to send files with the TestClient: + +```python +client = TestClient(app) + +files = {"file": open("example.txt", "rb")} +response = client.post("/form", files=files) +``` + +For more information you can view `requests.Session` [docs](https://requests.readthedocs.io/en/master/user/advanced/) and [API](https://requests.readthedocs.io/en/master/api/#requests.Session). + By default the `TestClient` will raise any exceptions that occur in the application. Occasionally you might want to test the content of 500 error responses, rather than allowing client to raise the server exception. In this From 8a0c186f45a82bb579d125635efcf8cc7c34b8c4 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sat, 11 Jun 2022 09:57:57 +0200 Subject: [PATCH 2/6] update --- docs/.DS_Store | Bin 6148 -> 0 bytes docs/testclient.md | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 docs/.DS_Store diff --git a/docs/.DS_Store b/docs/.DS_Store deleted file mode 100644 index 3945d792be1833c5e254f19cb4ecdcfa7deae4a4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKu};H44E41|3b1rRV$3geX9!i8SU~-Os06ivhD1~_=N}Lo13RC=z!xC#4{Uq^ z&%PtgHK8LyU`OY7@!4nRNr`hrWX3n?h-gSe0S4n>2dYDOokvT0&ceZl_ZUsiio6u- zpyjOwWk4DDZ4B_c+oz0jnk{DD`+K=s$%{CiE{Z8E!OLFt@bUV5*j!CFtd{fUuAGi~ zbU{lm8y&xY*_*ktgF2VQMp)UE|6q#cVC>)?2q%vxcYBx`vMZ zniXyK^y?EI0$Ngu8n1u_YFw-Cj3YWx!{kZ*Pa(|Bt>u|NB9Drwk|qe~JM! zh$rzFkK}r5>*2W9h8PJ33;R_TB?QW~W0`O(-p3HZTF4V%hNU E2|25d$^ZZW diff --git a/docs/testclient.md b/docs/testclient.md index eef4d55b8..024102231 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -33,7 +33,7 @@ client = TestClient(app) # Set the headers on the client for future requests client.headers = {"Authorization": "..."} -response = client.gete("/") +response = client.get("/") # Set headers for each request separately response = client.get("/", headers={"Authorization": "..."}) From 8c5eaad8879877eef5829874f0e82b441c0318c2 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sat, 11 Jun 2022 10:00:37 +0200 Subject: [PATCH 3/6] improve wording --- docs/testclient.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/testclient.md b/docs/testclient.md index 024102231..f0ca218a9 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -26,12 +26,12 @@ function calls, not awaitables. You can use any of `requests` standard API, such as authentication, session cookies handling, or file uploads. -For example, to set headers on the client you can do: +For example, to set headers on the TestClient you can do: ```python client = TestClient(app) -# Set the headers on the client for future requests +# Set headers on the client for future requests client.headers = {"Authorization": "..."} response = client.get("/") @@ -39,7 +39,7 @@ response = client.get("/") response = client.get("/", headers={"Authorization": "..."}) ``` -Or to send files with the TestClient: +And for example to send files with the TestClient: ```python client = TestClient(app) From f02a895f4ac7397d4b3f651e411d051dc6ebf940 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sat, 11 Jun 2022 10:29:02 +0200 Subject: [PATCH 4/6] add multiple files example --- docs/testclient.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/testclient.md b/docs/testclient.md index f0ca218a9..a3963f9b8 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -44,8 +44,13 @@ And for example to send files with the TestClient: ```python client = TestClient(app) +# Send a single file files = {"file": open("example.txt", "rb")} response = client.post("/form", files=files) + +# Send multiple files +files = {"file1": open("example.txt", "rb"), "file2": open("example.png", "rb")} +response = client.post("/form", files=files) ``` For more information you can view `requests.Session` [docs](https://requests.readthedocs.io/en/master/user/advanced/) and [API](https://requests.readthedocs.io/en/master/api/#requests.Session). From 8d547af8051a39b49fadf482d64e9e747f1e7e32 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sat, 11 Jun 2022 11:14:22 +0200 Subject: [PATCH 5/6] Update --- docs/testclient.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/testclient.md b/docs/testclient.md index a3963f9b8..a65538ba4 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -45,12 +45,14 @@ And for example to send files with the TestClient: client = TestClient(app) # Send a single file -files = {"file": open("example.txt", "rb")} -response = client.post("/form", files=files) +with open("example.txt", "rb") as f: + response = client.post("/form", files={"file": f}) # Send multiple files -files = {"file1": open("example.txt", "rb"), "file2": open("example.png", "rb")} -response = client.post("/form", files=files) +with open("example.txt", "rb") as f1: + with open("example.png", "rb") as f2: + files = {"file1": f1, "file2": ("filename", f2, "image/png")} + response = client.post("/form", files=files) ``` For more information you can view `requests.Session` [docs](https://requests.readthedocs.io/en/master/user/advanced/) and [API](https://requests.readthedocs.io/en/master/api/#requests.Session). From 28072b6466af0348719ea99068f1c4b7e0f15a6b Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 12 Jun 2022 09:12:12 +0200 Subject: [PATCH 6/6] Update docs/testclient.md Co-authored-by: Marcelo Trylesinski --- docs/testclient.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/testclient.md b/docs/testclient.md index a65538ba4..f42093030 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -55,7 +55,7 @@ with open("example.txt", "rb") as f1: response = client.post("/form", files=files) ``` -For more information you can view `requests.Session` [docs](https://requests.readthedocs.io/en/master/user/advanced/) and [API](https://requests.readthedocs.io/en/master/api/#requests.Session). +For more information you can check the `requests` [documentation](https://requests.readthedocs.io/en/master/user/advanced/). By default the `TestClient` will raise any exceptions that occur in the application. Occasionally you might want to test the content of 500 error