I have some methods in my app that make http requests. Is there a good way to simulate network failures and bad responses for the unit tests?
The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to the CRUD operations (create, read, update, and delete).
Here are some tips for testing POST requests: Create a resource with a POST request and ensure a 200 status code is returned. Next, make a GET request for that resource, and ensure the data was saved correctly. Add tests that ensure POST requests fail with incorrect or ill-formatted data.
The two most common HTTP methods are GET and POST.
The method designates the type of request being made to the web server. The most common types of request methods are GET and POST but there are many others, including HEAD, PUT, DELETE, CONNECT, and OPTIONS.
Suitable Mock Objects should let you perform such simulations easily; if you're unfamiliar with the concept, there's a good tutorial here.
For network failures nothing beats first unplugging your computers ethernet cable (or d/c it's wireless) and then unplugging whatever cable gives you access to the cloud.
If by bad responses you mean HTTP errors you can write ASP scripts that will always throw specific errors. If you want to test malformed HTTP packets you'll have to write a simple socket app to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With