Skip to content

Commit

Permalink
tests - httpx_mock changes
Browse files Browse the repository at this point in the history
  • Loading branch information
commonism committed Oct 3, 2024
1 parent 22675bc commit 5735051
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 8 additions & 0 deletions tests/path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def test_operation_populated(openapi_version, petstore_expanded):
assert type(con2.schema_._target) == openapi_version.schema


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_security(httpx_mock, with_paths_security):
api = OpenAPI(URLBASE, with_paths_security, session_factory=httpx.Client, use_operation_tags=False)
httpx_mock.add_response(headers={"Content-Type": "application/json"}, json="user")
Expand Down Expand Up @@ -227,6 +228,7 @@ def test_paths_parameters_invalid(with_paths_parameters_invalid):
OpenAPI(URLBASE, with_paths_parameters_invalid, session_factory=httpx.Client)


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_parameters_oneOf(httpx_mock, with_paths_parameters_oneOf):
api = OpenAPI(URLBASE, with_paths_parameters_oneOf, session_factory=httpx.Client)
httpx_mock.add_response(headers={"Content-Type": "application/json"}, json="test")
Expand Down Expand Up @@ -266,6 +268,7 @@ def test_paths_parameter_default(httpx_mock, with_paths_parameter_default):
assert u.parts[3] == "path"


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_parameter_format(httpx_mock, with_paths_parameter_format):
httpx_mock.add_response(headers={"Content-Type": "application/json"}, json="test")
api = OpenAPI(URLBASE, with_paths_parameter_format, session_factory=httpx.Client)
Expand Down Expand Up @@ -389,6 +392,7 @@ def test_paths_parameter_format(httpx_mock, with_paths_parameter_format):
return


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_parameter_format_complex(httpx_mock, with_paths_parameter_format_complex):
httpx_mock.add_response(headers={"Content-Type": "application/json"}, json="test")

Expand Down Expand Up @@ -423,6 +427,7 @@ def test_paths_response_header(httpx_mock, with_paths_response_header):
return


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_response_content_type_octet(httpx_mock, with_paths_response_content_type_octet):
CONTENT = b"\x00\x11"
httpx_mock.add_response(headers={"Content-Type": "application/octet-stream", "X-required": "1"}, content=CONTENT)
Expand All @@ -437,6 +442,7 @@ def test_paths_response_content_type_octet(httpx_mock, with_paths_response_conte
request = httpx_mock.get_requests()[-1]


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_tags(httpx_mock, with_paths_tags):
import copy

Expand Down Expand Up @@ -513,6 +519,7 @@ def test_paths_response_error(httpx_mock, with_paths_response_error):
api._.test()


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
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 Expand Up @@ -560,6 +567,7 @@ def test_paths_servers(httpx_mock, with_paths_servers):
return


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_server_variables(httpx_mock, with_paths_server_variables):
api = OpenAPI("http://example/openapi.yaml", with_paths_server_variables, session_factory=httpx.Client)
assert api.url.host == "default"
Expand Down
25 changes: 21 additions & 4 deletions tests/pathv20_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_paths_security_v20_url(with_paths_security_v20):
assert str(api.url) == "https://api.example.com/v1"


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_security_v20_securityparameters(httpx_mock, with_paths_security_v20):
api = OpenAPI(URLBASE, with_paths_security_v20, session_factory=httpx.Client)
user = api._.createUser.return_value().get_type().model_construct(name="test", id=1)
Expand Down Expand Up @@ -92,6 +93,7 @@ def test_paths_security_v20_alternate_securityparameters(httpx_mock, with_paths_
api._.alternateSecurity(data={}, parameters={})


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_security_v20_post_body(httpx_mock, with_paths_security_v20):
auth = str(uuid.uuid4())
api = OpenAPI(URLBASE, with_paths_security_v20, session_factory=httpx.Client)
Expand Down Expand Up @@ -147,6 +149,7 @@ def test_paths_response_header_v20(httpx_mock, with_paths_response_header_v20):
return


@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
def test_paths_parameter_format_v20(httpx_mock, with_paths_parameter_format_v20):
httpx_mock.add_response(headers={"Content-Type": "application/json"}, json="ok")
api = OpenAPI(URLBASE, with_paths_parameter_format_v20, session_factory=httpx.Client)
Expand All @@ -173,16 +176,30 @@ def test_paths_parameter_format_v20(httpx_mock, with_paths_parameter_format_v20)
params["file1"] = ("file1name", io.BytesIO(b"y"), "ct")
result = api._.formdata(parameters=params)
request = httpx_mock.get_requests()[-1]

import multipart
from httpx._multipart import MultipartStream

files = dict()

def on_file(file):
file.file_object.seek(0)
files[file.field_name] = (file.file_name.decode(), file.file_object)

multipart.parse_form(request.headers, io.BytesIO(request.content), None, on_file)

ms = MultipartStream({}, files=files)

assert (
(f := request.stream.fields[0]) is not None
(f := ms.fields[0]) is not None
and f.filename == "file0name"
and f.headers["Content-Type"] == "ct"
# and f.headers["Content-Type"] == "ct"
and f.file.read() == b"x"
)
assert (
(f := request.stream.fields[1]) is not None
(f := ms.fields[1]) is not None
and f.filename == "file1name"
and f.headers["Content-Type"] == "ct"
# and f.headers["Content-Type"] == "ct"
and f.file.read() == b"y"
)
assert result == "ok"
Expand Down

0 comments on commit 5735051

Please sign in to comment.