public void GetMovie_On401Response_MustThrowUnauthorizedApiAccessException() { HttpClient httpClient = new HttpClient(new Return401UnauthorizedResponseHandler()); TestableClassWithApiAccess testableClass = new TestableClassWithApiAccess(httpClient); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); Assert.ThrowsAsync <UnauthorizedApiAccessException>( () => testableClass.GetMovie(cancellationTokenSource.Token)); }
public void GetAuthors_On401Response_MustThrowUnauthorizedAccessExpection() { var httpClient = new HttpClient(new Return401UnauthorizedResponseHandler()); var cancellationToken = new CancellationTokenSource(); var testableClass = new TestableClassWithApiAccess(httpClient); AsyncTestDelegate act = () => testableClass.GetAuthors(cancellationToken.Token); Assert.That(act, Throws.TypeOf <UnauthorizedAccessException>()); //Assert.ThrowsAsync<UnauthorizedAccessException>(() => response); }
public void GetMovie_On401Response_MustThrowUnauthorizedApiAccessException_WithMoq() { var unauthorizedResponseHttpMessageHandlerMock = new Mock <HttpMessageHandler>(); unauthorizedResponseHttpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.Unauthorized }); var httpClient = new HttpClient(unauthorizedResponseHttpMessageHandlerMock.Object); var testableClass = new TestableClassWithApiAccess(httpClient); var cancellationTokenSource = new CancellationTokenSource(); Assert.ThrowsAsync <UnauthorizedAccessException>(() => testableClass.GetMovie(cancellationTokenSource.Token)); }
public void GetAuthors_On401Response_MustThrowUnauthorizedAccessExpection_WithMoq() { var unauthorizedResponseHttphandlerMock = new Mock <HttpMessageHandler>(); unauthorizedResponseHttphandlerMock.Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ).ReturnsAsync(new HttpResponseMessage() { StatusCode = System.Net.HttpStatusCode.Unauthorized }); var httpClient = new HttpClient(unauthorizedResponseHttphandlerMock.Object); var testableClass = new TestableClassWithApiAccess(httpClient); var cancellationToken = new CancellationTokenSource(); var r = testableClass.GetAuthors(cancellationToken.Token); AsyncTestDelegate act = () => testableClass.GetAuthors(cancellationToken.Token); Assert.That(act, Throws.TypeOf <UnauthorizedAccessException>()); //Assert.ThrowsAsync<UnauthorizedAccessException>(() => response); }