tomakehurst/wiremock — pull request #213 by marcingrzejszczak (2014-12-19 01:02:43+00:00)
Title: Groovy request stubbing

Hi!

This is a preeliminary suggestion of adding Groovy support for Wiremock.

The concept is such that instead of providing all the parameters of request mappings like this:

```
@Test
public void matchingOnRequestBodyWithTwoRegexes() {
stubFor(put(urlEqualTo("/match/this/body"))
.withRequestBody(matching(".*Blah.*"))
.withRequestBody(matching(".*@[0-9]{5}@.*"))
.willReturn(aResponse()
.withStatus(HTTP_OK)
.withBodyFile("plain-example.txt")));

WireMockResponse response = testClient.putWithBody("/match/this/body", "Blah...but not the rest", "text/plain");
assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
response = testClient.putWithBody("/match/this/body", "@12345@...but not the rest", "text/plain");
assertThat(response.statusCode(), is(HTTP_NOT_FOUND));

response = testClient.putWithBody("/match/this/body", "BlahBlah@56565@Blah", "text/plain");
assertThat(response.statusCode(), is(HTTP_OK));
}
```

you can write one using Groovy

```
@Test
public void matchingOnRequestBodyByGroovyPattern() {
stubFor(groovy("method == 'PUT' && url == '/match/this/body' && body.matches('.*Blah.*') && body.matches('.*@[0-9]{5}@.*')")
.willReturn(aResponse()
.withStatus(HTTP_OK)));

WireMockResponse response = testClient.putWithBody("/match/this/body", "Blah...but not the rest", "text/plain");
assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
response = testClient.putWithBody("/match/this/body", "@12345@...but not the rest", "text/plain");
assertThat(response.statusCode(), is(HTTP_NOT_FOUND));

response = testClient.putWithBody("/match/this/body", "BlahBlah@56565@Blah", "text/plain");
assertThat(response.statusCode(), is(HTTP_OK));
}
```

Of course the same applies to JSON files.

This is only a suggestion but I'm really open to discussions and hints on final API.