WireMock on docker

What is covered

- Running a wiremock docker image shared to us by holomekc
                    (https://hub.docker.com/r/holomekc/wiremock-gui)
- Open web UI and use it
- Overcome errors (Http failure response for /__admin/mappings/save: 500 Server Error)

What is wiremock

Wiremock is a HTTP mock tool to
- mock HTTP request
- record HTTP calls to a server and then able to replay these calls

For detail information: refer.

Wiremock has a UI?

Docker is all about usability. Holomekc has been gracious enough to allow us to use "web UI" he has developed through his docker image.

Why do I use this docker image? Because when writing this post, this image was most popular.

Running wiremock through docker image

Pull latest docker image
    docker pull holomekc/wiremock-gui

At time of writing this post, latest version was 2.25.1

Run wiremock and expose it on local port 8090. We name our container "wiremock"
    docker run -p 9080:8080 -d --name wiremock holomekc/wiremock-gui

Using wiremock UI

  1. Open in your browser: http://localhost:9080/__admin/webapp.
  2. This opens "Mappings" tab. We shall define our mappings here.
  3. Click on "New". This opens a JSON mapping for mocking. 
  4. Set request.url as: "/sayHello". Set response.body as "Hello my friend. Wiremock is working as expected."
  5. Click on "Save" button which is in place of "New" button.
  6. Open http://localhost:9080/sayHello in browser. And response should be what we had set in Step 4.
Wiremock has capability or request parameter matching, header matching etc. All this is wiremocks capability documented at http://wiremock.org/docs/.

Created mock mappings (also called stubbings by wiremock) will be lost when we restart docker container. This is because we never saved these mappings. On UI > Mappings page, there is a button "Mappings". Clicking on it opens a menu which has "Save Mappings" button. Click on it to save these mappings.

Overcome errors

Unable to save mapping

When I started working, my mappings weren't saved. I got error "Http failure response for http://localhost:9080/__admin/mappings/save: 500 Server Error"

So I first checked the logs:
    docker logs -f wiremock

I could see error 
    2020-03-22 23:33:44.686 Unrecoverable error handling admin request
    java.lang.RuntimeException: ./mappings does not exist

This meant that ./mappings isn't available.
In Docker Compose file of holomekc (https://github.com/holomekc/wiremock), there is a flag to set default folder: "--root-dir=/home/wiremock/storage" which didn't work for me.
Clearly in my case, application is running from "/" and is expecting a folder "/mappings".

So I have to create one. If I login as default user (wiremock user created through dockerfile), I cannot create a folder on /. So login as a root user
    docker exec -u 0 -ti wiremock /bin/bash

Once logged in as "root", go to /, and create "mappings" folder. chmod it to allow wiremock user to use it. Since it is my laptop, I could 
    chmod 777 mappings
Save mapping on UI.
And see a file generated in /mappings

You could stop and start docker wiremock container to see if our saving worked.

Comments