Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
chore: updates references to factories following abstractions update
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed May 21, 2024
1 parent b5af728 commit 53c78a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
26 changes: 13 additions & 13 deletions Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ public async Task SendReturnsObjectOnContent(HttpStatusCode statusCode)
var mockParseNode = new Mock<IParseNode>();
mockParseNode.Setup<IParsable>(x => x.GetObjectValue(It.IsAny<ParsableFactory<MockEntity>>()))
.Returns(new MockEntity());
var mockParseNodeFactory = new Mock<IParseNodeFactory>();
mockParseNodeFactory.Setup<IParseNode>(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>()))
.Returns(mockParseNode.Object);
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(mockParseNode.Object));
var adapter = new HttpClientRequestAdapter(_authenticationProvider, httpClient: client, parseNodeFactory: mockParseNodeFactory.Object);
var requestInfo = new RequestInformation
{
Expand All @@ -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);
Expand Down Expand Up @@ -440,7 +440,7 @@ public async Task SetsTheApiExceptionStatusCode(HttpStatusCode statusCode)
var response = await adapter.SendPrimitiveAsync<Stream>(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"));
Expand All @@ -467,9 +467,9 @@ public async Task SelectsTheXXXErrorMappingClassCorrectly(HttpStatusCode statusC
var mockParseNode = new Mock<IParseNode>();
mockParseNode.Setup<IParsable>(x => x.GetObjectValue(It.IsAny<ParsableFactory<IParsable>>()))
.Returns(new MockError("A general error occured"));
var mockParseNodeFactory = new Mock<IParseNodeFactory>();
mockParseNodeFactory.Setup<IParseNode>(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>()))
.Returns(mockParseNode.Object);
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(mockParseNode.Object));
var adapter = new HttpClientRequestAdapter(_authenticationProvider, mockParseNodeFactory.Object, httpClient: client);
var requestInfo = new RequestInformation
{
Expand All @@ -485,7 +485,7 @@ public async Task SelectsTheXXXErrorMappingClassCorrectly(HttpStatusCode statusC
var response = await adapter.SendPrimitiveAsync<Stream>(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);
Expand All @@ -511,9 +511,9 @@ public async Task ThrowsApiExceptionOnMissingMapping(HttpStatusCode statusCode)
var mockParseNode = new Mock<IParseNode>();
mockParseNode.Setup<IParsable>(x => x.GetObjectValue(It.IsAny<ParsableFactory<IParsable>>()))
.Returns(new MockError("A general error occured: " + statusCode.ToString()));
var mockParseNodeFactory = new Mock<IParseNodeFactory>();
mockParseNodeFactory.Setup<IParseNode>(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>()))
.Returns(mockParseNode.Object);
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(mockParseNode.Object));
var adapter = new HttpClientRequestAdapter(_authenticationProvider, mockParseNodeFactory.Object, httpClient: client);
var requestInfo = new RequestInformation
{
Expand All @@ -529,7 +529,7 @@ public async Task ThrowsApiExceptionOnMissingMapping(HttpStatusCode statusCode)
var response = await adapter.SendPrimitiveAsync<Stream>(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);
Expand Down
9 changes: 6 additions & 3 deletions src/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 446 in src/HttpClientRequestAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)

Check warning on line 446 in src/HttpClientRequestAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
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";
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 53c78a5

Please sign in to comment.