Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing methods that make http requests

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?

like image 487
Pedro Borges Avatar asked Jun 15 '09 18:06

Pedro Borges


People also ask

What are the 4 types of HTTP request methods?

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).

How do I test HTTP requests?

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.

What are the 2 most common method for HTTP request?

The two most common HTTP methods are GET and POST.

What are the 5 types of HTTP request that can be made?

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.


2 Answers

Suitable Mock Objects should let you perform such simulations easily; if you're unfamiliar with the concept, there's a good tutorial here.

like image 138
Alex Martelli Avatar answered Sep 25 '22 12:09

Alex Martelli


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.

like image 28
Spencer Ruport Avatar answered Sep 21 '22 12:09

Spencer Ruport