Midway: Simplifying Mocked Responses

  • harder to understand
  • harder to maintain

How it Works?

  1. api-message-GET.json - This will be returned for all the calls for the first endpoint with default HTTP response code 200
  2. api-product-getStatus-GET.html - This will be returned for all the calls for the second endpoint with default HTTP response code 200, except for the second as it has its own file
  3. api-product-getStatus-GET-2-code-201.json - This will be returned for the second call for the second endpoint with response code 201.
midway.setMockId(“test1”);or curl http://localhost:8000/_admin/api/midway/setMockId/test1
  • JSON
  • HTML
  • TXT
  • First file encountered of any other extension.

Good To Know

 midway.util.respondWithFile(this, reply,{filePath:‘./default.json’}
  • SetMockId
  • Custom File Path for default or variants endpoints.
  • File based on URL Path for default or variants endpoints.

Other Utilities

  • ResetCount — To reset the URL called count to zero and does not impact current mock id. After this, Midway will start counting the endpoints call count from zero.
midway.resetCount();or curl http://localhost:8000/_admin/api/midway/resetURLCount
  • ResetMockId — This removes the current SetMockId and sets the URL counts to zero
midway.resetMockId();orcurl http://localhost:8000/_admin/api/midway/resetMockId
  • GetMockId — To get the current mock-id set for Midway-Server
midway.getMockId();orcurl http://localhost:8000/_admin/api/midway/getMockId
  • GetURLCount — To get URL count for all Rest APIs mocked by Midway-Server
midway.getURLCount();or curl http://localhost:8000/_admin/api/midway/getURLCount

Advantages of Mock from Container approach:

  • Stub Multiple Rest APIs — Drop as many files in the mock folder as your scenario requires based on the Rest API path.
  • Mock Rest APIs with Multiple Calls & Different Data Expectancy — Add the call count to the file name and that file will be returned based on the time the API is being called.
  • Reduced Test Complexity — Test are decoupled from the setting mock responses and thus making test easy to read.
  • No Variants — For multiple calls to the same API, a new file can be added for a different data set and thus removes the need of creating any variants.
  • Easy Maintenance — If any changes happen, only files content or name needs to be changed.
  • Developers Paradise — Makes developing a feature super easy as no code needs to be changed if mock behavior needs to change.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store