Skip to content

Commit

Permalink
fix: support accessing query parameters for state handlebar extension (
Browse files Browse the repository at this point in the history
…#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
-->
  • Loading branch information
dirkbolte authored Apr 13, 2024
1 parent 2276ca1 commit 2d4399d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
25 changes: 24 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,27 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: jacoco-report
path: build/reports/jacoco/
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
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void postContext(String contextName, Map<String, Object> body) {
private void getContext(String contextName, Consumer<Map<String, Object>> assertion) {
Map<String, Object> 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));
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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() {
Expand Down

0 comments on commit 2d4399d

Please sign in to comment.