Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub calls to third party services using WireMock

I tried to find a way to stub calls to external services via WireMock. WireMock easily mocks any relative URL, but what if we want to intercept a REST call, which was sent from our node to some 3rd party service and return predefined response? Is there any possibility to do that?

like image 964
Alesto Avatar asked Jul 21 '17 10:07

Alesto


People also ask

What is stubbing in WireMock?

What Is Stubbing? Stubbing is a technique that allows us to configure the HTTP response that is returned by our WireMock server when it receives a specific HTTP request. We can stub HTTP requests with WireMock by using the static givenThat() method of the WireMock class.

What is API stubbing?

What is API Stubbing? The simplest of the three test doubles is the API stub. When a tester creates stubs, they're creating parts with minimal implementations of an interface. A stub always returns hardcoded data, or “canned” answers, to API calls made during individual tests.

What is WireMock and how do you use it?

WireMock is a tool that can mimic the behavior of an HTTP API and capture the HTTP requests send to that API. It allows us to: Configure the response returned by the HTTP API when it receives a specific request. Capture the incoming HTTP requests and write assertions for the captured HTTP requests.


1 Answers

I did a workaround by extracting the host as application configuration.

So if your application sends requests to:

http://thired-party-service.com/someEndPoint

You can extract the host as configuration param:

host=http://thired-party-service.com/

Now when you running in a test context fill the host param with same host as your WireMock server, for example:

host=http://localhost:8080/

Now you can use WireMock stubs as usual.

like image 198
Ghost Avatar answered Oct 12 '22 15:10

Ghost