vendredi 26 août 2016

Simulate HttpRequestException for unit testing

I have an application that downloads massive amounts of pdfs from the web. From time to time, I get a HttpRequestException, contaning the message: Error while copying content to a stream.

So, I am trying to unit test my code to handle this situation. My current code for downloading is:

var request = await httpClient.GetAsync(url);

var bytes = await Policy
.Handle<HttpRequestException>(ex => ex.Message.Contains("Error while copying content to a stream"))
.WaitAndRetryAsync(3, retryCount => TimeSpan.FromSeconds(Math.Pow(5, retryCount)))
.ExecuteAsync(async () => await request.Content.ReadAsByteArrayAsync());

Now I am trying to simulate a HttpRequestException, so that I can unit test the code above, but I dont know how to do it. Can anyone please help?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire