Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not working when expectedJson contains newlines #11

Closed
ShigeoTejima opened this issue Sep 23, 2023 · 1 comment
Closed

not working when expectedJson contains newlines #11

ShigeoTejima opened this issue Sep 23, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@ShigeoTejima
Copy link

Summary

    @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)
                )
        )
    }
  • Registered Wiremock mappings are as follows
{
  "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
  }
}
  • The request body is as follows
{
  "query": "query DemoQuery {
    demo {
      id
      name
    }
  }"
}
  • The request command is
curl -i -X POST http://localhost:8080/foo/graphql -d @graphql-request.json
  • The following error response was returned
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.&lt;init&gt;(JSONObject.java:245)
	at org.json.JSONObject.&lt;init&gt;(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>
  • It worked correctly when I removed the line breaks as follows
        val expectedJson = """
            {
                "query": "$expectedQuery"
            }
        """.trimIndent().replace("\n", "")

References

No response

@ShigeoTejima ShigeoTejima added the documentation Improvements or additions to documentation label Sep 23, 2023
@nilwurtz
Copy link
Collaborator

Thank you. I will fix this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants