Skip to content

Commit

Permalink
Merge pull request #1 from yyYank/main
Browse files Browse the repository at this point in the history
Added wiremock extension for remote servers
  • Loading branch information
nilwurtz committed May 24, 2023
2 parents 3e6ae79 + e963937 commit 4fe14e9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
18 changes: 17 additions & 1 deletion wiremock-graphql-extension/pom.xml
Expand Up @@ -32,7 +32,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<gpg.skip>true</gpg.skip>
<kotlin.version>1.8.20</kotlin.version>
<junit.version>5.9.2</junit.version>
Expand Down Expand Up @@ -191,6 +191,22 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Expand Up @@ -11,7 +11,7 @@ import org.json.JSONException
import org.json.JSONObject


class GraphqlBodyMatcher private constructor() : RequestMatcherExtension() {
class GraphqlBodyMatcher() : RequestMatcherExtension() {

companion object {
/**
Expand Down Expand Up @@ -99,12 +99,19 @@ class GraphqlBodyMatcher private constructor() : RequestMatcherExtension() {
throw InvalidQueryException("Invalid request query: ${e.message}")
}

val expectedQuery = try {
val expectedQuery = if (parameters.containsKey("expectedQuery")) {
expectedRequestJson = JSONObject(parameters.getString("expectedQuery"))
expectedRequestJson.getString("query")
.let { Parser().parseDocument(it) }
.let { GraphqlQueryNormalizer.normalizeGraphqlDocument(it) }
} catch (e: Exception) {
throw InvalidQueryException("Invalid expected query: ${e.message}")
} else {
try {
expectedRequestJson.getString("query")
.let { Parser().parseDocument(it) }
.let { GraphqlQueryNormalizer.normalizeGraphqlDocument(it) }
} catch (e: Exception) {
throw InvalidQueryException("Invalid expected query: ${e.message}")
}
}

// Extract and compare variables
Expand All @@ -126,4 +133,4 @@ class GraphqlBodyMatcher private constructor() : RequestMatcherExtension() {
return "graphql-body-matcher"
}

}
}
Expand Up @@ -3,6 +3,7 @@ package io.github.nilwurtz
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import com.github.tomakehurst.wiremock.http.Request
import com.github.tomakehurst.wiremock.extension.Parameters
import io.github.nilwurtz.exceptions.InvalidJsonException
import io.github.nilwurtz.exceptions.InvalidQueryException
import io.mockk.every
Expand All @@ -15,6 +16,7 @@ class GraphqlBodyMatcherTest {
@DisplayName("queries are identical")
fun testMatchedIdentical() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=json
val json = """
{
Expand All @@ -23,14 +25,16 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns json
val actual = GraphqlBodyMatcher.withRequestJson(json).match(request, mockk())
every { parameters.containsKey("expectedQuery") } returns false
val actual = GraphqlBodyMatcher.withRequestJson(json).match(request, parameters)
assertTrue(actual.isExactMatch)
}

@Test
@DisplayName("test `withRequest` when queries are identical")
fun testMatchedIdenticalWithRequest() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
val query = "{ hero { name friends { name }}}"
// language=json
val json = """
Expand All @@ -40,14 +44,16 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns json
val actual = GraphqlBodyMatcher.withRequestQueryAndVariables(query).match(request, mockk())
every { parameters.containsKey("expectedQuery") } returns false
val actual = GraphqlBodyMatcher.withRequestQueryAndVariables(query).match(request, parameters)
assertTrue(actual.isExactMatch)
}

@Test
@DisplayName("test `withRequest` when queries and variables are identical")
fun testMatchedIdenticalWithRequestAndVariables() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
val query = "query GetCharacters(\$ids: [ID!]) { characters(ids: \$ids) { name age } }"
val variables = """{"ids": [1, 2, 3]}"""
val escaped = "\$ids"
Expand All @@ -66,14 +72,16 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns json
val actual = GraphqlBodyMatcher.withRequestQueryAndVariables(query, variables).match(request, mockk())
every { parameters.containsKey("expectedQuery") } returns false
val actual = GraphqlBodyMatcher.withRequestQueryAndVariables(query, variables).match(request, parameters)
assertTrue(actual.isExactMatch)
}

@Test
@DisplayName("query has different order in single level")
fun testMatchedDifferentOrderSingleLevel() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=JSON
val requestJson = """
{
Expand All @@ -88,7 +96,8 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns requestJson
val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, mockk())
every { parameters.containsKey("expectedQuery") } returns false
val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, parameters)
assertTrue(actual.isExactMatch)
}

Expand All @@ -97,6 +106,7 @@ class GraphqlBodyMatcherTest {
@DisplayName("graphql query has different order so nested")
fun testMatchedDifferentOrderNested() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=JSON
val requestJson = """
{
Expand All @@ -111,15 +121,17 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns requestJson
every { parameters.containsKey("expectedQuery") } returns false

val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, mockk())
val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, parameters)
assertTrue(actual.isExactMatch)
}

@Test
@DisplayName("query has different depth")
fun testUnmatchedDifferentDepth() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=json
val requestJson = """
{
Expand All @@ -134,15 +146,17 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns requestJson
every { parameters.containsKey("expectedQuery") } returns false

val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, mockk())
val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, parameters)
assertFalse(actual.isExactMatch)
}

@Test
@DisplayName("query is missing a field")
fun testUnmatchedMissingField() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=json
val requestJson = """
{
Expand All @@ -157,15 +171,17 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns requestJson
every { parameters.containsKey("expectedQuery") } returns false

val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, mockk())
val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, parameters)
assertFalse(actual.isExactMatch)
}

@Test
@DisplayName("query has additional field")
fun testUnmatchedAdditionalField() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=json
val requestJson = """
{
Expand All @@ -180,15 +196,17 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns requestJson
every { parameters.containsKey("expectedQuery") } returns false

val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, mockk())
val actual = GraphqlBodyMatcher.withRequestJson(expectedJson).match(request, parameters)
assertFalse(actual.isExactMatch)
}

@Test
@DisplayName("query has different field name")
fun testUnmatchedDifferentFieldName() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=json
val json1 = """
{
Expand All @@ -203,15 +221,17 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns json1
every { parameters.containsKey("expectedQuery") } returns false

val actual = GraphqlBodyMatcher.withRequestJson(json2).match(request, mockk())
val actual = GraphqlBodyMatcher.withRequestJson(json2).match(request, parameters)
assertFalse(actual.isExactMatch)
}

@Test
@DisplayName("query is invalid JSON")
fun testUnmatchedInvalidJson() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
// language=json
val invalidQuery = """
{
Expand All @@ -220,19 +240,22 @@ class GraphqlBodyMatcherTest {
""".trimIndent()

every { request.bodyAsString } returns invalidQuery
every { parameters.containsKey("expectedQuery") } returns false

assertThrows<InvalidQueryException> {
GraphqlBodyMatcher.withRequestJson(invalidQuery).match(request, mockk())
GraphqlBodyMatcher.withRequestJson(invalidQuery).match(request, parameters)
}
}

@Test
@DisplayName("json is empty")
fun testUnmatchedEmptyJson() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
val emptyJson = ""

every { request.bodyAsString } returns emptyJson
every { parameters.containsKey("expectedQuery") } returns false

assertThrows<InvalidJsonException> {
GraphqlBodyMatcher.withRequestJson(emptyJson).match(request, mockk())
Expand All @@ -243,13 +266,15 @@ class GraphqlBodyMatcherTest {
@DisplayName("query is empty")
fun testUnmatchedEmptyQuery() {
val request = mockk<Request>()
val parameters = mockk<Parameters>()
val json = """{ "query": "" }"""

every { request.bodyAsString } returns json
every { parameters.containsKey("expectedQuery") } returns false

assertThrows<InvalidQueryException> {
GraphqlBodyMatcher.withRequestJson(json).match(request, mockk())
GraphqlBodyMatcher.withRequestJson(json).match(request, parameters)
}
}

}
}

0 comments on commit 4fe14e9

Please sign in to comment.