3

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

7
  • 1
    Wouldn't it be more straightforward to be able to configure the URLs for the services the app requires? Then you can point it to whatever you need to in any environment. Also what does "everything mocks if we create a mock object and then calls the mock object" mean?
    – jonrsharpe
    Commented Sep 3, 2020 at 10:49
  • I think that there are two ways. Either proxy that forwards the requests or changing local domain name resolution in hosts file. However in both cases you will have to disable certificate validation if the service is https one.
    – Alexey R.
    Commented Sep 3, 2020 at 10:51
  • Also why don't you want to edit the hosts file? If you've considered and dismissed options, what are the constraints or criteria leading to that?
    – jonrsharpe
    Commented Sep 3, 2020 at 10:53
  • security team doen't allow host file modificaton or proxy don't want us to modify
    – PDHide
    Commented Sep 3, 2020 at 11:01
  • 3
    You're between a rock and a hard place, then. You'll either need to get those two sets of stakeholders to agree, or make the app configurable (note that accessing services via a URL in the config is a recommended pattern for modern apps anyway, see e.g. 12factor.net/backing-services).
    – jonrsharpe
    Commented Sep 3, 2020 at 11:06

4 Answers 4

1

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/

5
  • Hi Dev team was telling we cannot use localhost due to cross domain limitation any suggestions
    – PDHide
    Commented Sep 4, 2020 at 11:17
  • 1
    Host it on a shared internal virtual server, host it on a cloud, or try my-json-server.typicode.com Commented Sep 4, 2020 at 11:19
  • You have any cloud suggestions
    – PDHide
    Commented Sep 5, 2020 at 8:27
  • @PDHide Any cloud providing virtual linux servers you can install json-server on would work. AWS, DigitalOcean or my favorite hetzner.com/cloud Commented Sep 5, 2020 at 8:38
  • @PDHide I can understand that maintaining your own virtual linux server might be extra work, opt for platform cloud servers like run.pivotal.io/pricing or Heroku elements.heroku.com/buttons/eecs130/json-server-heroku Heroku's free tier might even been good enough for your use-case. You could use your deployment pipeline to push the latest test-data changes automaticly to an Heroku json-server instance. Commented Sep 5, 2020 at 8:42
1

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.

1

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"
    ...
  },
  ...
}
3
  • Both will mock inside unit test only , I need it as a hosted service in cloud so anyone can access
    – PDHide
    Commented Sep 5, 2020 at 8:26
  • 1
    @PHide That's not true. Wiremock can be run as a standalone server: wiremock.org/docs/running-standalone
    – dzieciou
    Commented Sep 5, 2020 at 8:43
  • Thanks for this will read about it
    – PDHide
    Commented Sep 5, 2020 at 9:06
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.