Skip to content

Commit

Permalink
Custom transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroomes committed May 11, 2021
1 parent 391c92e commit 793573e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ various configurations. Use it like a recipe book for when you write your own Wi
Instructions:

* Use Java 11
* Run with `./gradlew programmatic:run`
* Make a request to a stubbed endpoint with `curl --request GET --url http://localhost:8070/message`
* Run:
* `./gradlew programmatic:run`
* Make requests to the stubbed endpoints:
* `curl --request GET --url http://localhost:8070/message`
* `curl --request GET --url http://localhost:8070/random-integer`
* Observe the Jetty server statistics by going to <http://localhost:8070/stats/> in your browser
* Statistics include things like the number of responses with 200/300/400/500 status codes, the number of connections,
and the amount of memory used by the underlying Jetty server.
Expand All @@ -44,7 +47,7 @@ Instructions:
General clean-ups, changes and things I wish to implement for this project:

* Make the sub-projects completely standalone and adhere to the convention I have in my other playground repos.
* Add a stub with a custom handler that uses custom Java code to respond to the request.
* DONE Add a stub with a custom handler that uses custom Java code to respond to the request.
* Create a WireMock-in-Docker example
* DONE create a sub-project that declares dependency constraint versions and which will be used as a `platform(...)`
from the other projects. For details of this feature, see the [Gradle docs about "platform"](https://docs.gradle.org/current/userguide/platforms.html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ public class ProgrammaticMain {
private static final Logger log = LoggerFactory.getLogger(ProgrammaticMain.class);
private static final int SLEEP_SECONDS = 30;
private static final int PORT = 8070;
// private static final String ROOT_DIR = "wiremock/scenarios/happy-path";
private static final String ROOT_DIR = "wiremock/scenarios/occasional-failure";
private static final String ROOT_DIR = "wiremock/scenarios/happy-path";
// private static final String ROOT_DIR = "wiremock/scenarios/occasional-failure";

public static void main(String[] args) throws InterruptedException {
var start = Instant.now();
var options = new WireMockConfiguration()
.port(PORT);
WireMockUtil.configureStatistics(options);
WireMockUtil.configureRootDir(options, ROOT_DIR);
WireMockUtil.configureExtensions(options);
WireMockServer wireMockServer = new WireMockServer(options);
log.debug("Starting the WireMock server");
wireMockServer.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dgroomes.wiremock;

import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;

import java.util.Random;

/**
* Return a random integer. Optionally, when a "limit" argument is supplied, use that as the exclusive maximum value.
*/
public class RandomIntegerTransformer extends ResponseTransformer {

private Random random = new Random();

@Override
public String getName() {
return "random-integer-transformer";
}

@Override
public boolean applyGlobally() {
return false;
}

private String template = "{\n" +
" \"random_integer\":" +
" %d\n" +
"}";

@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
if (parameters == null) {
parameters = new Parameters();
}

var limit = ((int) parameters.getOrDefault("limit", 100));
var randomInt = random.nextInt(limit);
return Response.response()
.body(String.format(template, randomInt))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,8 @@ public static void configureRootDir(WireMockConfiguration options, String rootDi

options.withRootDirectory(rootDir);
}

public static void configureExtensions(WireMockConfiguration options) {
options.extensions(new RandomIntegerTransformer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"request": {
"urlPathPattern": "/random-integer(/?)",
"method": "GET"
},
"response": {
"transformers": [
"random-integer-transformer"
]
}
}

0 comments on commit 793573e

Please sign in to comment.