Skip to content

Commit 39862fa

Browse files
committed
Add unit tests for HTTPClientFactory
1 parent 6d22aaf commit 39862fa

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tests/unit/test_client_factory.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import ssl
2+
3+
import pytest
4+
from requests import Session
5+
from requests.adapters import HTTPAdapter
6+
7+
from msgraphcore.client_factory import HTTPClientFactory
8+
from msgraphcore.constants import CONNECTION_TIMEOUT, REQUEST_TIMEOUT
9+
from msgraphcore.enums import APIVersion, NationalClouds
10+
from msgraphcore.middleware.authorization import AuthorizationHandler
11+
from msgraphcore.middleware.middleware import BaseMiddleware, MiddlewarePipeline
12+
13+
14+
def test_initialize_with_default_config():
15+
"""Test creation of HTTP Client will use the default configuration
16+
if none are passed"""
17+
client = HTTPClientFactory()
18+
19+
assert client.api_version == APIVersion.v1
20+
assert client.endpoint == NationalClouds.Global
21+
assert client.timeout == (CONNECTION_TIMEOUT, REQUEST_TIMEOUT)
22+
assert isinstance(client.session, Session)
23+
24+
25+
def test_initialize_with_custom_config():
26+
"""Test creation of HTTP Client will use custom configuration if they are passed"""
27+
client = HTTPClientFactory(api_version=APIVersion.beta, timeout=(5, 5))
28+
29+
assert client.api_version == APIVersion.beta
30+
assert client.endpoint == NationalClouds.Global
31+
assert client.timeout == (5, 5)
32+
assert isinstance(client.session, Session)
33+
34+
35+
def test_create_with_default_middleware():
36+
"""Test creation of HTTP Client using default middleware"""
37+
credential = _CustomTokenCredential()
38+
client = HTTPClientFactory().create_with_default_middleware(credential=credential)
39+
middleware = client.get_adapter('https://')
40+
41+
assert isinstance(middleware, HTTPAdapter)
42+
43+
44+
def test_create_with_custom_middleware():
45+
"""Test creation of HTTP Clients with custom middleware"""
46+
credential = _CustomTokenCredential()
47+
middleware = [
48+
AuthorizationHandler(credential),
49+
]
50+
client = HTTPClientFactory().create_with_custom_middleware(middleware=middleware)
51+
custom_middleware = client.get_adapter('https://')
52+
53+
assert isinstance(custom_middleware, HTTPAdapter)
54+
55+
56+
def test_get_base_url():
57+
"""
58+
Test base url is formed by combining the national cloud endpoint with
59+
Api version
60+
"""
61+
client = HTTPClientFactory(api_version=APIVersion.beta, cloud=NationalClouds.Germany)
62+
assert client.session.base_url == client.endpoint + '/' + client.api_version
63+
64+
65+
def test_register_middleware():
66+
credential = _CustomTokenCredential()
67+
middleware = [
68+
AuthorizationHandler(credential),
69+
]
70+
client = HTTPClientFactory()
71+
client._register(middleware)
72+
73+
assert isinstance(client.session.get_adapter('https://'), HTTPAdapter)
74+
75+
76+
class _CustomTokenCredential:
77+
def get_token(self, scopes):
78+
return ['{token:https://graph.microsoft.com/}']

0 commit comments

Comments
 (0)