public async Task CheckFileAsyncPasses() { // arrange FileServiceClient.ClearHttpClient(); var resourceId = 1; var baseUri = new Uri("http://foo/"); var fullUri = new Uri(baseUri, $"Files({resourceId})"); var mockResponse = new HttpResponseMessage(HttpStatusCode.OK) { StatusCode = HttpStatusCode.NoContent, Content = new StringContent(string.Empty), }; mockResponse.Content.Headers.LastModified = DateTimeOffset.UtcNow; var myHash = "MyHash"; mockResponse.Content.Headers.ContentMD5 = Encoding.UTF8.GetBytes(myHash); var myFileName = "MyFile.txt"; mockResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = myFileName }; var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() // Setup the PROTECTED method to mock .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) // prepare the expected response of the mocked http call .ReturnsAsync(mockResponse) .Verifiable(); // act var fileServiceClient = new FileServiceClient(this.mockAccessTokenRepository.Object, baseUri, handlerMock.Object, new CancellationToken()); var result = await fileServiceClient.CheckFileAsync(resourceId).ConfigureAwait(false); // assert Assert.AreEqual(mockResponse.StatusCode, result.StatusCode); Assert.AreEqual(mockResponse.Content.Headers.LastModified, result.LastModified); Assert.AreEqual(myHash, result.HashForFileOnServer); Assert.AreEqual(myFileName, result.FileNameOnServer); handlerMock.Protected() .Verify( "SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>( req => req.Method == HttpMethod.Get && req.RequestUri == fullUri), ItExpr.IsAny <CancellationToken>()); }
public async Task HandlesCheckFileAsyncWithNetworkHiccup() { // arrange FileServiceClient.ClearHttpClient(); var resourceId = 1; var baseUri = new Uri("http://foo/"); var fullUri = new Uri(baseUri, $"Files({resourceId})"); var mockResponse = new HttpResponseMessage(HttpStatusCode.OK) { StatusCode = HttpStatusCode.NoContent, Content = new StringContent(string.Empty), }; var headersLastModified = DateTimeOffset.Parse("10/20/2018"); mockResponse.Content.Headers.LastModified = headersLastModified; var myHash = "MyHash"; mockResponse.Content.Headers.ContentMD5 = Encoding.UTF8.GetBytes(myHash); var myFileName = "MyFile.txt"; mockResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = myFileName }; int count = 0; var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() // Setup the PROTECTED method to mock .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) // prepare the expected response of the mocked http call .ReturnsAsync(() => { if (count < 1) { count++; var response = new HttpResponseMessage(HttpStatusCode.OK) { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent("This is my error"), }; response.Content.Headers.LastModified = DateTimeOffset.Parse("11/11/2017"); return(response); } return(mockResponse); }) .Verifiable(); // act var fileServiceClient = new FileServiceClient(this.mockAccessTokenRepository.Object, baseUri, handlerMock.Object, new CancellationToken()); fileServiceClient.TransientError += (sender, args) => Console.WriteLine("Transient Error: " + args.StatusCode + " " + args.Response); var result = await fileServiceClient.CheckFileAsync(resourceId).ConfigureAwait(false); // assert Assert.AreEqual(mockResponse.StatusCode, result.StatusCode); Assert.AreEqual(myHash, result.HashForFileOnServer); Assert.AreEqual(myFileName, result.FileNameOnServer); Assert.AreEqual(headersLastModified, result.LastModified); handlerMock.Protected() .Verify( "SendAsync", Times.Exactly(2), ItExpr.Is <HttpRequestMessage>( req => req.Method == HttpMethod.Get && req.RequestUri == fullUri), ItExpr.IsAny <CancellationToken>()); }