Skip to content

Commit 90f859b

Browse files
committed
Updates auth handlers SendRetry to use per request provider if set.
1 parent c9a3093 commit 90f859b

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/Microsoft.Graph.Core/Requests/Middleware/AuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private async Task<HttpResponseMessage> SendRetryAsync(HttpResponseMessage httpR
7979

8080
// Authenticate request using AuthenticationProvider
8181

82-
await AuthenticationProvider.AuthenticateRequestAsync(newRequest);
82+
await authProvider.AuthenticateRequestAsync(newRequest);
8383
httpResponseMessage = await base.SendAsync(newRequest, cancellationToken);
8484

8585
retryAttempt++;

tests/Microsoft.Graph.Core.Test/Requests/Middleware/AuthenticationHandlerTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.Graph.Core.Test.Requests
1010
using System.Net.Http;
1111
using System.Threading.Tasks;
1212
using System.Threading;
13+
using System.Collections.Generic;
1314

1415
[TestClass]
1516
public class AuthenticationHandlerTests
@@ -32,6 +33,8 @@ public void Setup()
3233
public void TearDown()
3334
{
3435
invoker.Dispose();
36+
authenticationHandler.Dispose();
37+
testHttpMessageHandler.Dispose();
3538
}
3639

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

113+
114+
[TestMethod]
115+
public async Task AuthHandler_ShouldRetryUnauthorizedGetRequestUsingAuthHandlerOption()
116+
{
117+
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
118+
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
119+
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
120+
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
121+
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
122+
{
123+
httpRequestMessage.Properties.Add(typeof(GraphRequestContext).ToString(), new GraphRequestContext
124+
{
125+
MiddlewareOptions = new Dictionary<string, IMiddlewareOption>() {
126+
{
127+
typeof(AuthenticationHandlerOption).ToString(),
128+
new AuthenticationHandlerOption { AuthenticationProvider = mockAuthenticationProvider.Object }
129+
}
130+
}
131+
});
132+
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);
133+
134+
var response = await msgInvoker.SendAsync(httpRequestMessage, new CancellationToken());
135+
136+
Assert.AreNotSame(response.RequestMessage, httpRequestMessage, "Doesn't reissue a new http request.");
137+
Assert.AreSame(response, expectedResponse, "Retry didn't happen.");
138+
Assert.IsNull(response.RequestMessage.Content, "Content is not null.");
139+
}
140+
}
141+
110142
[TestMethod]
111143
public async Task AuthHandler_ShouldRetryUnauthorizedPostRequestWithNoContent()
112144
{
@@ -221,5 +253,23 @@ public async Task AuthHandler_ShouldReturnUnauthorizedRequestWithDefaultMaxRetry
221253
Assert.AreSame(response, expectedResponse, "Unexpected code returned.");
222254
Assert.AreEqual(response.RequestMessage.Content.ReadAsStringAsync().Result, "Hello Mars!");
223255
}
256+
257+
[TestMethod]
258+
public async Task AuthHandler_ShouldThrowExceptionWhenAuthProviderIsNotSet()
259+
{
260+
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
261+
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
262+
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
263+
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
264+
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
265+
{
266+
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);
267+
268+
ServiceException ex = await Assert.ThrowsExceptionAsync<ServiceException>(() => msgInvoker.SendAsync(httpRequestMessage, new CancellationToken()));
269+
270+
Assert.AreSame(ex.Error.Code, ErrorConstants.Codes.InvalidRequest, "Unexpected exception code set.");
271+
Assert.AreSame(ex.Error.Message, ErrorConstants.Messages.AuthenticationProviderMissing, "Unexpected exception message set.");
272+
}
273+
}
224274
}
225275
}

tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/AuthenticationHandlerTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.Requests
1111
using System.Threading;
1212
using Xunit;
1313
using System.Threading.Tasks;
14+
using System.Collections.Generic;
1415

1516
public class AuthenticationHandlerTests : IDisposable
1617
{
@@ -30,6 +31,8 @@ public AuthenticationHandlerTests()
3031
public void Dispose()
3132
{
3233
invoker.Dispose();
34+
authenticationHandler.Dispose();
35+
testHttpMessageHandler.Dispose();
3336
}
3437

3538
[Fact]
@@ -103,6 +106,34 @@ public async Task AuthHandler_ShouldRetryUnauthorizedGetRequest()
103106
Assert.Null(response.RequestMessage.Content);
104107
}
105108

109+
[Fact]
110+
public async Task AuthHandler_ShouldRetryUnauthorizedGetRequestUsingAuthHandlerOption()
111+
{
112+
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
113+
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
114+
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
115+
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
116+
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
117+
{
118+
httpRequestMessage.Properties.Add(typeof(GraphRequestContext).ToString(), new GraphRequestContext
119+
{
120+
MiddlewareOptions = new Dictionary<string, IMiddlewareOption>() {
121+
{
122+
typeof(AuthenticationHandlerOption).ToString(),
123+
new AuthenticationHandlerOption { AuthenticationProvider = mockAuthenticationProvider.Object }
124+
}
125+
}
126+
});
127+
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);
128+
129+
var response = await msgInvoker.SendAsync(httpRequestMessage, new CancellationToken());
130+
131+
Assert.NotSame(response.RequestMessage, httpRequestMessage);
132+
Assert.Same(response, expectedResponse);
133+
Assert.Null(response.RequestMessage.Content);
134+
}
135+
}
136+
106137
[Fact]
107138
public async Task AuthHandler_ShouldRetryUnauthorizedPostRequestWithNoContent()
108139
{
@@ -219,5 +250,23 @@ public async Task AuthHandler_ShouldReturnUnauthorizedRequestWithDefaultMaxRetry
219250
Assert.Same(response, expectedResponse);
220251
Assert.Equal(response.RequestMessage.Content.ReadAsStringAsync().Result, "Hello Mars!");
221252
}
253+
254+
[Fact]
255+
public async Task AuthHandler_ShouldThrowExceptionWhenAuthProviderIsNotSet()
256+
{
257+
DelegatingHandler authHandler = new AuthenticationHandler(null, testHttpMessageHandler);
258+
using (HttpMessageInvoker msgInvoker = new HttpMessageInvoker(authHandler))
259+
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/bar"))
260+
using (var unauthorizedResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized))
261+
using (var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK))
262+
{
263+
testHttpMessageHandler.SetHttpResponse(unauthorizedResponse, expectedResponse);
264+
265+
ServiceException ex = await Assert.ThrowsAsync<ServiceException>(() => msgInvoker.SendAsync(httpRequestMessage, new CancellationToken()));
266+
267+
Assert.Same(ex.Error.Code, ErrorConstants.Codes.InvalidRequest);
268+
Assert.Same(ex.Error.Message, ErrorConstants.Messages.AuthenticationProviderMissing);
269+
}
270+
}
222271
}
223272
}

0 commit comments

Comments
 (0)