-
Notifications
You must be signed in to change notification settings - Fork 24
Running sMockin as a Proxy Server
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).
- 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.
For this example we'll use the /helloworld mock created under the Getting Started step.
-
Open a terminal window:
- Start by running the below cURL command to ensure the helloworld mock is running:
- curl -i -X GET http://localhost:8001/helloworld
This should return either "Hello World!" (or "Bonjour le Monde!" if you applied sequenced responses from the Applying Sequenced Responses step).
- Next make the following call to this non-existant endpoint:
- curl -i -X GET http://www.google.com/helloworld
This should as expected return a 404 response, indicating that the service does not exist.
-
Now try making the same call but this time routing it through the sMockin proxy server:
- curl -i -X GET http://www.google.com/helloworld --proxy http://localhost:8010
All being well, the proxy server should have intercepted the call and replaced the original 404 response with response from the /helloworld mock.
- Start by running the below cURL command to ensure the helloworld mock is running:
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:
- curl -i -X GET http://www.google.com/pets --proxy http://localhost:8010
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.