Skip to content

feat(HttpMocker): adding support for PUT requests and bytes responses #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion airbyte_cdk/test/mock_http/mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SupportedHttpMethods(str, Enum):
GET = "get"
PATCH = "patch"
POST = "post"
PUT = "put"
DELETE = "delete"


Expand Down Expand Up @@ -77,14 +78,18 @@ def _mock_request_method(
additional_matcher=self._matches_wrapper(matcher),
response_list=[
{
"text": response.body,
self._get_body_field(response): response.body,
"status_code": response.status_code,
"headers": response.headers,
}
for response in responses
],
)

@staticmethod
def _get_body_field(response: HttpResponse) -> str:
return "text" if isinstance(response.body, str) else "content"

def get(self, request: HttpRequest, responses: Union[HttpResponse, List[HttpResponse]]) -> None:
self._mock_request_method(SupportedHttpMethods.GET, request, responses)

Expand All @@ -98,6 +103,9 @@ def post(
) -> None:
self._mock_request_method(SupportedHttpMethods.POST, request, responses)

def put(self, request: HttpRequest, responses: Union[HttpResponse, List[HttpResponse]]) -> None:
self._mock_request_method(SupportedHttpMethods.PUT, request, responses)

def delete(
self, request: HttpRequest, responses: Union[HttpResponse, List[HttpResponse]]
) -> None:
Expand Down
9 changes: 6 additions & 3 deletions airbyte_cdk/test/mock_http/response.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.

from types import MappingProxyType
from typing import Mapping
from typing import Mapping, Union


class HttpResponse:
def __init__(
self, body: str, status_code: int = 200, headers: Mapping[str, str] = MappingProxyType({})
self,
body: Union[str, bytes],
status_code: int = 200,
headers: Mapping[str, str] = MappingProxyType({}),
):
self._body = body
self._status_code = status_code
self._headers = headers

@property
def body(self) -> str:
def body(self) -> Union[str, bytes]:
return self._body

@property
Expand Down
30 changes: 29 additions & 1 deletion unit_tests/test/mock_http/test_mocker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.

import gzip
from unittest import TestCase

import pytest
Expand Down Expand Up @@ -75,6 +75,34 @@ def test_given_post_request_match_when_decorate_then_return_response(self, http_
assert response.text == _A_RESPONSE_BODY
assert response.status_code == 474

@HttpMocker()
def test_given_put_request_match_when_decorate_then_return_response(self, http_mocker):
http_mocker.put(
HttpRequest(_A_URL, _SOME_QUERY_PARAMS, _SOME_HEADERS, _SOME_REQUEST_BODY_STR),
HttpResponse(_A_RESPONSE_BODY, 474),
)

response = requests.put(
_A_URL, params=_SOME_QUERY_PARAMS, headers=_SOME_HEADERS, data=_SOME_REQUEST_BODY_STR
)

assert response.text == _A_RESPONSE_BODY
assert response.status_code == 474

@HttpMocker()
def test_given_response_in_bytes_when_decorate_then_match(self, http_mocker):
response_content = gzip.compress(bytes("This is the response within the gzip", "utf-8"))
http_mocker.put(
HttpRequest(_A_URL, _SOME_QUERY_PARAMS, _SOME_HEADERS, _SOME_REQUEST_BODY_STR),
HttpResponse(response_content, 474),
)

response = requests.put(
_A_URL, params=_SOME_QUERY_PARAMS, headers=_SOME_HEADERS, data=_SOME_REQUEST_BODY_STR
)

assert response.content == response_content

@HttpMocker()
def test_given_multiple_responses_when_decorate_get_request_then_return_response(
self, http_mocker
Expand Down
Loading