|
10 | 10 | from msgraph.core._constants import DEFAULT_CONNECTION_TIMEOUT, DEFAULT_REQUEST_TIMEOUT |
11 | 11 | from msgraph.core.middleware import GraphAuthorizationHandler |
12 | 12 | from msgraph.core.middleware.middleware import GraphMiddlewarePipeline |
| 13 | +from msgraph.core.middleware.redirect import GraphRedirectHandler |
| 14 | +from msgraph.core.middleware.retry import GraphRetryHandler |
| 15 | +from msgraph.core.middleware.telemetry import GraphTelemetryHandler |
13 | 16 |
|
14 | 17 |
|
15 | | -def test_initialize_with_custom_config(): |
16 | | - """Test creation of HTTP Client will use custom configuration if they are passed""" |
17 | | - client = GraphClientFactory( |
18 | | - api_version=APIVersion.beta, base_url=NationalClouds.Global, timeout=(5, 5), client=None |
19 | | - ) |
| 18 | +def test_create_with_default_middleware_no_auth_provider(): |
| 19 | + """Test creation of GraphClient without a token provider does not |
| 20 | + add the Authorization middleware""" |
| 21 | + client = GraphClientFactory.create_with_default_middleware(client=httpx.AsyncClient()) |
20 | 22 |
|
21 | | - assert client.api_version == APIVersion.beta |
22 | | - assert client.base_url == NationalClouds.Global |
23 | | - assert client.timeout == (5, 5) |
24 | | - assert not isinstance(client.client, httpx.AsyncClient) |
| 23 | + assert isinstance(client, httpx.AsyncClient) |
| 24 | + assert isinstance(client._transport, AsyncKiotaTransport) |
| 25 | + pipeline = client._transport.middleware |
| 26 | + assert isinstance(pipeline, GraphMiddlewarePipeline) |
| 27 | + assert not isinstance(pipeline._first_middleware, GraphAuthorizationHandler) |
25 | 28 |
|
26 | 29 |
|
27 | 30 | def test_create_with_default_middleware(mock_token_provider): |
28 | | - """Test creation of GraphClient using default middleware""" |
29 | | - client = GraphClientFactory( |
30 | | - api_version=APIVersion.beta, |
31 | | - timeout=httpx.Timeout( |
32 | | - 5, |
33 | | - connect=5, |
34 | | - ), |
35 | | - base_url=NationalClouds.Global, |
36 | | - client=None |
37 | | - ).create_with_default_middleware(token_provider=mock_token_provider) |
| 31 | + """Test creation of GraphClient using default middleware and passing a token |
| 32 | + provider adds Authorization middleware""" |
| 33 | + client = GraphClientFactory.create_with_default_middleware( |
| 34 | + client=httpx.AsyncClient(), token_provider=mock_token_provider |
| 35 | + ) |
38 | 36 |
|
39 | 37 | assert isinstance(client, httpx.AsyncClient) |
40 | 38 | assert isinstance(client._transport, AsyncKiotaTransport) |
41 | | - assert str(client.base_url) == f'{NationalClouds.Global}/{APIVersion.beta}/' |
| 39 | + pipeline = client._transport.middleware |
| 40 | + assert isinstance(pipeline, GraphMiddlewarePipeline) |
| 41 | + assert isinstance(pipeline._first_middleware, GraphAuthorizationHandler) |
42 | 42 |
|
43 | 43 |
|
44 | 44 | def test_create_with_custom_middleware(mock_token_provider): |
45 | 45 | """Test creation of HTTP Clients with custom middleware""" |
46 | 46 | middleware = [ |
47 | | - GraphAuthorizationHandler(mock_token_provider), |
| 47 | + GraphTelemetryHandler(), |
48 | 48 | ] |
49 | | - client = GraphClientFactory( |
50 | | - api_version=APIVersion.v1, |
51 | | - timeout=httpx.Timeout( |
52 | | - 5, |
53 | | - connect=5, |
54 | | - ), |
55 | | - base_url=NationalClouds.Global, |
56 | | - client=None |
57 | | - ).create_with_custom_middleware(middleware=middleware) |
| 49 | + client = GraphClientFactory.create_with_custom_middleware( |
| 50 | + client=httpx.AsyncClient(), middleware=middleware |
| 51 | + ) |
58 | 52 |
|
59 | 53 | assert isinstance(client, httpx.AsyncClient) |
60 | 54 | assert isinstance(client._transport, AsyncKiotaTransport) |
61 | | - assert str(client.base_url) == f'{NationalClouds.Global}/{APIVersion.v1}/' |
| 55 | + pipeline = client._transport.middleware |
| 56 | + assert isinstance(pipeline._first_middleware, GraphTelemetryHandler) |
62 | 57 |
|
63 | 58 |
|
64 | | -def test_get_base_url(): |
65 | | - """ |
66 | | - Test base url is formed by combining the national cloud endpoint with |
67 | | - Api version |
68 | | - """ |
69 | | - client = GraphClientFactory( |
70 | | - api_version=APIVersion.beta, |
71 | | - base_url=NationalClouds.Germany, |
72 | | - timeout=httpx.Timeout( |
73 | | - 5, |
74 | | - connect=5, |
75 | | - ), |
76 | | - client=None |
77 | | - ) |
78 | | - assert client._get_base_url() == f'{NationalClouds.Germany}/{APIVersion.beta}' |
79 | | - |
80 | | - |
81 | | -def test_get_default_middleware(mock_token_provider): |
82 | | - client = GraphClientFactory( |
83 | | - api_version=APIVersion.beta, |
84 | | - base_url=NationalClouds.Germany, |
85 | | - timeout=httpx.Timeout( |
86 | | - 5, |
87 | | - connect=5, |
88 | | - ), |
89 | | - client=None |
90 | | - ) |
91 | | - middleware = client._get_default_middleware(mock_token_provider, httpx.AsyncClient()._transport) |
| 59 | +def test_get_common_middleware(): |
| 60 | + middleware = GraphClientFactory._get_common_middleware() |
92 | 61 |
|
93 | | - assert isinstance(middleware, GraphMiddlewarePipeline) |
| 62 | + assert len(middleware) == 3 |
| 63 | + assert isinstance(middleware[0], GraphRedirectHandler) |
| 64 | + assert isinstance(middleware[1], GraphRetryHandler) |
| 65 | + assert isinstance(middleware[2], GraphTelemetryHandler) |
0 commit comments