mirror of https://github.com/tiangolo/fastapi.git
Add test for JSON decode error handling in long documents with early errors.
This commit is contained in:
parent
ac50938ea1
commit
7a8da3a4ad
|
|
@ -121,3 +121,21 @@ def test_json_decode_error_unclosed_brace():
|
|||
assert "column" in error["msg"].lower()
|
||||
assert error["type"] == "json_invalid"
|
||||
assert "position" in error["ctx"]
|
||||
|
||||
|
||||
def test_json_decode_error_in_middle_of_long_document():
|
||||
# Create a JSON with error early in a long document (need >40 chars after error)
|
||||
# The error is at position for "invalid" which needs at least 41 chars after it
|
||||
long_json = '{"field": invalid, "padding_field": "this needs to be long enough that we have more than forty characters after the error position"}'
|
||||
|
||||
response = client.post(
|
||||
"/items/", content=long_json, headers={"Content-Type": "application/json"}
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
error = response.json()["detail"][0]
|
||||
|
||||
# The error snippet should have "..." at the end since error is early in doc
|
||||
assert error["input"].endswith("...")
|
||||
assert "invalid" in error["input"]
|
||||
assert error["type"] == "json_invalid"
|
||||
|
|
|
|||
Loading…
Reference in New Issue