Closed7
Try wiremock-graphql-extension v0.6.2 - not working when expectedJson contains newlines
Reference
What I did
- Try
Running with a Remote Wiremock Server
https://github.com/wiremock/wiremock-graphql-extension
Start Wiremock Server
-
download
wiremock-graphql-extension-0.6.2-jar-with-dependencies.jar
https://github.com/wiremock/wiremock-graphql-extension/releases/tag/v0.6.2 -
run docker
- add args
--verbose
to check the behavior
- add args
docker run -it --rm \
-p 8080:8080 \
--name wiremock \
-v ./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 --verbose
Configuration to register stub
- maven module
- add dependency to
pom.xml
- add dependency to
pom.xml
<dependency>
<groupId>io.github.nilwurtz</groupId>
<artifactId>wiremock-graphql-extension</artifactId>
<version>0.6.2</version>
<scope>test</scope>
</dependency>
- code
Demo.kt
@Test
fun x() {
val expectedQuery = """
query DemoQuery {
demo {
id
name
}
}
""".trimIndent()
val expectedJson = """
{
"query": "$expectedQuery"
}
""".trimIndent()
val endpoint = "/foo/graphql"
WireMock(8080).register(
post(urlPathEqualTo(endpoint))
.andMatching(GraphqlBodyMatcher.extensionName, GraphqlBodyMatcher.withRequest(expectedJson))
.willReturn(
aResponse()
.withStatus(200)
)
)
}
- run test
x()
- confirm wiremock mappings
curl http://localhost:8080/__admin/mappings
output
{
"mappings" : [ {
"id" : "f5a13500-565e-4e68-8f5b-f0097b09c6ca",
"request" : {
"urlPath" : "/foo/graphql",
"method" : "POST",
"customMatcher" : {
"name" : "graphql-body-matcher",
"parameters" : {
"expectedJson" : " {\n \"query\": \"query DemoQuery {\n demo {\n id\n name\n }\n}\"\n }"
}
}
},
"response" : {
"status" : 200
},
"uuid" : "f5a13500-565e-4e68-8f5b-f0097b09c6ca"
} ],
"meta" : {
"total" : 1
}
}
org.json.JSONException
) occured when request GraphQL
Error (- generate request json file
graphql-request.json
{
"query": "query DemoQuery {
demo {
id
name
}
}"
}
- request
curl -i -X POST http://localhost:8080/foo/graphql -d @graphql-request.json
output
HTTP/1.1 500 Server Error
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 5094
Connection: close
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 500 org.json.JSONException: Unterminated string at 58 [character 0 line 3]</title>
</head>
<body><h2>HTTP ERROR 500 org.json.JSONException: Unterminated string at 58 [character 0 line 3]</h2>
<table>
<tr><th>URI:</th><td>/foo/graphql</td></tr>
<tr><th>STATUS:</th><td>500</td></tr>
<tr><th>MESSAGE:</th><td>org.json.JSONException: Unterminated string at 58 [character 0 line 3]</td></tr>
<tr><th>SERVLET:</th><td>com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet-5c669da8</td></tr>
<tr><th>CAUSED BY:</th><td>org.json.JSONException: Unterminated string at 58 [character 0 line 3]</td></tr>
</table>
<h3>Caused by:</h3><pre>org.json.JSONException: Unterminated string at 58 [character 0 line 3]
at org.json.JSONTokener.syntaxError(JSONTokener.java:497)
at org.json.JSONTokener.nextString(JSONTokener.java:302)
at org.json.JSONTokener.nextValue(JSONTokener.java:410)
at org.json.JSONObject.<init>(JSONObject.java:245)
at org.json.JSONObject.<init>(JSONObject.java:404)
at io.github.nilwurtz.GraphqlBodyMatcher.match(GraphqlBodyMatcher.kt:116)
at com.github.tomakehurst.wiremock.matching.RequestPattern.match(RequestPattern.java:242)
at com.github.tomakehurst.wiremock.store.StubMappingStore.lambda$findAllMatchingRequest$0(StubMappingStore.java:42)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
at java.base/java.util.concurrent.ConcurrentSkipListMap$KeySpliterator.tryAdvance(Unknown Source)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.base/java.util.stream.ReferencePipeline.findFirst(Unknown Source)
at com.github.tomakehurst.wiremock.stubbing.AbstractStubMappings.serveFor(AbstractStubMappings.java:83)
at com.github.tomakehurst.wiremock.core.WireMockApp.serveStubFor(WireMockApp.java:247)
at com.github.tomakehurst.wiremock.http.StubRequestHandler.handleRequest(StubRequestHandler.java:72)
at com.github.tomakehurst.wiremock.http.AbstractRequestHandler.handle(AbstractRequestHandler.java:72)
at com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet.service(WireMockHandlerDispatchingServlet.java:157)
at wiremock.jakarta.servlet.http.HttpServlet.service(HttpServlet.java:587)
at wiremock.org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:764)
at wiremock.org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1665)
at wiremock.org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:527)
at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:221)
at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1381)
at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:176)
at wiremock.org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:484)
at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:174)
at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1303)
at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:129)
at wiremock.org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:122)
at wiremock.org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:822)
at wiremock.org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:141)
at wiremock.org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:122)
at wiremock.org.eclipse.jetty.server.Server.handle(Server.java:563)
at wiremock.org.eclipse.jetty.server.HttpChannel$RequestDispatchable.dispatch(HttpChannel.java:1598)
at wiremock.org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:753)
at wiremock.org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:501)
at wiremock.org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:282)
at wiremock.org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:314)
at wiremock.org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:100)
at wiremock.org.eclipse.jetty.io.SelectableChannelEndPoint$1.run(SelectableChannelEndPoint.java:53)
at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:969)
at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.doRunJob(QueuedThreadPool.java:1194)
at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1149)
at java.base/java.lang.Thread.run(Unknown Source)
</pre>
</body>
</html>
Remove newlines from expectedJson
- change code
Demo.kt
val expectedJson = """
{
"query": "$expectedQuery"
}
""".trimIndent().replace("\n", "")
- reset wiremock mappings
curl -i -X POST http://localhost:8080/__admin/mappings/reset
output
HTTP/1.1 200 OK
Vary: Origin
Transfer-Encoding: chunked
- confirm wiremock mappings
curl http://localhost:8080/__admin/mappings
output
{
"mappings" : [ ],
"meta" : {
"total" : 0
}
}
- run test
x()
- confirm wiremock mappings
curl http://localhost:8080/__admin/mappings
output
{
"mappings" : [ {
"id" : "fc1d05fd-d3fa-4d2f-9713-923f6d5d34f2",
"request" : {
"urlPath" : "/foo/graphql",
"method" : "POST",
"customMatcher" : {
"name" : "graphql-body-matcher",
"parameters" : {
"expectedJson" : " { \"query\": \"query DemoQuery { demo { id name }}\" }"
}
}
},
"response" : {
"status" : 200
},
"uuid" : "fc1d05fd-d3fa-4d2f-9713-923f6d5d34f2"
} ],
"meta" : {
"total" : 1
}
}
Request GraphQL (2)
- request
curl -i -X POST http://localhost:8080/foo/graphql -d @graphql-request.json
output
HTTP/1.1 200 OK
Matched-Stub-Id: fc1d05fd-d3fa-4d2f-9713-923f6d5d34f2
Transfer-Encoding: chunked
- wiremock log
wiremock log
172.17.0.1 - POST /foo/graphql
Host: [localhost:8080]
User-Agent: [curl/8.1.2]
Accept: [*/*]
Content-Length: [68]
Content-Type: [application/x-www-form-urlencoded]
{ "query": "query DemoQuery { demo { id name } }"}
Matched response definition:
{
"status" : 200
}
Response:
HTTP/1.1 200
Matched-Stub-Id: [fc1d05fd-d3fa-4d2f-9713-923f6d5d34f2]
このスクラップは2023/09/21にクローズされました