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

Commit e698ffe

Browse files
committed
chore: updates references to factories following abstractions update
1 parent cffe43c commit e698ffe

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ public async Task SendReturnsObjectOnContent(HttpStatusCode statusCode)
364364
var mockParseNode = new Mock<IParseNode>();
365365
mockParseNode.Setup<IParsable>(x => x.GetObjectValue(It.IsAny<ParsableFactory<MockEntity>>()))
366366
.Returns(new MockEntity());
367-
var mockParseNodeFactory = new Mock<IParseNodeFactory>();
368-
mockParseNodeFactory.Setup<IParseNode>(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>()))
369-
.Returns(mockParseNode.Object);
367+
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
368+
mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
369+
.Returns(Task.FromResult(mockParseNode.Object));
370370
var adapter = new HttpClientRequestAdapter(_authenticationProvider, httpClient: client, parseNodeFactory: mockParseNodeFactory.Object);
371371
var requestInfo = new RequestInformation
372372
{
@@ -393,7 +393,7 @@ public async Task RetriesOnCAEResponse()
393393
StatusCode = methodCalled ? HttpStatusCode.OK : HttpStatusCode.Unauthorized,
394394
Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Test")))
395395
};
396-
if (!methodCalled)
396+
if(!methodCalled)
397397
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=\""));
398398
methodCalled = true;
399399
return Task.FromResult(response);
@@ -440,7 +440,7 @@ public async Task SetsTheApiExceptionStatusCode(HttpStatusCode statusCode)
440440
var response = await adapter.SendPrimitiveAsync<Stream>(requestInfo);
441441
Assert.Fail("Expected an ApiException to be thrown");
442442
}
443-
catch (ApiException e)
443+
catch(ApiException e)
444444
{
445445
Assert.Equal((int)statusCode, e.ResponseStatusCode);
446446
Assert.True(e.ResponseHeaders.ContainsKey("request-id"));
@@ -467,9 +467,9 @@ public async Task SelectsTheXXXErrorMappingClassCorrectly(HttpStatusCode statusC
467467
var mockParseNode = new Mock<IParseNode>();
468468
mockParseNode.Setup<IParsable>(x => x.GetObjectValue(It.IsAny<ParsableFactory<IParsable>>()))
469469
.Returns(new MockError("A general error occured"));
470-
var mockParseNodeFactory = new Mock<IParseNodeFactory>();
471-
mockParseNodeFactory.Setup<IParseNode>(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>()))
472-
.Returns(mockParseNode.Object);
470+
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
471+
mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
472+
.Returns(Task.FromResult(mockParseNode.Object));
473473
var adapter = new HttpClientRequestAdapter(_authenticationProvider, mockParseNodeFactory.Object, httpClient: client);
474474
var requestInfo = new RequestInformation
475475
{
@@ -485,7 +485,7 @@ public async Task SelectsTheXXXErrorMappingClassCorrectly(HttpStatusCode statusC
485485
var response = await adapter.SendPrimitiveAsync<Stream>(requestInfo, errorMapping);
486486
Assert.Fail("Expected an ApiException to be thrown");
487487
}
488-
catch (MockError mockError)
488+
catch(MockError mockError)
489489
{
490490
Assert.Equal((int)statusCode, mockError.ResponseStatusCode);
491491
Assert.Equal("A general error occured", mockError.Message);
@@ -511,9 +511,9 @@ public async Task ThrowsApiExceptionOnMissingMapping(HttpStatusCode statusCode)
511511
var mockParseNode = new Mock<IParseNode>();
512512
mockParseNode.Setup<IParsable>(x => x.GetObjectValue(It.IsAny<ParsableFactory<IParsable>>()))
513513
.Returns(new MockError("A general error occured: " + statusCode.ToString()));
514-
var mockParseNodeFactory = new Mock<IParseNodeFactory>();
515-
mockParseNodeFactory.Setup<IParseNode>(x => x.GetRootParseNode(It.IsAny<string>(), It.IsAny<Stream>()))
516-
.Returns(mockParseNode.Object);
514+
var mockParseNodeFactory = new Mock<IAsyncParseNodeFactory>();
515+
mockParseNodeFactory.Setup(x => x.GetRootParseNodeAsync(It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
516+
.Returns(Task.FromResult(mockParseNode.Object));
517517
var adapter = new HttpClientRequestAdapter(_authenticationProvider, mockParseNodeFactory.Object, httpClient: client);
518518
var requestInfo = new RequestInformation
519519
{
@@ -529,7 +529,7 @@ public async Task ThrowsApiExceptionOnMissingMapping(HttpStatusCode statusCode)
529529
var response = await adapter.SendPrimitiveAsync<Stream>(requestInfo, errorMapping);
530530
Assert.Fail("Expected an ApiException to be thrown");
531531
}
532-
catch (ApiException apiException)
532+
catch(ApiException apiException)
533533
{
534534
Assert.Equal((int)statusCode, apiException.ResponseStatusCode);
535535
Assert.Contains("The server returned an unexpected status code and no error factory is registered for this code", apiException.Message);

src/HttpClientRequestAdapter.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,11 @@ private async Task ThrowIfFailedResponse(HttpResponseMessage response, Dictionar
441441
using var contentStream = await (response.Content?.ReadAsStreamAsync() ?? Task.FromResult(Stream.Null)).ConfigureAwait(false);
442442
#endif
443443
if(contentStream == Stream.Null || (contentStream.CanSeek && contentStream.Length == 0))
444-
return null;// ensure a usefule stream is passed to the factory
445-
var rootNode = pNodeFactory.GetRootParseNode(responseContentType!, contentStream);
444+
return null;// ensure a useful stream is passed to the factory
445+
#pragma warning disable CS0618 // Type or member is obsolete
446+
//TODO remove with v2
447+
var rootNode = pNodeFactory is IAsyncParseNodeFactory asyncParseNodeFactory ? await asyncParseNodeFactory.GetRootParseNodeAsync(responseContentType!, contentStream, cancellationToken) : pNodeFactory.GetRootParseNode(responseContentType!, contentStream);
448+
#pragma warning restore CS0618 // Type or member is obsolete
446449
return rootNode;
447450
}
448451
private const string ClaimsKey = "claims";
@@ -538,7 +541,7 @@ private HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInform
538541
{
539542
Method = new HttpMethod(requestInfo.HttpMethod.ToString().ToUpperInvariant()),
540543
RequestUri = requestUri,
541-
Version=new Version(2,0)
544+
Version = new Version(2, 0)
542545
};
543546

544547
if(requestInfo.RequestOptions.Any())

0 commit comments

Comments
 (0)