Skip to content

Running sMockin as a Proxy Server

Matthew Gallina edited this page Dec 12, 2019 · 17 revisions

Sometimes pointing parts of an existing application to a mock server can tricky. Say for example, Application A needs to talk to some new services in Application B, chances are there will only by a single configurable url instructing A where to find B.

To help address this scenario, sMockin can be configured to sit in the middle of A and B, running as a proxy server. When set up this way, sMockin will continue to route any calls from A to B, unless the call from A matches a mock, in which case the mock will be returned to A instead.

This can be useful in the following 2 scenarios:

  • Where a brand new service is required and therefore not present in B.
  • Where there is a need to override the behavior of an existing HTTP service (in B).

Enabling the sMockin Proxy Server

  • Open http://localhost:8000/index.html in your browser
  • Go to the HTTP tab and click (edit settings)
  • In the HTTP Mock Server Config window, check the Enable Proxy Server checkbox
  • Click the Save button
  • From the dashboard, click on the play button (or stop and then start if the server is already running)

This should enable both the HTTP mock server on port 8001 and the proxt server on 8010.


Using the Proxy Server

For this example we'll use the /helloworld mock created under the Getting Started step.

  • Open a terminal window:

    This should return either "Hello World!" (or "Bonjour le Monde!" if you applied sequenced responses from the Applying Sequenced Responses step).

    This should as expected return a 404 response, indicating that the service does not exist.

    All being well, the proxy server should have intercepted the call and replaced the original 404 response with response from the /helloworld mock.


Proxying with Rule Based Mocks

It is possible to configure HTTP Rules Based mocks to only substitute the original response with a mock should a rule condition have been matched.

To expand on this, consider we have the mock GET /pets which returns ["cat", "dog", "mouse"] by default and has a rule to return ["mouse"] should the following request parameter be present GET /pets?type=rodent.

Were there now a real endpoint called www.google.com/pets and we were to call this whilst running the request through sMockin as a proxy server, i.e:

the response would always be intercepted and swapped out with the mock.

We can however change this behavior, so that we receive the original response from http://www.google.com/pets and only get a mocked response if the /pets?type=rodent is matched.

To do this:

  • Open http://localhost:8000/index.html in your browser
  • Go to the HTTP tab
  • Search for the HTTP Rules Based mock in question and click the View button
  • At the bottom of the HTTP Endpoint page there is a checkbox labelled Only intercept if rule matched
  • Enable this (which should disable all of the Default based fields) and click Save to apply the change
  • Finally restart the mock server to put the change into effect.