How to spoof a third party API

WePython 1 Months+

Our application does calls to a third party API for completing a few tasks.

This endpoint has an issue in the test environment, and management has asked us to mock this environment so when the app or anyone tries to call www.test.com from the test machine, it gets information from a mock server rather than the actual www.test.com.

I tried to figure out different options but everything mocks if we create a mock object and then calls the mock object.

I am trying to figure out a way in which whenever someone calls www.test.com it should show content from localhost.

I don't want to edit the host file. Is there any other way?

Update:

Made the app configurable, so we can specify which endpoint the app should call.

I used Mockserver in postman to simulate this . But postmand doesn't support query parameter

so for v1/api?q=1 and v1/api?q=2 it returns the same response

Could someone help how to create a mockserver that gives different response for different query parmaeter

-----------------Answer-----------------

We have a good experience with json-server. We found it really flexible.

Get a full fake REST API with zero coding in less than 30 seconds (seriously)

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

https://github.com/typicode/json-server

It can server from static json files, where each ID returns a different value. Or you can use JavaScript to generate them. You could change the data based on the id, give random data, rotate datasets, etc...

They also have a version hosted for you if you want to test over the internet. Small datasets are free: https://my-json-server.typicode.com/

-----------------Answer-----------------

According to Postman docs:

If there is no exact match found, Postman will return the best matching response based on its algorithm.

I have tested the mock server and it does work with query parameters, however the algorithm points to a default value when you do not mock the parameter. In my case:

?id=1 points to 200 with ok page
?id=2 points to 404 with bad request page
?id=3 and so on is not implemented and mock server returns the 200 with ok page.

-----------------Answer-----------------

If you accept mocking with other packages than Postman, then I suggest using MockServer or WireMock, both having support for describing matching rules in Java and JSON.

For instance, with WireMock you can match HTTP Request by URL and query string as follows:

Regex matching on path and query

Java:

urlMatching("/your/([a-z]*)\\?and=query")

JSON:

{
  "request": {
    "urlPattern": "/your/([a-z]*)\\?and=query"
    ...
  },
  ...
}
-----------------Answer-----------------

Can you set up env params and then use those to control which apis your app talks to? You could use that to easily set development, test and production api addresses.


Previous : Path is wrong while running a job created for GitHub repository using webhook
Next : Graphical front-end for jmeter