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(
"/v1.0/post_wrong_content_type",
data="<html></html>",
content="<html></html>",
headers={"content-type": "text/html"},
)
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]
url = "/v1.0/test_array_csv_form_param"
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
assert array_response == ["D", "E", "F"]
url = "/v1.0/test_array_pipes_form_param"
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
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"}
resp = app_client.post(
"/v1.0/body-not-allowed-additional-properties",
data=json.dumps(body),
headers={"Content-Type": "application/json"},
json=body,
)
assert resp.status_code == 400
@@ -395,14 +394,14 @@ def test_nullable_parameter(simple_app):
assert resp.json() == "it was None"
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"
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"
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"
@@ -421,8 +420,7 @@ def test_args_kwargs(simple_app):
body = {"foo": "a", "bar": "b"}
resp = app_client.post(
"/v1.0/body-params-as-kwargs",
data=json.dumps(body),
headers={"Content-Type": "application/json"},
json=body,
)
assert resp.status_code == 200
# 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"}
resp = app_client.post(
"/v1.0/body-sanitization",
data=json.dumps(body),
json=body,
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200
@@ -458,7 +456,7 @@ def test_param_sanitization(simple_app):
body = {"body1": "bodyString", "body2": 12, "body3": {"a": "otherString"}}
resp = app_client.post(
"/v1.0/body-sanitization-additional-properties",
data=json.dumps(body),
json=body,
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200
@@ -471,7 +469,7 @@ def test_param_sanitization(simple_app):
}
resp = app_client.post(
"/v1.0/body-sanitization-additional-properties-defined",
data=json.dumps(body),
json=body,
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200
@@ -501,25 +499,25 @@ def test_parameters_snake_case(snake_case_app):
resp = app_client.post(
"/v1.0/test-post-path-snake/123",
headers=headers,
data=json.dumps({"a": "test"}),
json={"a": "test"},
)
assert resp.status_code == 200
resp = app_client.post(
"/v1.0/test-post-path-shadow/123",
headers=headers,
data=json.dumps({"a": "test"}),
json={"a": "test"},
)
assert resp.status_code == 200
resp = app_client.post(
"/v1.0/test-post-query-snake?someId=123",
headers=headers,
data=json.dumps({"a": "test"}),
json={"a": "test"},
)
assert resp.status_code == 200
resp = app_client.post(
"/v1.0/test-post-query-shadow?id=123&class=header",
headers=headers,
data=json.dumps({"a": "test"}),
json={"a": "test"},
)
assert resp.status_code == 200
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()
resp = app_client.post(
"/v1.0/test-empty-object-body",
data=json.dumps({}),
headers={"Content-Type": "application/json"},
json={},
)
assert resp.status_code == 200
response = resp.json()
@@ -271,7 +270,7 @@ def test_nested_additional_properties(simple_openapi_app):
app_client = simple_openapi_app.test_client()
resp = app_client.post(
"/v1.0/test-nested-additional-properties",
data=json.dumps({"nested": {"object": True}}),
json={"nested": {"object": True}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200
@@ -356,7 +355,7 @@ def test_bad_operations(bad_operations_app):
def test_text_request(simple_app):
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
@@ -371,21 +370,21 @@ def test_post_wrong_content_type(simple_app):
resp = app_client.post(
"/v1.0/post_wrong_content_type",
headers={"content-type": "application/xml"},
data=json.dumps({"some": "data"}),
json={"some": "data"},
)
assert resp.status_code == 415
resp = app_client.post(
"/v1.0/post_wrong_content_type",
headers={"content-type": "application/x-www-form-urlencoded"},
data="a=1&b=2",
content="a=1&b=2",
)
assert resp.status_code == 415
resp = app_client.post(
"/v1.0/post_wrong_content_type",
headers={"content-type": "application/json"},
data="not a valid json",
content="not a valid json",
)
assert (
resp.status_code == 400
@@ -429,12 +428,9 @@ def test_streaming_response(simple_app):
def test_oneof(simple_openapi_app):
app_client = simple_openapi_app.test_client()
headers = {"Content-type": "application/json"}
post_greeting = app_client.post(
"/v1.0/oneof_greeting",
data=json.dumps({"name": 3}),
headers=headers,
json={"name": 3},
)
assert post_greeting.status_code == 200
assert post_greeting.headers.get("content-type") == "application/json"
@@ -443,8 +439,7 @@ def test_oneof(simple_openapi_app):
post_greeting = app_client.post(
"/v1.0/oneof_greeting",
data=json.dumps({"name": True}),
headers=headers,
json={"name": True},
)
assert post_greeting.status_code == 200
assert post_greeting.headers.get("content-type") == "application/json"
@@ -453,7 +448,6 @@ def test_oneof(simple_openapi_app):
post_greeting = app_client.post(
"/v1.0/oneof_greeting",
data=json.dumps({"name": "jsantos"}),
headers=headers,
json={"name": "jsantos"},
)
assert post_greeting.status_code == 400

View File

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