Ejemplo n.º 1
0
        public async Task TestSendPostWithHttp500TypeFailureAsync()
        {
            using (var httpManager = new MockHttpManager())
            {
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.GatewayTimeout);
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.ServiceUnavailable);

                try
                {
                    var msalHttpResponse = await httpManager.SendPostAsync(
                        new Uri(TestConstants.AuthorityHomeTenant + "oauth2/token"),
                        null,
                        (IDictionary <string, string>) null,
                        Substitute.For <ICoreLogger>())
                                           .ConfigureAwait(false);

                    Assert.Fail("request should have failed");
                }
                catch (MsalServiceException exc)
                {
                    Assert.IsNotNull(exc);
                    Assert.AreEqual(MsalError.ServiceNotAvailable, exc.ErrorCode);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task TestSendGetWithHttp500TypeFailure2Async()
        {
            using (var httpManager = new MockHttpManager())
            {
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.BadGateway);
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.BadGateway);

                var msalHttpResponse = await httpManager.SendPostForceResponseAsync(
                    new Uri(TestConstants.AuthorityHomeTenant + "oauth2/token"),
                    null,
                    new StringContent("body"),
                    Substitute.For <ICoreLogger>())
                                       .ConfigureAwait(false);

                Assert.AreEqual(HttpStatusCode.BadGateway, msalHttpResponse.StatusCode);
            }
        }
Ejemplo n.º 3
0
        public async Task TestSendGetWithHttp500TypeFailure2Async()
        {
            using (var httpManager = new MockHttpManager())
            {
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.BadGateway);
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.BadGateway);

                var msalHttpResponse = await httpManager.SendPostForceResponseAsync(
                    new Uri(MsalTestConstants.AuthorityHomeTenant + "oauth2/token"),
                    new Dictionary <string, string>(),
                    new StringContent("body"),
                    RequestContext.CreateForTest())
                                       .ConfigureAwait(false);

                Assert.AreEqual(HttpStatusCode.BadGateway, msalHttpResponse.StatusCode);
            }
        }
Ejemplo n.º 4
0
        public async Task TestSendGetWithHttp500TypeFailureAsync()
        {
            using (var httpManager = new MockHttpManager())
            {
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Get, HttpStatusCode.GatewayTimeout);
                httpManager.AddResiliencyMessageMockHandler(HttpMethod.Get, HttpStatusCode.InternalServerError);

                try
                {
                    var msalHttpResponse = await httpManager.SendGetAsync(
                        new Uri(MsalTestConstants.AuthorityHomeTenant + "oauth2/token"),
                        new Dictionary <string, string>(),
                        RequestContext.CreateForTest())
                                           .ConfigureAwait(false);

                    Assert.Fail("request should have failed");
                }
                catch (MsalServiceException exc)
                {
                    Assert.IsNotNull(exc);
                    Assert.AreEqual(MsalError.ServiceNotAvailable, exc.ErrorCode);
                }
            }
        }
Ejemplo n.º 5
0
        public async Task NoResiliencyIfRetryAfterHeaderPresentAsync(bool useTimeSpanRetryAfter)
        {
            using (var httpManager = new MockHttpManager())
            {
                var response = httpManager.AddResiliencyMessageMockHandler(HttpMethod.Get, HttpStatusCode.ServiceUnavailable);

                response.Headers.RetryAfter = useTimeSpanRetryAfter ?
                                              new System.Net.Http.Headers.RetryConditionHeaderValue(TimeSpan.FromSeconds(1)) :
                                              new System.Net.Http.Headers.RetryConditionHeaderValue(DateTimeOffset.UtcNow + TimeSpan.FromMinutes(2));

                var exc = await AssertException.TaskThrowsAsync <MsalServiceException>(
                    () => httpManager.SendGetAsync(
                        new Uri(TestConstants.AuthorityHomeTenant + "oauth2/token"), null, Substitute.For <ICoreLogger>()))
                          .ConfigureAwait(false);

                Assert.AreEqual(0, httpManager.QueueSize, "HttpManager must not retry because a RetryAfter header is present");
                Assert.AreEqual(MsalError.ServiceNotAvailable, exc.ErrorCode);
            }
        }