Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit dac0eec

Browse files
committed
chore: linting
1 parent e6b667b commit dac0eec

11 files changed

+166
-142
lines changed

kiota_http/middleware/redirect_handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ async def send(
7777
request, f"RedirectHandler_send - redirect {len(history)}"
7878
)
7979
response = await super().send(request, transport)
80-
_redirect_span.set_attribute(
81-
HTTP_RESPONSE_STATUS_CODE, response.status_code
82-
)
80+
_redirect_span.set_attribute(HTTP_RESPONSE_STATUS_CODE, response.status_code)
8381
redirect_location = self.get_redirect_location(response)
8482

8583
if redirect_location and current_options.should_redirect:

kiota_http/middleware/url_replace_handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import httpx
22
from kiota_abstractions.request_option import RequestOption
3-
from opentelemetry.semconv.attributes.url_attributes import (
4-
URL_FULL
5-
)
3+
from opentelemetry.semconv.attributes.url_attributes import (URL_FULL)
64

75
from .middleware import BaseMiddleware
86
from .options import UrlReplaceHandlerOption

tests/conftest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
from .helpers import MockTransport, MockErrorObject, MockResponseObject, OfficeLocation
1515

16+
1617
@pytest.fixture
1718
def sample_headers():
1819
return {"Content-Type": "application/json"}
1920

21+
2022
@pytest.fixture
2123
def auth_provider():
2224
return AnonymousAuthenticationProvider()
@@ -26,6 +28,7 @@ def auth_provider():
2628
def request_info():
2729
return RequestInformation()
2830

31+
2932
@pytest.fixture
3033
def mock_async_transport():
3134
return MockTransport()
@@ -57,27 +60,32 @@ def mock_error_500_map():
5760
"500": Exception("Internal Server Error"),
5861
}
5962

63+
6064
@pytest.fixture
6165
def mock_apierror_map(sample_headers):
6266
return {
6367
"400": APIError("Resource not found", 400, sample_headers),
6468
"500": APIError("Custom Internal Server Error", 500, sample_headers)
6569
}
6670

71+
6772
@pytest.fixture
6873
def mock_apierror_XXX_map(sample_headers):
69-
return {"XXX": APIError("OdataError",400, sample_headers)}
70-
74+
return {"XXX": APIError("OdataError", 400, sample_headers)}
75+
76+
7177
@pytest.fixture
7278
def mock_request_adapter(sample_headers):
7379
resp = httpx.Response(json={'error': 'not found'}, status_code=404, headers=sample_headers)
7480
mock_request_adapter = AsyncMock
7581
mock_request_adapter.get_http_response_message = AsyncMock(return_value=resp)
7682

83+
7784
@pytest.fixture
7885
def simple_error_response(sample_headers):
7986
return httpx.Response(json={'error': 'not found'}, status_code=404, headers=sample_headers)
8087

88+
8189
@pytest.fixture
8290
def simple_success_response(sample_headers):
8391
return httpx.Response(json={'message': 'Success!'}, status_code=200, headers=sample_headers)
@@ -153,9 +161,7 @@ def mock_users_response(mocker):
153161

154162
@pytest.fixture
155163
def mock_primitive_collection_response(sample_headers):
156-
return httpx.Response(
157-
200, json=[12.1, 12.2, 12.3, 12.4, 12.5], headers=sample_headers
158-
)
164+
return httpx.Response(200, json=[12.1, 12.2, 12.3, 12.4, 12.5], headers=sample_headers)
159165

160166

161167
@pytest.fixture

tests/helpers/mock_async_transport.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import httpx
22

3+
34
class MockTransport():
5+
46
async def handle_async_request(self, request):
5-
return httpx.Response(200, request=request, content=b'Hello World', headers={"Content-Type": "application/json", "test": "test_response_header"})
7+
return httpx.Response(
8+
200,
9+
request=request,
10+
content=b'Hello World',
11+
headers={
12+
"Content-Type": "application/json",
13+
"test": "test_response_header"
14+
}
15+
)

tests/middleware_tests/test_base_middleware.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def test_next_is_none():
99
middleware = BaseMiddleware()
1010
assert middleware.next is None
1111

12+
1213
def test_span_created(request_info):
1314
"""Ensures the current span is returned and the parent_span is not set."""
1415
middleware = BaseMiddleware()

tests/middleware_tests/test_headers_inspection_handler.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,42 @@ def test_custom_config():
2626

2727
options = HeadersInspectionHandlerOption(inspect_request_headers=False)
2828
assert not options.inspect_request_headers
29-
30-
29+
30+
3131
def test_headers_inspection_handler_construction():
3232
"""
3333
Ensures the Header Inspection handler instance is set.
3434
"""
3535
handler = HeadersInspectionHandler()
3636
assert handler
37-
37+
38+
3839
@pytest.mark.asyncio
3940
async def test_headers_inspection_handler_gets_headers():
41+
4042
def request_handler(request: httpx.Request):
4143
return httpx.Response(
42-
200,
43-
json={"text": "Hello, world!"},
44-
headers={'test_response': 'test_response_header'}
44+
200, json={"text": "Hello, world!"}, headers={'test_response': 'test_response_header'}
4545
)
46+
4647
handler = HeadersInspectionHandler()
47-
48+
4849
# First request
4950
request = httpx.Request(
50-
'GET',
51-
'https://localhost',
52-
headers={'test_request': 'test_request_header'}
51+
'GET', 'https://localhost', headers={'test_request': 'test_request_header'}
5352
)
5453
mock_transport = httpx.MockTransport(request_handler)
5554
resp = await handler.send(request, mock_transport)
5655
assert resp.status_code == 200
5756
assert handler.options.request_headers.try_get('test_request') == {'test_request_header'}
5857
assert handler.options.response_headers.try_get('test_response') == {'test_response_header'}
59-
58+
6059
# Second request
6160
request2 = httpx.Request(
62-
'GET',
63-
'https://localhost',
64-
headers={'test_request_2': 'test_request_header_2'}
61+
'GET', 'https://localhost', headers={'test_request_2': 'test_request_header_2'}
6562
)
6663
resp = await handler.send(request2, mock_transport)
6764
assert resp.status_code == 200
6865
assert not handler.options.request_headers.try_get('test_request') == {'test_request_header'}
6966
assert handler.options.request_headers.try_get('test_request_2') == {'test_request_header_2'}
7067
assert handler.options.response_headers.try_get('test_response') == {'test_response_header'}
71-
72-

