Standalone mocking webservice with dynamically response

77 views
Skip to first unread message

Nattasan Wang-Aphisit

unread,
May 10, 2021, 7:45:13 AM5/10/21
to wiremock-user
My scope is 
- to make a mocking web server for be called from my current project.
- need to response for specific request specs: 'path', 'method', etc...
- then it can read the value from request and can use those values to the response

I've tried to make with reading the docs but still confuse, what I've tried are
- make a standalone project then read from 'mappings' folder
- from above make a new stub with expecting url and return with 'aResponse()`, no clue how to read value from request?
- make a class extends to 'ResponseDefinitionTransformer' but it not reflect on '/__admin'

All reply would be appreciate.

aaron...@willowtreeapps.com

unread,
May 10, 2021, 9:44:24 AM5/10/21
to wiremock-user
So, it sounds like you are mixing the two ways of creating responses - both in code and standalone. By using the standalone and having your mappings created from a `mappings/` folder, your responses will be placed inside of those mapping jsons, instead of created in Java.

```
{
  "request": {
    "url": "/test",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "body": "{ \"response\": \"body\" }"
  }
}

```
Additionally, you can point your responses to files in the `__files/` folder by using `bodyFileName` instead of `body`. (This allows you to more neatly store and edit your responses).

If you've correctly made a custom ResponseDefinitionTransformer, you'll need to attach _the compiled class_ to the WireMock JAR on startup. The documentation is somewhat _iffy_ on how exactly to do this, but in my current project, we do it with a simple java command:
```
java -cp "wiremock-jre8-standalone-2.26.3.jar:target/*project-name*-1.0-SNAPSHOT-jar-with-dependencies.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --port 8089 --verbose --global-response-templating --extensions path.to.compiled.extensions.class
```
The exact location of these JARs and classes can be a little confusing, but the standard WireMock JAR is in the root folder, the `target/*project-name*` JAR is in the `target/` folder, and the compiled classes are located there as well. (Also, it obviously depends on how you're compiling the classes, but it should just be re-assembling the project. In my above example, I compile everything by using `mvn clean compile assembly:single`) (Also, my exact knowledge on compilation/assembly is iffy at best, so I may be using the wrong words -- sorry.)
Reply all
Reply to author
Forward
0 new messages