From d752efdff426d779919ccabd7bfa85b8019ec255 Mon Sep 17 00:00:00 2001 From: Patrick Wang Date: Wed, 20 Jan 2021 15:30:15 -0500 Subject: [PATCH] Add test for handling various Content-Type headers --- .../test_body/test_tutorial001.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_tutorial/test_body/test_tutorial001.py b/tests/test_tutorial/test_body/test_tutorial001.py index 382b57650916d..f50e415cf6644 100644 --- a/tests/test_tutorial/test_body/test_tutorial001.py +++ b/tests/test_tutorial/test_body/test_tutorial001.py @@ -188,3 +188,37 @@ def test_post_broken_body(): response = client.post("/items/", json={"test": "test2"}) assert response.status_code == 400, response.text assert response.json() == {"detail": "There was an error parsing the body"} + + +def test_explicit_content_type(): + data = '{"name": "Foo", "price": 50.5}' + response = client.post( + "/items/", data=data, headers={"Content-Type": "applications/json"} + ) + assert response.status_code == 200, response.text + + data = '{"name": "Foo", "price": 50.5}' + response = client.post( + "/items/", data=data, headers={"Content-Type": "applications/geo+json"} + ) + assert response.status_code == 200, response.text + + invalid_dict = { + "detail": [ + { + "loc": ["body"], + "msg": "value is not a valid dict", + "type": "type_error.dict", + } + ] + } + + response = client.post("/items/", data=data, headers={"Content-Type": "text/plain"}) + assert response.status_code == 422, response.text + assert response.json() == invalid_dict + + response = client.post( + "/items/", data=data, headers={"Content-Type": "application/geo+json-seq"} + ) + assert response.status_code == 422, response.text + assert response.json() == invalid_dict