tests/middleware_tests/test_parameters_name_decoding_handler.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from kiota_http.middleware.options import ParametersNameDecodingHandlerOption
66

77
OPTION_KEY = "ParametersNameDecodingHandlerOption"
8+
9+
810
def test_no_config():
911
"""
1012
Test that default values are used if no custom confguration is passed
@@ -19,9 +21,7 @@ def test_custom_options():
1921
"""
2022
Test that default configuration is overrriden if custom configuration is provided
2123
"""
22-
options = ParametersNameDecodingHandlerOption(
23-
enable=False, characters_to_decode=[".", "-"]
24-
)
24+
options = ParametersNameDecodingHandlerOption(enable=False, characters_to_decode=[".", "-"])
2525
handler = ParametersNameDecodingHandler(options)
2626

2727
assert handler.options.enabled is not True
@@ -35,24 +35,40 @@ async def test_decodes_query_parameter_names_only():
3535
Test that only query parameter names are decoded
3636
"""
3737
encoded_decoded = [
38-
("http://localhost?%24select=diplayName&api%2Dversion=2", "http://localhost?$select=diplayName&api-version=2"),
39-
("http://localhost?%24select=diplayName&api%7Eversion=2", "http://localhost?$select=diplayName&api~version=2"),
40-
("http://localhost?%24select=diplayName&api%2Eversion=2", "http://localhost?$select=diplayName&api.version=2"),
41-
("http://localhost:888?%24select=diplayName&api%2Dversion=2", "http://localhost:888?$select=diplayName&api-version=2"),
42-
("http://localhost", "http://localhost"),
43-
("https://google.com/?q=1%2b2", "https://google.com/?q=1%2b2"),
44-
("https://google.com/?q=M%26A", "https://google.com/?q=M%26A"),
45-
("https://google.com/?q=1%2B2", "https://google.com/?q=1%2B2"), # Values are not decoded
46-
("https://google.com/?q=M%26A", "https://google.com/?q=M%26A"), # Values are not decoded
47-
("https://google.com/?q%2D1=M%26A", "https://google.com/?q-1=M%26A"), # Values are not decoded but params are
48-
("https://google.com/?q%2D1&q=M%26A=M%26A", "https://google.com/?q-1&q=M%26A=M%26A"), # Values are not decoded but params are
49-
("https://graph.microsoft.com?%24count=true&query=%24top&created%2Din=2022-10-05&q=1%2b2&q2=M%26A&subject%2Ename=%7eWelcome&%24empty",
50-
"https://graph.microsoft.com?$count=true&query=%24top&created-in=2022-10-05&q=1%2b2&q2=M%26A&subject.name=%7eWelcome&$empty")
38+
(
39+
"http://localhost?%24select=diplayName&api%2Dversion=2",
40+
"http://localhost?$select=diplayName&api-version=2"
41+
),
42+
(
43+
"http://localhost?%24select=diplayName&api%7Eversion=2",
44+
"http://localhost?$select=diplayName&api~version=2"
45+
),
46+
(
47+
"http://localhost?%24select=diplayName&api%2Eversion=2",
48+
"http://localhost?$select=diplayName&api.version=2"
49+
),
50+
(
51+
"http://localhost:888?%24select=diplayName&api%2Dversion=2",
52+
"http://localhost:888?$select=diplayName&api-version=2"
53+
),
54+
("http://localhost", "http://localhost"),
55+
("https://google.com/?q=1%2b2", "https://google.com/?q=1%2b2"),
56+
("https://google.com/?q=M%26A", "https://google.com/?q=M%26A"),
57+
("https://google.com/?q=1%2B2", "https://google.com/?q=1%2B2"), # Values are not decoded
58+
("https://google.com/?q=M%26A", "https://google.com/?q=M%26A"), # Values are not decoded
59+
("https://google.com/?q%2D1=M%26A",
60+
"https://google.com/?q-1=M%26A"), # Values are not decoded but params are
61+
("https://google.com/?q%2D1&q=M%26A=M%26A",
62+
"https://google.com/?q-1&q=M%26A=M%26A"), # Values are not decoded but params are
63+
(
64+
"https://graph.microsoft.com?%24count=true&query=%24top&created%2Din=2022-10-05&q=1%2b2&q2=M%26A&subject%2Ename=%7eWelcome&%24empty",
65+
"https://graph.microsoft.com?$count=true&query=%24top&created-in=2022-10-05&q=1%2b2&q2=M%26A&subject.name=%7eWelcome&$empty"
66+
)
5167
]
52-
68+
5369
def request_handler(request: httpx.Request):
5470
return httpx.Response(200, json={"text": "Hello, world!"})
55-
71+
5672
handler = ParametersNameDecodingHandler()
5773
for encoded, decoded in encoded_decoded:
5874
request = httpx.Request('GET', encoded)

0 commit comments

Comments
 (0)