From 2d4399d2b665f448e2ef3f49d526f13fc5b65b50 Mon Sep 17 00:00:00 2001 From: Dirk Bolte Date: Sat, 13 Apr 2024 08:52:34 +0200 Subject: [PATCH] fix: support accessing query parameters for state handlebar extension (#127) - always call `toString` when accessing an option hash ## References - #126 ## 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) --- .github/workflows/build-and-test.yml | 25 +++++++++++++- .../extensions/StateHandlerbarHelper.java | 8 ++--- ...teTemplateHelperProviderExtensionTest.java | 34 ++++++++++++++++++- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index b67de42..a5a88ff 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -69,4 +69,27 @@ jobs: uses: actions/upload-artifact@v3 with: name: jacoco-report - path: build/reports/jacoco/ \ No newline at end of file + path: build/reports/jacoco/ + + archive: + name: Archive job results + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up JDK + uses: actions/setup-java@v3 + with: + java-version: 11 + distribution: 'temurin' + - name: Run the Gradle package task + uses: gradle/gradle-build-action@v2 + with: + arguments: jar shadowjar + - name: Archive production artifacts + uses: actions/upload-artifact@v4 + with: + name: jars + path: | + build/libs/*.jar diff --git a/src/main/java/org/wiremock/extensions/state/extensions/StateHandlerbarHelper.java b/src/main/java/org/wiremock/extensions/state/extensions/StateHandlerbarHelper.java index e970e32..ef90ba8 100644 --- a/src/main/java/org/wiremock/extensions/state/extensions/StateHandlerbarHelper.java +++ b/src/main/java/org/wiremock/extensions/state/extensions/StateHandlerbarHelper.java @@ -51,10 +51,10 @@ public StateHandlerbarHelper(ContextManager contextManager) { @Override public Object apply(Object o, Options options) { - String contextName = options.hash("context"); - String property = options.hash("property"); - String list = options.hash("list"); - String defaultValue = options.hash("default"); + String contextName = Optional.ofNullable(options.hash("context")).map(Object::toString).orElse(null); + String property = Optional.ofNullable(options.hash("property")).map(Object::toString).orElse(null); + String list = Optional.ofNullable(options.hash("list")).map(Object::toString).orElse(null); + String defaultValue = Optional.ofNullable(options.hash("default")).map(Object::toString).orElse(null); if (StringUtils.isEmpty(contextName)) { return handleError("'context' cannot be empty"); } diff --git a/src/test/java/org/wiremock/extensions/state/functionality/StateTemplateHelperProviderExtensionTest.java b/src/test/java/org/wiremock/extensions/state/functionality/StateTemplateHelperProviderExtensionTest.java index 0bc0d02..2b46ac2 100644 --- a/src/test/java/org/wiremock/extensions/state/functionality/StateTemplateHelperProviderExtensionTest.java +++ b/src/test/java/org/wiremock/extensions/state/functionality/StateTemplateHelperProviderExtensionTest.java @@ -60,7 +60,7 @@ private void postContext(String contextName, Map body) { private void getContext(String contextName, Consumer> assertion) { Map result = given() .accept(ContentType.JSON) - .get(assertDoesNotThrow(() -> new URI(String.format("%s/%s/%s", wm.getRuntimeInfo().getHttpBaseUrl(), "contexturl", contextName)))) + .get(assertDoesNotThrow(() -> new URI(String.format("%s/%s/%s?contextName=%s", wm.getRuntimeInfo().getHttpBaseUrl(), "contexturl", contextName, contextName)))) .then() .statusCode(HttpStatus.SC_OK) .extract().body().as(mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class)); @@ -165,6 +165,7 @@ public void test_noExtensionUsage_ok() throws JsonProcessingException, URISyntax public class ConfigurationErrors { private final String contextName = "contextName"; + @DisplayName("fails on missing context") @Test public void test_missingContext_fail() { @@ -561,12 +562,43 @@ public void test_listSizeWithDefault() { } } + @DisplayName("with request query") + @Nested + public class RequestQuery { + + private final String contextName = "aContextName"; + + @DisplayName("works for context") + @Test + void test_contextFromQuery() { + Map request = Map.of("contextValue", "aContextValue"); + createContextStatePostStub(Map.of("contextValue", "{{jsonPath request.body '$.contextValue'}}")); + createContextGetStub(Map.of("contextValue", "{{state context=request.query.contextName property='contextValue'}}")); + + postContext(contextName, request); + getContext(contextName, (result) -> assertThat(result).containsExactlyEntriesOf(request)); + } + + @DisplayName("works for property") + @Test + void test_contextFromProperty() { + Map request = Map.of("contextValue", "aContextValue"); + createContextStatePostStub(Map.of(contextName, "{{jsonPath request.body '$.contextValue'}}")); + createContextGetStub(Map.of("contextValue", String.format("{{state context=request.query.contextName property='%s'}}", contextName))); + + postContext(contextName, request); + getContext(contextName, (result) -> assertThat(result).containsExactlyEntriesOf(request)); + } + + } + @DisplayName("with existing property") @Nested public class ExistingProperty { private final String contextName = "aContextName"; + @DisplayName("returns property from previous request") @Test void test_returnsState() {