Mock-Server for server/integrating systems

santosh
3 min readOct 4, 2020

--

There are scenario when a system is not ready & this can act as a blocker for development of UI or related backed system. Now what to do??

Fig1: what to do

We can mock the system with help of mock-server https://www.mock-server.com

Benefits of using mock-server

  1. Polyglot

It supports following ways to setup and customize the mock-server

-REST API

- Java

- JavaScript (both browser API & Node.js module)

2. Multiple way to run mock-server

- programmatically via a Java API in an @Before or @After method

- using a JUnit 4 @Rule via a @Rule annotated field in a JUnit 4 test

- using a JUnit 5 Test Extension via a @ExtendWith annotated JUnit 5 class

- As a Docker container in any Docker enabled environment

- via a Helm chart in any Kubernetes environment

- from the command line as a stand-alone process in a test environment

- via a Maven Plugin as part of a Maven build cycle

- As a Node.js (npm) module from any Node.js code

- As a Grunt plugin as part of a Grunt build cycle

- As a deployable WAR to an existing application server

3. Mockserver-UI

MockServer has a UI that can be used to view the internal state within MockServer, including:

- logs

- active expectations

- requests received

- proxied requests

Fig2. Mock-server UI

Hands on using Mock-Server

  1. Run mock server (using java command line)
  • java -jar <path to mockserver-netty-5.11.1-jar-with-dependencies.jar> -serverPort <port>

2. Verify mock server via mock-server ui. We can

open the url http://hostname:port/mockserver/dashboard

Fig 3: Mock-Server UI

3. Now we will setup few expectations using rest api

What is expectations :

An expectation defines the action that is taken, for example, a response could be returned

When MockServer receives a request it matches the request against active expectations that have been configured, if no matches are found it proxies the request if appropriate otherwise a 404 is returned.

3.1 Expectation :

Expectation to match a given path and method GET and return configured response

Curl to setup expectation

curl — location — request PUT ‘http://localhost:1080/mockserver/expectation' \
— header ‘Content-Type: application/x-www-form-urlencoded’ \
— data-raw ‘{
“httpRequest” : {
“method”: “GET”,
“path” : “/health”
},
“httpResponse” : {
“body” : “i am up “
}
}’

Fig 4: Expectation matching path /health

Now expectation is configured we can hit the url to get the response

curl — location — request GET ‘http://localhost:1080/health'

Fig 5: health url

3.2 Expectation match header using regex.

We can create expectation to match a request header with regex.

Curl to setup the same.

curl — location — request PUT ‘http://localhost:1080/mockserver/expectation' \
— header ‘Content-Type: text/plain’ \
— data-raw ‘{
“httpRequest”: {
“path”: “/some/path/regexheadermatch”,
“headers”: {
“Custom.*”: [ “” ]
}
},
“httpResponse”: {
“body”: “request matching header via regex”
}

}’

Now we can hit the url to see the response

Fig 6: Request with header matching via regex

On similar lines we can configure the mock-server using rest api

Meta Command on Mock-Server

  1. Getting java code for all active expectations

We can get java code for all expectation using rest api call

curl — location — request PUT ‘http://localhost:1080/mockserver/retrieve?type=ACTIVE_EXPECTATIONS&format=java'

Fig 7: java code for active expectations

Referrence

  1. https://www.mock-server.com/mock_server/getting_started.html#setup_expectations
  2. https://www.mock-server.com

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

santosh
santosh

No responses yet

Write a response