Skip to content

Commit a5b8a2b

Browse files
authored
Adding test and example for recording and retrieving a complete response body (#7)
- adding explicit test - extending README <!-- Please describe your pull request here. --> ## References - TODO <!-- References to relevant GitHub issues and pull requests, esp. upstream and downstream changes --> ## Submitter checklist - [ ] The PR request is well described and justified, including the body and the references - [ ] The PR title represents the desired changelog entry - [ ] The repository's code style is followed (see the contributing guide) - [ ] Test coverage that demonstrates that the change works as expected - [ ] For new features, there's necessary documentation in this pull request or in a subsequent PR to [wiremock.org](https://github.com/wiremock/wiremock.org) <!-- Put an `x` into the [ ] to show you have filled the information. The template comes from https://github.com/wiremock/.github/blob/main/.github/pull_request_template.md You can override it by creating .github/pull_request_template.md in your own repository -->
1 parent cfa2b27 commit a5b8a2b

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ The state is recorded in `withServeEventListener` of a stub. The following param
159159
{
160160
"id": "{{jsonPath response.body '$.id'}}",
161161
"firstName": "{{jsonPath request.body '$.firstName'}}",
162-
"lastName": "{{jsonPath request.body '$.lastName'}}"
162+
"lastName": "{{jsonPath request.body '$.lastName'}}",
163163
}
164164
```
165165

@@ -192,6 +192,26 @@ Full example:
192192
}
193193
]
194194
}
195+
```
196+
197+
To record a complete response body, use:
198+
199+
```json
200+
{
201+
"request": {},
202+
"response": {},
203+
"withServeEventListener": [
204+
{
205+
"name": "recordState",
206+
"parameters": {
207+
"context": "{{jsonPath response.body '$.id'}}",
208+
"state": {
209+
"fullBody": "{{jsonPath response.body '$'}}"
210+
}
211+
}
212+
}
213+
]
214+
}
195215

196216
```
197217

@@ -310,6 +330,8 @@ The handler has two parameters:
310330
- `context`: has to match the context data was registered with
311331
- `property`: the property of the state context to retrieve, so e.g. `firstName`
312332

333+
To retrieve a full body, use: `{{{state context=request.pathSegments.[1] property='fullBody'}}}` .
334+
313335
### Error handling
314336

315337
Missing Helper properties as well as unknown context properties are reported as error. Wiremock renders them in the field, itself, so there won't be an

src/test/java/org/wiremock/extensions/state/StateTemplateHelperProviderExtensionTest.java

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
import org.junit.jupiter.api.TestInstance;
3232
import org.junit.jupiter.api.extension.RegisterExtension;
3333
import org.junit.jupiter.api.parallel.Execution;
34-
import org.wiremock.extensions.state.extensions.DeleteStateEventListener;
35-
import org.wiremock.extensions.state.extensions.RecordStateEventListener;
36-
import org.wiremock.extensions.state.extensions.StateTemplateHelperProviderExtension;
3734

3835
import java.net.URI;
3936
import java.net.URISyntaxException;
@@ -47,6 +44,8 @@
4744
import static io.restassured.RestAssured.given;
4845
import static org.assertj.core.api.Assertions.assertThat;
4946
import static org.hamcrest.Matchers.equalTo;
47+
import static org.hamcrest.Matchers.notNullValue;
48+
import static org.hamcrest.Matchers.nullValue;
5049
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
5150

5251
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -112,13 +111,13 @@ void test_unknownProperty_fail() throws JsonProcessingException, URISyntaxExcept
112111

113112
createPostStub();
114113
wm.stubFor(
115-
get(urlPathMatching(TEST_URL + "/[^/]+"))
114+
get(urlPathMatching(TEST_URL + "/value/[^/]+"))
116115
.willReturn(
117116
WireMock.ok()
118117
.withHeader("content-type", "application/json")
119118
.withJsonBody(
120119
mapper.readTree(
121-
mapper.writeValueAsString(Map.of("value", "{{state context=request.pathSegments.[1] property='unknownValue'}}")))
120+
mapper.writeValueAsString(Map.of("value", "{{state context=request.pathSegments.[2] property='unknownValue'}}")))
122121
)
123122
)
124123
);
@@ -138,6 +137,17 @@ void test_returnsStateFromPreviousRequest_ok() throws JsonProcessingException, U
138137
getAndAssertContextValue(context, contextValue);
139138
}
140139

