Change deprecated httpx data usage in tests to content

This commit is contained in:
Robbe Sneyders
2023-02-16 09:15:02 +01:00
parent 816707d469
commit c1ff7641f6
5 changed files with 47 additions and 101 deletions

View File

@@ -82,7 +82,7 @@ def test_errors(problem_app):
unsupported_media_type = app_client.post( unsupported_media_type = app_client.post(
"/v1.0/post_wrong_content_type", "/v1.0/post_wrong_content_type",
data="<html></html>", content="<html></html>",
headers={"content-type": "text/html"}, headers={"content-type": "text/html"},
) )
assert unsupported_media_type.status_code == 415 assert unsupported_media_type.status_code == 415

View File

@@ -89,12 +89,12 @@ def test_array_form_param(simple_app):
assert array_response == [1, 2, 3] assert array_response == [1, 2, 3]
url = "/v1.0/test_array_csv_form_param" url = "/v1.0/test_array_csv_form_param"
data = "items=A&items=B&items=C&items=D,E,F" data = "items=A&items=B&items=C&items=D,E,F"
response = app_client.post(url, headers=headers, data=data) response = app_client.post(url, headers=headers, content=data)
array_response: List[str] = response.json() # multi array with csv format array_response: List[str] = response.json() # multi array with csv format
assert array_response == ["D", "E", "F"] assert array_response == ["D", "E", "F"]
url = "/v1.0/test_array_pipes_form_param" url = "/v1.0/test_array_pipes_form_param"
data = "items=4&items=5&items=6&items=7|8|9" data = "items=4&items=5&items=6&items=7|8|9"
response = app_client.post(url, headers=headers, data=data) response = app_client.post(url, headers=headers, content=data)
array_response: List[int] = response.json() # multi array with pipes format array_response: List[int] = response.json() # multi array with pipes format
assert array_response == [7, 8, 9] assert array_response == [7, 8, 9]
@@ -290,8 +290,7 @@ def test_body_not_allowed_additional_properties(simple_app):
body = {"body1": "bodyString", "additional_property": "test1"} body = {"body1": "bodyString", "additional_property": "test1"}
resp = app_client.post( resp = app_client.post(
"/v1.0/body-not-allowed-additional-properties", "/v1.0/body-not-allowed-additional-properties",
data=json.dumps(body), json=body,
headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 400 assert resp.status_code == 400
@@ -395,14 +394,14 @@ def test_nullable_parameter(simple_app):
assert resp.json() == "it was None" assert resp.json() == "it was None"
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
resp = app_client.put("/v1.0/nullable-parameters", data="null", headers=headers) resp = app_client.put("/v1.0/nullable-parameters", content="null", headers=headers)
assert resp.json() == "it was None" assert resp.json() == "it was None"
resp = app_client.put("/v1.0/nullable-parameters", data="None", headers=headers) resp = app_client.put("/v1.0/nullable-parameters", content="None", headers=headers)
assert resp.json() == "it was None" assert resp.json() == "it was None"
resp = app_client.put( resp = app_client.put(
"/v1.0/nullable-parameters-noargs", data="None", headers=headers "/v1.0/nullable-parameters-noargs", content="None", headers=headers
) )
assert resp.json() == "hello" assert resp.json() == "hello"
@@ -421,8 +420,7 @@ def test_args_kwargs(simple_app):
body = {"foo": "a", "bar": "b"} body = {"foo": "a", "bar": "b"}
resp = app_client.post( resp = app_client.post(
"/v1.0/body-params-as-kwargs", "/v1.0/body-params-as-kwargs",
data=json.dumps(body), json=body,
headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
# having only kwargs, the handler would have been passed 'body' # having only kwargs, the handler would have been passed 'body'
@@ -449,7 +447,7 @@ def test_param_sanitization(simple_app):
body = {"body1": "bodyString", "body2": "otherString"} body = {"body1": "bodyString", "body2": "otherString"}
resp = app_client.post( resp = app_client.post(
"/v1.0/body-sanitization", "/v1.0/body-sanitization",
data=json.dumps(body), json=body,
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
@@ -458,7 +456,7 @@ def test_param_sanitization(simple_app):
body = {"body1": "bodyString", "body2": 12, "body3": {"a": "otherString"}} body = {"body1": "bodyString", "body2": 12, "body3": {"a": "otherString"}}
resp = app_client.post( resp = app_client.post(
"/v1.0/body-sanitization-additional-properties", "/v1.0/body-sanitization-additional-properties",
data=json.dumps(body), json=body,
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
@@ -471,7 +469,7 @@ def test_param_sanitization(simple_app):
} }
resp = app_client.post( resp = app_client.post(
"/v1.0/body-sanitization-additional-properties-defined", "/v1.0/body-sanitization-additional-properties-defined",
data=json.dumps(body), json=body,
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
@@ -501,25 +499,25 @@ def test_parameters_snake_case(snake_case_app):
resp = app_client.post( resp = app_client.post(
"/v1.0/test-post-path-snake/123", "/v1.0/test-post-path-snake/123",
headers=headers, headers=headers,
data=json.dumps({"a": "test"}), json={"a": "test"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = app_client.post( resp = app_client.post(
"/v1.0/test-post-path-shadow/123", "/v1.0/test-post-path-shadow/123",
headers=headers, headers=headers,
data=json.dumps({"a": "test"}), json={"a": "test"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = app_client.post( resp = app_client.post(
"/v1.0/test-post-query-snake?someId=123", "/v1.0/test-post-query-snake?someId=123",
headers=headers, headers=headers,
data=json.dumps({"a": "test"}), json={"a": "test"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = app_client.post( resp = app_client.post(
"/v1.0/test-post-query-shadow?id=123&class=header", "/v1.0/test-post-query-shadow?id=123&class=header",
headers=headers, headers=headers,
data=json.dumps({"a": "test"}), json={"a": "test"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = app_client.get("/v1.0/test-get-path-snake/123") resp = app_client.get("/v1.0/test-get-path-snake/123")

View File

@@ -259,8 +259,7 @@ def test_empty_object_body(simple_app):
app_client = simple_app.test_client() app_client = simple_app.test_client()
resp = app_client.post( resp = app_client.post(
"/v1.0/test-empty-object-body", "/v1.0/test-empty-object-body",
data=json.dumps({}), json={},
headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
response = resp.json() response = resp.json()
@@ -271,7 +270,7 @@ def test_nested_additional_properties(simple_openapi_app):
app_client = simple_openapi_app.test_client() app_client = simple_openapi_app.test_client()
resp = app_client.post( resp = app_client.post(
"/v1.0/test-nested-additional-properties", "/v1.0/test-nested-additional-properties",
data=json.dumps({"nested": {"object": True}}), json={"nested": {"object": True}},
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
assert resp.status_code == 200 assert resp.status_code == 200
@@ -356,7 +355,7 @@ def test_bad_operations(bad_operations_app):
def test_text_request(simple_app): def test_text_request(simple_app):
app_client = simple_app.test_client() app_client = simple_app.test_client()
resp = app_client.post("/v1.0/text-request", data="text") resp = app_client.post("/v1.0/text-request", content="text")
assert resp.status_code == 200 assert resp.status_code == 200
@@ -371,21 +370,21 @@ def test_post_wrong_content_type(simple_app):
resp = app_client.post( resp = app_client.post(
"/v1.0/post_wrong_content_type", "/v1.0/post_wrong_content_type",
headers={"content-type": "application/xml"}, headers={"content-type": "application/xml"},
data=json.dumps({"some": "data"}), json={"some": "data"},
) )
assert resp.status_code == 415 assert resp.status_code == 415
resp = app_client.post( resp = app_client.post(
"/v1.0/post_wrong_content_type", "/v1.0/post_wrong_content_type",
headers={"content-type": "application/x-www-form-urlencoded"}, headers={"content-type": "application/x-www-form-urlencoded"},
data="a=1&b=2", content="a=1&b=2",
) )
assert resp.status_code == 415 assert resp.status_code == 415
resp = app_client.post( resp = app_client.post(
"/v1.0/post_wrong_content_type", "/v1.0/post_wrong_content_type",
headers={"content-type": "application/json"}, headers={"content-type": "application/json"},
data="not a valid json", content="not a valid json",
) )
assert ( assert (
resp.status_code == 400 resp.status_code == 400
@@ -429,12 +428,9 @@ def test_streaming_response(simple_app):
def test_oneof(simple_openapi_app): def test_oneof(simple_openapi_app):
app_client = simple_openapi_app.test_client() app_client = simple_openapi_app.test_client()
headers = {"Content-type": "application/json"}
post_greeting = app_client.post( post_greeting = app_client.post(
"/v1.0/oneof_greeting", "/v1.0/oneof_greeting",
data=json.dumps({"name": 3}), json={"name": 3},
headers=headers,
) )
assert post_greeting.status_code == 200 assert post_greeting.status_code == 200
assert post_greeting.headers.get("content-type") == "application/json" assert post_greeting.headers.get("content-type") == "application/json"
@@ -443,8 +439,7 @@ def test_oneof(simple_openapi_app):
post_greeting = app_client.post( post_greeting = app_client.post(
"/v1.0/oneof_greeting", "/v1.0/oneof_greeting",
data=json.dumps({"name": True}), json={"name": True},
headers=headers,
) )
assert post_greeting.status_code == 200 assert post_greeting.status_code == 200
assert post_greeting.headers.get("content-type") == "application/json" assert post_greeting.headers.get("content-type") == "application/json"
@@ -453,7 +448,6 @@ def test_oneof(simple_openapi_app):
post_greeting = app_client.post( post_greeting = app_client.post(
"/v1.0/oneof_greeting", "/v1.0/oneof_greeting",
data=json.dumps({"name": "jsantos"}), json={"name": "jsantos"},
headers=headers,
) )
assert post_greeting.status_code == 400 assert post_greeting.status_code == 400

View File

@@ -3,11 +3,8 @@ import json
def test_schema(schema_app): def test_schema(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
empty_request = app_client.post( empty_request = app_client.post("/v1.0/test_schema", json={})
"/v1.0/test_schema", headers=headers, data=json.dumps({})
)
assert empty_request.status_code == 400 assert empty_request.status_code == 400
assert empty_request.headers.get("content-type") == "application/problem+json" assert empty_request.headers.get("content-type") == "application/problem+json"
empty_request_response = empty_request.json() empty_request_response = empty_request.json()
@@ -16,18 +13,14 @@ def test_schema(schema_app):
"'image_version' is a required property" "'image_version' is a required property"
) )
bad_type = app_client.post( bad_type = app_client.post("/v1.0/test_schema", json={"image_version": 22})
"/v1.0/test_schema", headers=headers, data=json.dumps({"image_version": 22})
)
assert bad_type.status_code == 400 assert bad_type.status_code == 400
assert bad_type.headers.get("content-type") == "application/problem+json" assert bad_type.headers.get("content-type") == "application/problem+json"
bad_type_response = bad_type.json() bad_type_response = bad_type.json()
assert bad_type_response["title"] == "Bad Request" assert bad_type_response["title"] == "Bad Request"
assert bad_type_response["detail"].startswith("22 is not of type 'string'") assert bad_type_response["detail"].startswith("22 is not of type 'string'")
bad_type_path = app_client.post( bad_type_path = app_client.post("/v1.0/test_schema", json={"image_version": 22})
"/v1.0/test_schema", headers=headers, data=json.dumps({"image_version": 22})
)
assert bad_type_path.status_code == 400 assert bad_type_path.status_code == 400
assert bad_type_path.headers.get("content-type") == "application/problem+json" assert bad_type_path.headers.get("content-type") == "application/problem+json"
bad_type_path_response = bad_type_path.json() bad_type_path_response = bad_type_path.json()
@@ -36,8 +29,7 @@ def test_schema(schema_app):
good_request = app_client.post( good_request = app_client.post(
"/v1.0/test_schema", "/v1.0/test_schema",
headers=headers, json={"image_version": "version"},
data=json.dumps({"image_version": "version"}),
) )
assert good_request.status_code == 200 assert good_request.status_code == 200
good_request_response = good_request.json() good_request_response = good_request.json()
@@ -45,16 +37,13 @@ def test_schema(schema_app):
good_request_extra = app_client.post( good_request_extra = app_client.post(
"/v1.0/test_schema", "/v1.0/test_schema",
headers=headers, json={"image_version": "version", "extra": "stuff"},
data=json.dumps({"image_version": "version", "extra": "stuff"}),
) )
assert good_request_extra.status_code == 200 assert good_request_extra.status_code == 200
good_request_extra_response = good_request.json() good_request_extra_response = good_request.json()
assert good_request_extra_response["image_version"] == "version" assert good_request_extra_response["image_version"] == "version"
wrong_type = app_client.post( wrong_type = app_client.post("/v1.0/test_schema", json=42)
"/v1.0/test_schema", headers=headers, data=json.dumps(42)
)
assert wrong_type.status_code == 400 assert wrong_type.status_code == 400
assert wrong_type.headers.get("content-type") == "application/problem+json" assert wrong_type.headers.get("content-type") == "application/problem+json"
wrong_type_response = wrong_type.json() wrong_type_response = wrong_type.json()
@@ -139,20 +128,15 @@ def test_schema_in_query(schema_app):
def test_schema_list(schema_app): def test_schema_list(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
wrong_type = app_client.post( wrong_type = app_client.post("/v1.0/test_schema_list", json=42)
"/v1.0/test_schema_list", headers=headers, data=json.dumps(42)
)
assert wrong_type.status_code == 400 assert wrong_type.status_code == 400
assert wrong_type.headers.get("content-type") == "application/problem+json" assert wrong_type.headers.get("content-type") == "application/problem+json"
wrong_type_response = wrong_type.json() wrong_type_response = wrong_type.json()
assert wrong_type_response["title"] == "Bad Request" assert wrong_type_response["title"] == "Bad Request"
assert wrong_type_response["detail"].startswith("42 is not of type 'array'") assert wrong_type_response["detail"].startswith("42 is not of type 'array'")
wrong_items = app_client.post( wrong_items = app_client.post("/v1.0/test_schema_list", json=[42])
"/v1.0/test_schema_list", headers=headers, data=json.dumps([42])
)
assert wrong_items.status_code == 400 assert wrong_items.status_code == 400
assert wrong_items.headers.get("content-type") == "application/problem+json" assert wrong_items.headers.get("content-type") == "application/problem+json"
wrong_items_response = wrong_items.json() wrong_items_response = wrong_items.json()
@@ -162,7 +146,6 @@ def test_schema_list(schema_app):
def test_schema_map(schema_app): def test_schema_map(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
valid_object = { valid_object = {
"foo": {"image_version": "string"}, "foo": {"image_version": "string"},
@@ -171,33 +154,26 @@ def test_schema_map(schema_app):
invalid_object = {"foo": 42} invalid_object = {"foo": 42}
wrong_type = app_client.post( wrong_type = app_client.post("/v1.0/test_schema_map", json=42)
"/v1.0/test_schema_map", headers=headers, data=json.dumps(42)
)
assert wrong_type.status_code == 400 assert wrong_type.status_code == 400
assert wrong_type.headers.get("content-type") == "application/problem+json" assert wrong_type.headers.get("content-type") == "application/problem+json"
wrong_type_response = wrong_type.json() wrong_type_response = wrong_type.json()
assert wrong_type_response["title"] == "Bad Request" assert wrong_type_response["title"] == "Bad Request"
assert wrong_type_response["detail"].startswith("42 is not of type 'object'") assert wrong_type_response["detail"].startswith("42 is not of type 'object'")
wrong_items = app_client.post( wrong_items = app_client.post("/v1.0/test_schema_map", json=invalid_object)
"/v1.0/test_schema_map", headers=headers, data=json.dumps(invalid_object)
)
assert wrong_items.status_code == 400 assert wrong_items.status_code == 400
assert wrong_items.headers.get("content-type") == "application/problem+json" assert wrong_items.headers.get("content-type") == "application/problem+json"
wrong_items_response = wrong_items.json() wrong_items_response = wrong_items.json()
assert wrong_items_response["title"] == "Bad Request" assert wrong_items_response["title"] == "Bad Request"
assert wrong_items_response["detail"].startswith("42 is not of type 'object'") assert wrong_items_response["detail"].startswith("42 is not of type 'object'")
right_type = app_client.post( right_type = app_client.post("/v1.0/test_schema_map", json=valid_object)
"/v1.0/test_schema_map", headers=headers, data=json.dumps(valid_object)
)
assert right_type.status_code == 200 assert right_type.status_code == 200
def test_schema_recursive(schema_app): def test_schema_recursive(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
valid_object = { valid_object = {
"children": [ "children": [
@@ -213,37 +189,28 @@ def test_schema_recursive(schema_app):
invalid_object = {"children": [42]} invalid_object = {"children": [42]}
wrong_type = app_client.post( wrong_type = app_client.post("/v1.0/test_schema_recursive", json=42)
"/v1.0/test_schema_recursive", headers=headers, data=json.dumps(42)
)
assert wrong_type.status_code == 400 assert wrong_type.status_code == 400
assert wrong_type.headers.get("content-type") == "application/problem+json" assert wrong_type.headers.get("content-type") == "application/problem+json"
wrong_type_response = wrong_type.json() wrong_type_response = wrong_type.json()
assert wrong_type_response["title"] == "Bad Request" assert wrong_type_response["title"] == "Bad Request"
assert wrong_type_response["detail"].startswith("42 is not of type 'object'") assert wrong_type_response["detail"].startswith("42 is not of type 'object'")
wrong_items = app_client.post( wrong_items = app_client.post("/v1.0/test_schema_recursive", json=invalid_object)
"/v1.0/test_schema_recursive", headers=headers, data=json.dumps(invalid_object)
)
assert wrong_items.status_code == 400 assert wrong_items.status_code == 400
assert wrong_items.headers.get("content-type") == "application/problem+json" assert wrong_items.headers.get("content-type") == "application/problem+json"
wrong_items_response = wrong_items.json() wrong_items_response = wrong_items.json()
assert wrong_items_response["title"] == "Bad Request" assert wrong_items_response["title"] == "Bad Request"
assert wrong_items_response["detail"].startswith("42 is not of type 'object'") assert wrong_items_response["detail"].startswith("42 is not of type 'object'")
right_type = app_client.post( right_type = app_client.post("/v1.0/test_schema_recursive", json=valid_object)
"/v1.0/test_schema_recursive", headers=headers, data=json.dumps(valid_object)
)
assert right_type.status_code == 200 assert right_type.status_code == 200
def test_schema_format(schema_app): def test_schema_format(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
wrong_type = app_client.post( wrong_type = app_client.post("/v1.0/test_schema_format", json="xy")
"/v1.0/test_schema_format", headers=headers, data=json.dumps("xy")
)
assert wrong_type.status_code == 400 assert wrong_type.status_code == 400
assert wrong_type.headers.get("content-type") == "application/problem+json" assert wrong_type.headers.get("content-type") == "application/problem+json"
wrong_type_response = wrong_type.json() wrong_type_response = wrong_type.json()
@@ -253,11 +220,8 @@ def test_schema_format(schema_app):
def test_schema_array(schema_app): def test_schema_array(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
array_request = app_client.post( array_request = app_client.post("/v1.0/schema_array", json=["list", "hello"])
"/v1.0/schema_array", headers=headers, data=json.dumps(["list", "hello"])
)
assert array_request.status_code == 200 assert array_request.status_code == 200
assert array_request.headers.get("content-type") == "application/json" assert array_request.headers.get("content-type") == "application/json"
array_response = array_request.json() array_response = array_request.json()
@@ -268,9 +232,7 @@ def test_schema_int(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"} headers = {"Content-type": "application/json"}
array_request = app_client.post( array_request = app_client.post("/v1.0/schema_int", json=42)
"/v1.0/schema_int", headers=headers, data=json.dumps(42)
)
assert array_request.status_code == 200 assert array_request.status_code == 200
assert array_request.headers.get("content-type") == "application/json" assert array_request.headers.get("content-type") == "application/json"
array_response = array_request.json() # type: list array_response = array_request.json() # type: list
@@ -285,9 +247,6 @@ def test_global_response_definitions(schema_app):
def test_media_range(schema_app): def test_media_range(schema_app):
app_client = schema_app.test_client() app_client = schema_app.test_client()
headers = {"Content-type": "application/json"}
array_request = app_client.post( array_request = app_client.post("/v1.0/media_range", json={})
"/v1.0/media_range", headers=headers, data=json.dumps({})
)
assert array_request.status_code == 200, array_request.text assert array_request.status_code == 200, array_request.text

View File

@@ -39,15 +39,13 @@ def test_validator_map(json_validation_spec_dir, spec):
res = app_client.post( res = app_client.post(
"/v1.0/minlength", "/v1.0/minlength",
data=json.dumps({"foo": "bar"}), json={"foo": "bar"},
headers={"content-type": "application/json"},
) )
assert res.status_code == 200 assert res.status_code == 200
res = app_client.post( res = app_client.post(
"/v1.0/minlength", "/v1.0/minlength",
data=json.dumps({"foo": ""}), json={"foo": ""},
headers={"content-type": "application/json"},
) )
assert res.status_code == 400 assert res.status_code == 400
@@ -69,16 +67,14 @@ def test_readonly(json_validation_spec_dir, spec, app_class):
res = app_client.post( res = app_client.post(
"/v1.0/user", "/v1.0/user",
data=json.dumps({"name": "max", "password": "1234"}), json={"name": "max", "password": "1234"},
headers=headers,
) )
assert res.status_code == 200 assert res.status_code == 200
assert res.json().get("user_id") == 8 assert res.json().get("user_id") == 8
res = app_client.post( res = app_client.post(
"/v1.0/user", "/v1.0/user",
data=json.dumps({"user_id": 9, "name": "max"}), json={"user_id": 9, "name": "max"},
headers=headers,
) )
assert res.status_code == 400 assert res.status_code == 400
@@ -94,8 +90,7 @@ def test_writeonly(json_validation_spec_dir, spec, app_class):
res = app_client.post( res = app_client.post(
"/v1.0/user", "/v1.0/user",
data=json.dumps({"name": "max", "password": "1234"}), json={"name": "max", "password": "1234"},
headers={"content-type": "application/json"},
) )
assert res.status_code == 200 assert res.status_code == 200
assert "password" not in res.json() assert "password" not in res.json()