Skip to content

Commit

Permalink
tests/v30 - explicit test for various request/response error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
commonism committed Dec 7, 2023
1 parent ec1e0e3 commit 38ef3b2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,8 @@ def with_paths_servers(openapi_version):
@pytest.fixture
def with_paths_server_variables(openapi_version):
yield _get_parsed_yaml("paths-server-variables.yaml", openapi_version)


@pytest.fixture
def with_paths_response_error():
yield _get_parsed_yaml("paths-response-error.yaml")
22 changes: 22 additions & 0 deletions tests/fixtures/paths-response-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.1.0
info:
title: response error
version: 0.0.0
servers:
- url: http://127.0.0.1/api

security:
- {}

paths:
/test:
get:
operationId: test
responses:
"200":
description: "ok"
content:
application/json:
schema:
type: string
const: ok
26 changes: 26 additions & 0 deletions tests/path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,32 @@ def test_paths_response_status_pattern_default(httpx_mock, with_paths_response_s
api._.test()


def test_paths_response_error(httpx_mock, with_paths_response_error):
from aiopenapi3 import ResponseSchemaError, ContentTypeError, HTTPStatusError, ResponseDecodingError

api = OpenAPI("/", with_paths_response_error, session_factory=httpx.Client)

httpx_mock.add_response(headers={"Content-Type": "application/json"}, status_code=200, json="ok")
r = api._.test()
assert r == "ok"

httpx_mock.add_response(headers={"Content-Type": "text/html"}, status_code=200, json="ok")
with pytest.raises(ContentTypeError):
api._.test()

httpx_mock.add_response(headers={"Content-Type": "application/json"}, status_code=201, json="ok")
with pytest.raises(HTTPStatusError):
api._.test()

httpx_mock.add_response(headers={"Content-Type": "application/json"}, status_code=200, content="'")
with pytest.raises(ResponseDecodingError):
api._.test()

httpx_mock.add_response(headers={"Content-Type": "application/json"}, status_code=200, json="fail")
with pytest.raises(ResponseSchemaError):
api._.test()


def test_paths_request_calling(httpx_mock, with_paths_response_status_pattern_default):
api = OpenAPI("/", with_paths_response_status_pattern_default, session_factory=httpx.Client)

Expand Down

0 comments on commit 38ef3b2

Please sign in to comment.