-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
programmatic/src/main/java/dgroomes/wiremock/RandomIntegerTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
programmatic/wiremock/scenarios/happy-path/mappings/__files/random-integer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |