diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index b67de42..e745122 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -69,4 +69,24 @@ 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: + 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() {