Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 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,7 +78,7 @@ def _mock_request_method(
additional_matcher=self._matches_wrapper(matcher),
response_list=[
{
"text": response.body,
"text" if isinstance(response.body, str) else "content": response.body,
"status_code": response.status_code,
"headers": response.headers,
}
Expand All @@ -98,6 +99,11 @@ 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
6 changes: 3 additions & 3 deletions airbyte_cdk/test/mock_http/response.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# 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