Skip to content

Commit

Permalink
✨ added withRequest for remote parameter #2
Browse files Browse the repository at this point in the history
  • Loading branch information
nilwurtz committed Sep 8, 2023
1 parent 3fd1252 commit e52279b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

## [0.6.2] - 2023-09-08
### Added
- Added `withRequest` method which can used easily when using remote wiremock server.

## [0.6.1] - 2023-08-31
### Changed
- Update target jvmVersion 1.8 -> 11
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repositories {
}
dependencies {
testImplementation 'io.github.nilwurtz:wiremock-graphql-extension:0.6.1'
testImplementation 'io.github.nilwurtz:wiremock-graphql-extension:0.6.2'
}
```

Expand All @@ -40,7 +40,7 @@ dependencies {
<dependency>
<groupId>io.github.nilwurtz</groupId>
<artifactId>wiremock-graphql-extension</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -103,15 +103,15 @@ Please download `wiremock-graphql-extension-x.y.z-jar-with-dependencies.jar` fro
docker run -it --rm \
-p 8080:8080 \
--name wiremock \
-v /path/to/wiremock-graphql-extension-0.6.1-jar-with-dependencies.jar:/var/wiremock/extensions/wiremock-graphql-extension-0.6.1-jar-with-dependencies.jar \
-v /path/to/wiremock-graphql-extension-0.6.2-jar-with-dependencies.jar:/var/wiremock/extensions/wiremock-graphql-extension-0.6.2-jar-with-dependencies.jar \
wiremock/wiremock \
--extensions io.github.nilwurtz.GraphqlBodyMatcher
```

#### When building with `docker build`:
```dockerfile
FROM wiremock/wiremock:latest
COPY ./wiremock-graphql-extension-0.6.1-jar-with-dependencies.jar /var/wiremock/extensions/wiremock-graphql-extension-0.6.1-jar-with-dependencies.jar
COPY ./wiremock-graphql-extension-0.6.2-jar-with-dependencies.jar /var/wiremock/extensions/wiremock-graphql-extension-0.6.2-jar-with-dependencies.jar
CMD ["--extensions", "io.github.nilwurtz.GraphqlBodyMatcher"]
```

Expand All @@ -125,12 +125,14 @@ import com.github.tomakehurst.wiremock.client.WireMock.*
import io.github.nilwurtz.GraphqlBodyMatcher

fun registerGraphQLWiremock(json: String) {
WireMock(8080).register(post(urlPathEqualTo(endPoint))
.andMatching(GraphqlBodyMatcher.extensionName, Parameters.one("expectedJson", json))
.willReturn(
aResponse()
.withStatus(200)
))
WireMock(8080).register(
post(urlPathEqualTo(endPoint))
.andMatching(GraphqlBodyMatcher.extensionName, GraphqlBodyMatcher.withRequest(json))
.willReturn(
aResponse()
.withStatus(200)
)
)
}
```

Expand Down
3 changes: 3 additions & 0 deletions e2e/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ run:

docker: docker/build docker/run

docker/stop:
docker stop $$(docker ps -q --filter ancestor=wiremock-graphql-extension:latest)

docker/build:
cd ../wiremock-graphql-extension && docker build -t wiremock-graphql-extension:latest .

Expand Down
4 changes: 2 additions & 2 deletions e2e/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<kotlin.compiler.jvmTarget>${java.version}</kotlin.compiler.jvmTarget>
<gauge.java.version>0.10.2</gauge.java.version>
<gauge.maven.version>1.5.0</gauge.maven.version>
<wiremock.version>3.0.0</wiremock.version>
<wiremock.version>3.0.3</wiremock.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand All @@ -22,7 +22,7 @@
<dependency>
<artifactId>wiremock-graphql-extension</artifactId>
<groupId>io.github.nilwurtz</groupId>
<version>0.6.1</version>
<version>0.6.2</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.gauge</groupId>
Expand Down
5 changes: 2 additions & 3 deletions e2e/src/test/kotlin/Steps.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.github.tomakehurst.wiremock.client.WireMock.*
import com.github.tomakehurst.wiremock.extension.Parameters
import com.thoughtworks.gauge.Step
import io.github.nilwurtz.GraphqlBodyMatcher
import java.net.URI
Expand All @@ -12,11 +11,11 @@ class Steps {
fun setupGraphqlJsonStub(json: String) {
// for remote
Datastore.client()?.register(post(urlEqualTo("/graphql"))
.andMatching(GraphqlBodyMatcher.extensionName, Parameters.one("expectedJson", json)).willReturn(ok()))
.andMatching(GraphqlBodyMatcher.extensionName, GraphqlBodyMatcher.withRequest(json)).willReturn(ok()))
// for local
Datastore.localServer()
?.stubFor(post(urlEqualTo("/graphql"))
.andMatching(GraphqlBodyMatcher.extensionName, Parameters.one("expectedJson", json)).willReturn(ok()))
.andMatching(GraphqlBodyMatcher.extensionName, GraphqlBodyMatcher.withRequest(json)).willReturn(ok()))
}

@Step("Register a stub to return 200 upon receiving the query<query>")
Expand Down
4 changes: 2 additions & 2 deletions wiremock-graphql-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>wiremock-graphql-extension</artifactId>
<groupId>io.github.nilwurtz</groupId>
<version>0.6.1</version>
<version>0.6.2</version>
<packaging>jar</packaging>
<name>wiremock-graphql-extension</name>
<description>A WireMock extension for handling GraphQL requests, allowing for easy mocking of GraphQL APIs in
Expand Down Expand Up @@ -36,7 +36,7 @@
<gpg.skip>true</gpg.skip>
<kotlin.version>1.9.10</kotlin.version>
<junit.version>5.10.0</junit.version>
<wiremock.version>3.0.0</wiremock.version>
<wiremock.version>3.0.3</wiremock.version>
<graphql-java.version>21.0</graphql-java.version>
<json.version>20230618</json.version>
<mockk.version>1.13.7</mockk.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ class GraphqlBodyMatcher() : RequestMatcherExtension() {
initExpectedRequestJson(expectedJson)
}
}

/**
* Creates a Parameters instance containing the given raw JSON string expected in the GraphQL request.
*
* This method is used to set up JSON expected in remote requests. The expectedJson parameter should be a raw JSON string that encapsulates the expected query and optionally variables for the GraphQL request. This string is used to create a parameters object utilized internally in the GraphqlBodyMatcher.
*
* @param expectedJson A raw JSON string that contains the GraphQL query and optionally variables expected in the requests.
* @return A Parameters instance created based on the expected JSON string.
*/
fun withRequest(expectedJson: String): Parameters {
return Parameters.one(expectedJsonKey, expectedJson)
}
}

private lateinit var expectedRequestJson: JSONObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,23 @@ class GraphqlBodyMatcherTest {
assertTrue(actual.isExactMatch)
}
}

@Nested
@DisplayName("test `withRemoteRequestJson`")
inner class WithRemoteRequestJson {
@Test
@DisplayName("returns Parameters with expectedJsonKey")
fun testMatchedIdentical() {
// language=json
val json = """
{
"query": "{ hero { name friends { name }}}"
}
""".trimIndent()

val actual = GraphqlBodyMatcher.withRequest(json)
assertTrue(actual.containsKey("expectedJson"))
assertEquals(json, actual.getString("expectedJson"))
}
}
}

1 comment on commit e52279b

@nilwurtz
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ;)
PR #5 #6

Please sign in to comment.