From 53c78a58bdad7fa53dd1ed3ecd8c9ffa5901bb02 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 May 2024 15:07:43 -0400 Subject: [PATCH] chore: updates references to factories following abstractions update --- .../RequestAdapterTests.cs | 26 +++++++++---------- src/HttpClientRequestAdapter.cs | 9 ++++--- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs index cd15006..1c8ae11 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs @@ -364,9 +364,9 @@ public async Task SendReturnsObjectOnContent(HttpStatusCode statusCode) var mockParseNode = new Mock(); mockParseNode.Setup(x => x.GetObjectValue(It.IsAny>())) .Returns(new MockEntity()); - var mockParseNodeFactory = new Mock(); - mockParseNodeFactory.Setup(x => x.GetRootParseNode(It.IsAny(), It.IsAny())) - .Returns(mockParseNode.Object); + var mockParseNodeFactory = new Mock(); + mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(mockParseNode.Object)); var adapter = new HttpClientRequestAdapter(_authenticationProvider, httpClient: client, parseNodeFactory: mockParseNodeFactory.Object); var requestInfo = new RequestInformation { @@ -393,7 +393,7 @@ public async Task RetriesOnCAEResponse() StatusCode = methodCalled ? HttpStatusCode.OK : HttpStatusCode.Unauthorized, Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Test"))) }; - if (!methodCalled) + if(!methodCalled) response.Headers.WwwAuthenticate.Add(new("Bearer", "realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\", error=\"insufficient_claims\", claims=\"eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTY1MjgxMzUwOCJ9fX0=\"")); methodCalled = true; return Task.FromResult(response); @@ -440,7 +440,7 @@ public async Task SetsTheApiExceptionStatusCode(HttpStatusCode statusCode) var response = await adapter.SendPrimitiveAsync(requestInfo); Assert.Fail("Expected an ApiException to be thrown"); } - catch (ApiException e) + catch(ApiException e) { Assert.Equal((int)statusCode, e.ResponseStatusCode); Assert.True(e.ResponseHeaders.ContainsKey("request-id")); @@ -467,9 +467,9 @@ public async Task SelectsTheXXXErrorMappingClassCorrectly(HttpStatusCode statusC var mockParseNode = new Mock(); mockParseNode.Setup(x => x.GetObjectValue(It.IsAny>())) .Returns(new MockError("A general error occured")); - var mockParseNodeFactory = new Mock(); - mockParseNodeFactory.Setup(x => x.GetRootParseNode(It.IsAny(), It.IsAny())) - .Returns(mockParseNode.Object); + var mockParseNodeFactory = new Mock(); + mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(mockParseNode.Object)); var adapter = new HttpClientRequestAdapter(_authenticationProvider, mockParseNodeFactory.Object, httpClient: client); var requestInfo = new RequestInformation { @@ -485,7 +485,7 @@ public async Task SelectsTheXXXErrorMappingClassCorrectly(HttpStatusCode statusC var response = await adapter.SendPrimitiveAsync(requestInfo, errorMapping); Assert.Fail("Expected an ApiException to be thrown"); } - catch (MockError mockError) + catch(MockError mockError) { Assert.Equal((int)statusCode, mockError.ResponseStatusCode); Assert.Equal("A general error occured", mockError.Message); @@ -511,9 +511,9 @@ public async Task ThrowsApiExceptionOnMissingMapping(HttpStatusCode statusCode) var mockParseNode = new Mock(); mockParseNode.Setup(x => x.GetObjectValue(It.IsAny>())) .Returns(new MockError("A general error occured: " + statusCode.ToString())); - var mockParseNodeFactory = new Mock(); - mockParseNodeFactory.Setup(x => x.GetRootParseNode(It.IsAny(), It.IsAny())) - .Returns(mockParseNode.Object); + var mockParseNodeFactory = new Mock(); + mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(mockParseNode.Object)); var adapter = new HttpClientRequestAdapter(_authenticationProvider, mockParseNodeFactory.Object, httpClient: client); var requestInfo = new RequestInformation { @@ -529,7 +529,7 @@ public async Task ThrowsApiExceptionOnMissingMapping(HttpStatusCode statusCode) var response = await adapter.SendPrimitiveAsync(requestInfo, errorMapping); Assert.Fail("Expected an ApiException to be thrown"); } - catch (ApiException apiException) + catch(ApiException apiException) { Assert.Equal((int)statusCode, apiException.ResponseStatusCode); Assert.Contains("The server returned an unexpected status code and no error factory is registered for this code", apiException.Message); diff --git a/src/HttpClientRequestAdapter.cs b/src/HttpClientRequestAdapter.cs index bf30bf3..7626fc8 100644 --- a/src/HttpClientRequestAdapter.cs +++ b/src/HttpClientRequestAdapter.cs @@ -441,8 +441,11 @@ private async Task ThrowIfFailedResponse(HttpResponseMessage response, Dictionar using var contentStream = await (response.Content?.ReadAsStreamAsync() ?? Task.FromResult(Stream.Null)).ConfigureAwait(false); #endif if(contentStream == Stream.Null || (contentStream.CanSeek && contentStream.Length == 0)) - return null;// ensure a usefule stream is passed to the factory - var rootNode = pNodeFactory.GetRootParseNode(responseContentType!, contentStream); + return null;// ensure a useful stream is passed to the factory +#pragma warning disable CS0618 // Type or member is obsolete + //TODO remove with v2 + var rootNode = pNodeFactory is IAsyncParseNodeFactory asyncParseNodeFactory ? await asyncParseNodeFactory.GetRootParseNodeAsync(responseContentType!, contentStream, cancellationToken) : pNodeFactory.GetRootParseNode(responseContentType!, contentStream); +#pragma warning restore CS0618 // Type or member is obsolete return rootNode; } private const string ClaimsKey = "claims"; @@ -538,7 +541,7 @@ private HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInform { Method = new HttpMethod(requestInfo.HttpMethod.ToString().ToUpperInvariant()), RequestUri = requestUri, - Version=new Version(2,0) + Version = new Version(2, 0) }; if(requestInfo.RequestOptions.Any())