Skip to content

Commit 2d4399d

Browse files
authored
fix: support accessing query parameters for state handlebar extension (#127)
- always call `toString` when accessing an option hash <!-- Please describe your pull request here. --> ## References - #126 <!-- References to relevant GitHub issues and pull requests, esp. upstream and downstream changes --> ## Submitter checklist - [ ] Recommended: Join [WireMock Slack](https://slack.wiremock.org/) to get any help in `#help-contributing` or a project-specific channel like `#wiremock-java` - [ ] 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 2276ca1 commit 2d4399d

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

.github/workflows/build-and-test.yml

+24-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,27 @@ jobs:
6969
uses: actions/upload-artifact@v3
7070
with:
7171
name: jacoco-report
72-
path: build/reports/jacoco/
72+
path: build/reports/jacoco/
73+
74+
archive:
75+
name: Archive job results
76+
runs-on: ubuntu-latest
77+
steps:
78+
- uses: actions/checkout@v4
79+
with:
80+
fetch-depth: 0
81+
- name: Set up JDK
82+
uses: actions/setup-java@v3
83+
with:
84+
java-version: 11
85+
distribution: 'temurin'
86+
- name: Run the Gradle package task
87+
uses: gradle/gradle-build-action@v2
88+
with:
89+
arguments: jar shadowjar
90+
- name: Archive production artifacts
91+
uses: actions/upload-artifact@v4
92+
with:
93+
name: jars
94+
path: |
95+
build/libs/*.jar

src/main/java/org/wiremock/extensions/state/extensions/StateHandlerbarHelper.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public StateHandlerbarHelper(ContextManager contextManager) {
5151

5252
@Override
5353
public Object apply(Object o, Options options) {
54-
String contextName = options.hash("context");
55-
String property = options.hash("property");
56-
String list = options.hash("list");
57-
String defaultValue = options.hash("default");
54+
String contextName = Optional.ofNullable(options.hash("context")).map(Object::toString).orElse(null);
55+
String property = Optional.ofNullable(options.hash("property")).map(Object::toString).orElse(null);
56+
String list = Optional.ofNullable(options.hash("list")).map(Object::toString).orElse(null);
57+
String defaultValue = Optional.ofNullable(options.hash("default")).map(Object::toString).orElse(null);
5858
if (StringUtils.isEmpty(contextName)) {
5959
return handleError("'context' cannot be empty");
6060
}

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void postContext(String contextName, Map<String, Object> body) {
6060
private void getContext(String contextName, Consumer<Map<String, Object>> assertion) {
6161
Map<String, Object> result = given()
6262
.accept(ContentType.JSON)
63-
.get(assertDoesNotThrow(() -> new URI(String.format("%s/%s/%s", wm.getRuntimeInfo().getHttpBaseUrl(), "contexturl", contextName))))
63+
.get(assertDoesNotThrow(() -> new URI(String.format("%s/%s/%s?contextName=%s", wm.getRuntimeInfo().getHttpBaseUrl(), "contexturl", contextName, contextName))))
6464
.then()
6565
.statusCode(HttpStatus.SC_OK)
6666
.extract().body().as(mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class));
@@ -165,6 +165,7 @@ public void test_noExtensionUsage_ok() throws JsonProcessingException, URISyntax
165165
public class ConfigurationErrors {
166166

167167
private final String contextName = "contextName";
168+
168169
@DisplayName("fails on missing context")
169170
@Test
170171
public void test_missingContext_fail() {
@@ -561,12 +562,43 @@ public void test_listSizeWithDefault() {
561562
}
562563
}
563564

565+
@DisplayName("with request query")
566+
@Nested
567+
public class RequestQuery {
568+
569+
private final String contextName = "aContextName";
570+
571+
@DisplayName("works for context")
572+
@Test
573+
void test_contextFromQuery() {
574+
Map<String, Object> request = Map.of("contextValue", "aContextValue");
575+
createContextStatePostStub(Map.of("contextValue", "{{jsonPath request.body '$.contextValue'}}"));
576+
createContextGetStub(Map.of("contextValue", "{{state context=request.query.contextName property='contextValue'}}"));
577+
578+
postContext(contextName, request);
579+
getContext(contextName, (result) -> assertThat(result).containsExactlyEntriesOf(request));
580+
}
581+
582+
@DisplayName("works for property")
583+
@Test
584+
void test_contextFromProperty() {
585+
Map<String, Object> request = Map.of("contextValue", "aContextValue");
586+
createContextStatePostStub(Map.of(contextName, "{{jsonPath request.body '$.contextValue'}}"));
587+
createContextGetStub(Map.of("contextValue", String.format("{{state context=request.query.contextName property='%s'}}", contextName)));
588+
589+
postContext(contextName, request);
590+
getContext(contextName, (result) -> assertThat(result).containsExactlyEntriesOf(request));
591+
}
592+
593+
}
594+
564595
@DisplayName("with existing property")
565596
@Nested
566597
public class ExistingProperty {
567598

568599
private final String contextName = "aContextName";
569600

601+
570602
@DisplayName("returns property from previous request")
571603
@Test
572604
void test_returnsState() {

0 commit comments

Comments
 (0)