Skip to content

Commit

Permalink
Updates auth handlers SendRetry to use per request provider if set.
Browse files Browse the repository at this point in the history
  • Loading branch information
peombwa committed Mar 12, 2019
1 parent c9a3093 commit 90f859b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private async Task<HttpResponseMessage> SendRetryAsync(HttpResponseMessage httpR

// Authenticate request using AuthenticationProvider

await AuthenticationProvider.AuthenticateRequestAsync(newRequest);
await authProvider.AuthenticateRequestAsync(newRequest);
httpResponseMessage = await base.SendAsync(newRequest, cancellationToken);

retryAttempt++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Graph.Core.Test.Requests
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;

[TestClass]
public class AuthenticationHandlerTests
Expand All @@ -32,6 +33,8 @@ public void Setup()
public void TearDown()
{
invoker.Dispose();
authenticationHandler.Dispose();
testHttpMessageHandler.Dispose();
}

[TestMethod]
Expand Down Expand Up @@ -107,6 +110,35 @@ public async Task AuthHandler_ShouldRetryUnauthorizedGetRequest()
Assert.IsNull(response.RequestMessage.Content, "Content is not null.");
}


[TestMethod]
public async Task AuthHandler_ShouldRetryUnauthorizedGetRequestUsingAuthHandlerOption()
{
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
{
httpRequestMessage.Properties.Add(typeof(GraphRequestContext).ToString(), new GraphRequestContext
{
MiddlewareOptions = new Dictionary<string, IMiddlewareOption>() {
{
typeof(AuthenticationHandlerOption).ToString(),
new AuthenticationHandlerOption { AuthenticationProvider = mockAuthenticationProvider.Object }
}
}
});
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);

var response = await msgInvoker.SendAsync(httpRequestMessage, new CancellationToken());

Assert.AreNotSame(response.RequestMessage, httpRequestMessage, "Doesn't reissue a new http request.");
Assert.AreSame(response, expectedResponse, "Retry didn't happen.");
Assert.IsNull(response.RequestMessage.Content, "Content is not null.");
}
}

[TestMethod]
public async Task AuthHandler_ShouldRetryUnauthorizedPostRequestWithNoContent()
{
Expand Down Expand Up @@ -221,5 +253,23 @@ public async Task AuthHandler_ShouldReturnUnauthorizedRequestWithDefaultMaxRetry
Assert.AreSame(response, expectedResponse, "Unexpected code returned.");
Assert.AreEqual(response.RequestMessage.Content.ReadAsStringAsync().Result, "Hello Mars!");
}

[TestMethod]
public async Task AuthHandler_ShouldThrowExceptionWhenAuthProviderIsNotSet()
{
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
{
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);

ServiceException ex = await Assert.ThrowsExceptionAsync<ServiceException>(() => msgInvoker.SendAsync(httpRequestMessage, new CancellationToken()));

Assert.AreSame(ex.Error.Code, ErrorConstants.Codes.InvalidRequest, "Unexpected exception code set.");
Assert.AreSame(ex.Error.Message, ErrorConstants.Messages.AuthenticationProviderMissing, "Unexpected exception message set.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.Requests
using System.Threading;
using Xunit;
using System.Threading.Tasks;
using System.Collections.Generic;

public class AuthenticationHandlerTests : IDisposable
{
Expand All @@ -30,6 +31,8 @@ public AuthenticationHandlerTests()
public void Dispose()
{
invoker.Dispose();
authenticationHandler.Dispose();
testHttpMessageHandler.Dispose();
}

[Fact]
Expand Down Expand Up @@ -103,6 +106,34 @@ public async Task AuthHandler_ShouldRetryUnauthorizedGetRequest()
Assert.Null(response.RequestMessage.Content);
}

[Fact]
public async Task AuthHandler_ShouldRetryUnauthorizedGetRequestUsingAuthHandlerOption()
{
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
{
httpRequestMessage.Properties.Add(typeof(GraphRequestContext).ToString(), new GraphRequestContext
{
MiddlewareOptions = new Dictionary<string, IMiddlewareOption>() {
{
typeof(AuthenticationHandlerOption).ToString(),
new AuthenticationHandlerOption { AuthenticationProvider = mockAuthenticationProvider.Object }
}
}
});
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);

var response = await msgInvoker.SendAsync(httpRequestMessage, new CancellationToken());

Assert.NotSame(response.RequestMessage, httpRequestMessage);
Assert.Same(response, expectedResponse);
Assert.Null(response.RequestMessage.Content);
}
}

[Fact]
public async Task AuthHandler_ShouldRetryUnauthorizedPostRequestWithNoContent()
{
Expand Down Expand Up @@ -219,5 +250,23 @@ public async Task AuthHandler_ShouldReturnUnauthorizedRequestWithDefaultMaxRetry
Assert.Same(response, expectedResponse);
Assert.Equal(response.RequestMessage.Content.ReadAsStringAsync().Result, "Hello Mars!");
}

[Fact]
public async Task AuthHandler_ShouldThrowExceptionWhenAuthProviderIsNotSet()
{
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
{
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);

ServiceException ex = await Assert.ThrowsAsync<ServiceException>(() => msgInvoker.SendAsync(httpRequestMessage, new CancellationToken()));

Assert.Same(ex.Error.Code, ErrorConstants.Codes.InvalidRequest);
Assert.Same(ex.Error.Message, ErrorConstants.Messages.AuthenticationProviderMissing);
}
}
}
}

0 comments on commit 90f859b

Please sign in to comment.