WireMock 101
WireMock is a simulator for HTTP-based APIs.
It enables you to stay productive when an API you depend on does not exist or not complete.
We use WireMock standalone jar as startup in this post. Download the jar file from http://wiremock.org, the last version is 2.17.0
, run the WireMock process via command java -jar wiremock-standalone-2.17.0.jar
.
There are several commond line options such set the port number, refer at Running as a Standalone Process.
WireMock Folder
__files
Html, js, css, picture files.mappings
Json files.
Put a json file with below content into the mappings
folder.
{
"request": {
"method": "GET",
"url": "/api/mytest"
},
"response": {
"status": 200,
"body": "More content\n"
}
}
And try to fetch the API via GET
:
http://localhost:8080/api/mytest
Record && Playback
We use the douban API to fetch online movie for test:
https://api.douban.com/v2/movie/in_theaters
Record
-
Web Recoder
http://localhost:8080/__admin/recorder
Fill into the root proxy domain:https://api.douban.com
Then make a request through WireMock to the target API viaGET
:
http://localhost:8080/v2/movie/in_theaters
-
Command Line Tool Use command line instead of Web GUI:
java -jar wiremock-standalone-2.17.0.jar --proxy-all="https://api.douban.com" --record-mappings --verbose
Playback
After Record
, there will have a file with name v2_movie_in_theaters-xxxxxx.json
in the mappings
folder.
Disable network and try to fetch:
http://localhost:8080/v2/movie/in_theaters
Thanks!