140+
@Test
141+
void test_returnsFullBodyFromPreviousRequest_ok() throws JsonProcessingException, URISyntaxException {
142+
var contextValue = RandomStringUtils.randomAlphabetic(5);
143+
144+
createPostStub();
145+
createGetStub();
146+
147+
var context = postAndAssertContextValue(contextValue);
148+
getAndAssertFullBody(context, contextValue);
149+
}
150+
141151
@Test
142152
void test_differentStatesSupported_ok() throws JsonProcessingException, URISyntaxException {
143153
var contextValueOne = RandomStringUtils.randomAlphabetic(5);
@@ -154,16 +164,28 @@ void test_differentStatesSupported_ok() throws JsonProcessingException, URISynta
154164

155165
private void createGetStub() throws JsonProcessingException {
156166
wm.stubFor(
157-
get(urlPathMatching(TEST_URL + "/[^/]+"))
167+
get(urlPathMatching(TEST_URL + "/value/[^/]+"))
158168
.willReturn(
159169
WireMock.ok()
160170
.withHeader("content-type", "application/json")
161171
.withJsonBody(
162172
mapper.readTree(
163-
mapper.writeValueAsString(Map.of("value", "{{state context=request.pathSegments.[1] property='stateValue'}}")))
173+
mapper.writeValueAsString(Map.of(
174+
"value", "{{state context=request.pathSegments.[2] property='stateValue'}}"
175+
)
176+
)
177+
)
164178
)
165179
)
166180
);
181+
wm.stubFor(
182+
get(urlPathMatching(TEST_URL + "/full/[^/]+"))
183+
.willReturn(
184+
WireMock.ok()
185+
.withHeader("content-type", "application/json")
186+
.withBody("{{{state context=request.pathSegments.[2] property='stateBody'}}}")
187+
)
188+
);
167189
}
168190

169191
private void createPostStub() throws JsonProcessingException {
@@ -174,7 +196,13 @@ private void createPostStub() throws JsonProcessingException {
174196
.withHeader("content-type", "application/json")
175197
.withJsonBody(
176198
mapper.readTree(
177-
mapper.writeValueAsString(Map.of("id", "{{randomValue length=32 type='ALPHANUMERIC' uppercase=false}}")))
199+
mapper.writeValueAsString(Map.of(
200+
"id", "{{randomValue length=32 type='ALPHANUMERIC' uppercase=false}}",
201+
"contextValue", "{{jsonPath request.body '$.contextValue'}}",
202+
"other", "randomValue length=32 type='ALPHANUMERIC'"
203+
)
204+
)
205+
)
178206
)
179207
)
180208
.withServeEventListener(
@@ -183,7 +211,8 @@ private void createPostStub() throws JsonProcessingException {
183211
Map.of(
184212
"context", "{{jsonPath response.body '$.id'}}",
185213
"state", Map.of(
186-
"stateValue", "{{jsonPath request.body '$.contextValue'}}"
214+
"stateValue", "{{jsonPath request.body '$.contextValue'}}",
215+
"stateBody", "{{{jsonPath response.body '$'}}}"
187216
)
188217
)
189218
)
@@ -192,15 +221,25 @@ private void createPostStub() throws JsonProcessingException {
192221
}
193222

194223
private void getAndAssertContextValue(String context, String contextValue) throws URISyntaxException {
195-
var responseValue = given()
224+
given()
196225
.accept(ContentType.JSON)
197-
.get(new URI(String.format("%s%s/%s", wm.getRuntimeInfo().getHttpBaseUrl(), TEST_URL, context)))
226+
.get(new URI(String.format("%s%s/value/%s", wm.getRuntimeInfo().getHttpBaseUrl(), TEST_URL, context)))
198227
.then()
199228
.statusCode(HttpStatus.SC_OK)
200229
.body("value", equalTo(contextValue))
201-
.extract()
202-
.body()
203-
.jsonPath().get("value");
230+
.body("other", nullValue());
231+
}
232+
233+
private void getAndAssertFullBody(String context, String contextValue) throws URISyntaxException {
234+
given()
235+
.accept(ContentType.JSON)
236+
.get(new URI(String.format("%s%s/full/%s", wm.getRuntimeInfo().getHttpBaseUrl(), TEST_URL, context)))
237+
.then()
238+
.statusCode(HttpStatus.SC_OK)
239+
.body("id", equalTo(context))
240+
.body("contextValue", equalTo(contextValue))
241+
.body("other", notNullValue())
242+
.body("value", nullValue());
204243
}
205244

206245
private String postAndAssertContextValue(String contextValue) throws URISyntaxException {

0 commit comments

Comments
 (0)