@@ -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}
0 commit comments