Sometimes when developing a service that calls an external REST API or testing calling an endpoint from a front-end, you'll need to test what hap

Testing REST endpoints with fake HTTP statuses

submited by
Style Pass
2024-10-21 19:00:04

Sometimes when developing a service that calls an external REST API or testing calling an endpoint from a front-end, you'll need to test what happens when that API returns different HTTP status codes. You can do this using tests, and mock/stub a 404, 500, etc. But often it's very helpful to see what happens when a specific response status is returned.

Or imagine your QA team needs to manually test what happens when a 500 is returned. For example, they need to confirm an error page shows if the app receives an error response. In that scenario you can't (purely) rely on stubbed automated tests.

Anytime you have to stub responses, that setup can be tricky and can slow you down. You could go an alternative route and setup your own server that returns 404, 401, 500, etc. And then you could manually swap out the endpoints that get called to point to your "stubbed" service, or using some complicated proxy setup to forward the requests. But that also takes time, adds complexity, and will slow you down.

Rather than having to spin up your own server or only handle this via tests, you can use free 3rd party services that do this for you! This will make seeing how your code handles different HTTP response statuses, with scenarios like the below much easier:

Leave a Comment