|
4 | 4 | # ------------------------------------ |
5 | 5 | import httpx |
6 | 6 | import pytest |
7 | | -from kiota_http.middleware import AsyncKiotaTransport |
| 7 | +from kiota_http.middleware import ( |
| 8 | + AsyncKiotaTransport, |
| 9 | + MiddlewarePipeline, |
| 10 | + ParametersNameDecodingHandler, |
| 11 | +) |
8 | 12 |
|
9 | 13 | from msgraph.core import APIVersion, GraphClientFactory, NationalClouds |
10 | | -from msgraph.core._constants import DEFAULT_CONNECTION_TIMEOUT, DEFAULT_REQUEST_TIMEOUT |
11 | | -from msgraph.core.middleware import GraphAuthorizationHandler |
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 | 14 | from msgraph.core.middleware.telemetry import GraphTelemetryHandler |
16 | 15 |
|
17 | 16 |
|
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()) |
| 17 | +def test_create_with_default_middleware(): |
| 18 | + """Test creation of GraphClient using default middleware""" |
| 19 | + client = GraphClientFactory.create_with_default_middleware() |
22 | 20 |
|
23 | 21 | assert isinstance(client, httpx.AsyncClient) |
24 | 22 | assert isinstance(client._transport, AsyncKiotaTransport) |
25 | | - pipeline = client._transport.middleware |
26 | | - assert isinstance(pipeline, GraphMiddlewarePipeline) |
27 | | - assert not isinstance(pipeline._first_middleware, GraphAuthorizationHandler) |
| 23 | + pipeline = client._transport.pipeline |
| 24 | + assert isinstance(pipeline, MiddlewarePipeline) |
| 25 | + assert isinstance(pipeline._first_middleware, ParametersNameDecodingHandler) |
| 26 | + assert isinstance(pipeline._current_middleware, GraphTelemetryHandler) |
28 | 27 |
|
29 | 28 |
|
30 | | -def test_create_with_default_middleware(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 | | - ) |
36 | | - |
37 | | - assert isinstance(client, httpx.AsyncClient) |
38 | | - assert isinstance(client._transport, AsyncKiotaTransport) |
39 | | - pipeline = client._transport.middleware |
40 | | - assert isinstance(pipeline, GraphMiddlewarePipeline) |
41 | | - assert isinstance(pipeline._first_middleware, GraphAuthorizationHandler) |
42 | | - |
43 | | - |
44 | | -def test_create_with_custom_middleware(mock_token_provider): |
| 29 | +def test_create_with_custom_middleware(): |
45 | 30 | """Test creation of HTTP Clients with custom middleware""" |
46 | 31 | middleware = [ |
47 | 32 | GraphTelemetryHandler(), |
48 | 33 | ] |
49 | | - client = GraphClientFactory.create_with_custom_middleware( |
50 | | - client=httpx.AsyncClient(), middleware=middleware |
51 | | - ) |
| 34 | + client = GraphClientFactory.create_with_custom_middleware(middleware=middleware) |
52 | 35 |
|
53 | 36 | assert isinstance(client, httpx.AsyncClient) |
54 | 37 | assert isinstance(client._transport, AsyncKiotaTransport) |
55 | | - pipeline = client._transport.middleware |
| 38 | + pipeline = client._transport.pipeline |
56 | 39 | assert isinstance(pipeline._first_middleware, GraphTelemetryHandler) |
57 | 40 |
|
58 | 41 |
|
59 | | -def test_get_common_middleware(): |
60 | | - middleware = GraphClientFactory._get_common_middleware() |
61 | | - |
62 | | - assert len(middleware) == 3 |
63 | | - assert isinstance(middleware[0], GraphRedirectHandler) |
64 | | - assert isinstance(middleware[1], GraphRetryHandler) |
65 | | - assert isinstance(middleware[2], GraphTelemetryHandler) |
| 42 | +def test_graph_client_factory_with_custom_configuration(): |
| 43 | + """ |
| 44 | + Test creating a graph client with custom url overrides the default |
| 45 | + """ |
| 46 | + graph_client = GraphClientFactory.create_with_default_middleware( |
| 47 | + api_version=APIVersion.beta, host=NationalClouds.China |
| 48 | + ) |
| 49 | + assert isinstance(graph_client, httpx.AsyncClient) |
| 50 | + assert str(graph_client.base_url) == f'{NationalClouds.China}/{APIVersion.beta}/' |
| 51 | + |
| 52 | + |
| 53 | +def test_get_base_url(): |
| 54 | + """ |
| 55 | + Test base url is formed by combining the national cloud endpoint with |
| 56 | + Api version |
| 57 | + """ |
| 58 | + url = GraphClientFactory._get_base_url( |
| 59 | + host=NationalClouds.Germany, |
| 60 | + api_version=APIVersion.beta, |
| 61 | + ) |
| 62 | + assert url == f'{NationalClouds.Germany}/{APIVersion.beta}' |
0 commit comments