Closed6

Try wiremock-graphql-extension v0.6.2 - not working when the order of certain mappings

ぺこぺこ

What I did

  • register the following stubs
    • GraphQL request
    • not GraphQL request
      • request body is json
      • request body is not json
ぺこぺこ

Configuration to register stub

  • maven module
    • code
Demo.java
    @Test
    void x() {
        new WireMock(8080).loadMappingsFrom(Paths.get("fixtures").toFile());
    }
  • place stub files in fixtures/mappings
  • run test x()
ぺこぺこ

Stubs pattern 1

ready stubs

  • Stub files
    • GraphQL request
    • not GraphQL request
      • request body is json
foo-graphql.json - GraphQL request
{
  "request": {
    "method": "POST",
    "urlPath": "/foo/graphql",
    "customMatcher" : {
      "name" : "graphql-body-matcher",
      "parameters" : {
        "expectedJson" : "{ \"query\": \",query DemoQuery { demo { id name } }\" }"
      }
    }
  },

  "response": {
    "status": 200,
    "jsonBody": {
      "data": [
        {
          "id": 1,
          "name": "foo"
        },
        {
          "id": 2,
          "name": "bar"
        }
      ]
    }
  }
}
foo.json - not GraphQL request, request body is json
{
  "request": {
    "method": "POST",
    "urlPath": "/foo",
    "bodyPatterns" : [{
      "equalToJson" : {
        "name": "baz"
      }
    }]
  },

  "response": {
    "status": 201,
    "jsonBody": {
      "message": "Hello baz"
    }
  }
}

request

GraphQL request

foo-graphql-request.json
{
  "query": "query DemoQuery {
    demo {
      id
      name
    }
  }"
}
curl -i -X POST http://localhost:8080/foo/graphql -d @foo-graphql-request.json
output
HTTP/1.1 200 OK
Matched-Stub-Id: 51da9376-f4b7-4103-87e9-f3dbc77f2acf
Transfer-Encoding: chunked

{"data":[{"id":1,"name":"foo"},{"id":2,"name":"bar"}]}

not GraphQL request, request body is json

foo-request.json
{
  "name": "baz"
}
curl -i -X POST http://localhost:8080/foo -d @foo-request.json
output
HTTP/1.1 201 Created
Matched-Stub-Id: a21788a8-fec7-417f-acb7-11c1a428d856
Transfer-Encoding: chunked

{"message":"Hello baz"}
ぺこぺこ

Stubs pattern 2

ready stubs

  • Stub files
    • GraphQL request
    • not GraphQL request
      • request body is not json
foo-graphql.json - GraphQL request
{
  "request": {
    "method": "POST",
    "urlPath": "/foo/graphql",
    "customMatcher" : {
      "name" : "graphql-body-matcher",
      "parameters" : {
        "expectedJson" : "{ \"query\": \",query DemoQuery { demo { id name } }\" }"
      }
    }
  },

  "response": {
    "status": 200,
    "jsonBody": {
      "data": [
        {
          "id": 1,
          "name": "foo"
        },
        {
          "id": 2,
          "name": "bar"
        }
      ]
    }
  }
}
foo.json - not GraphQL request, request body is not json
{
  "request": {
    "method": "POST",
    "urlPath": "/foo",
    "formParameters": {
      "name": {
        "equalTo": "baz"
      }
    }
  },

  "response": {
    "status": 201,
    "jsonBody": {
      "message": "Hello baz"
    }
  }
}

request

GraphQL request

foo-graphql-request.json
{
  "query": "query DemoQuery {
    demo {
      id
      name
    }
  }"
}
curl -i -X POST http://localhost:8080/foo/graphql -d @foo-graphql-request.json
output
HTTP/1.1 200 OK
Matched-Stub-Id: 6d68f6e4-974d-41e2-b8e3-369ad47afb89
Transfer-Encoding: chunked

{"data":[{"id":1,"name":"foo"},{"id":2,"name":"bar"}]}

not GraphQL request, request body is not json

curl -i -X POST http://localhost:8080/foo -d 'name=baz'
output
HTTP/1.1 201 Created
Matched-Stub-Id: 745847bc-fc4a-43db-beb3-4b64f0d0ad48
Transfer-Encoding: chunked

{"message":"Hello baz"}
ぺこぺこ

Stubs pattern 3 - Error occured when the order of certain mappings and the request body is not JSON

condition

  • the order of certain mappings
    • 1st, mapping use customMatcher of extension
    • 2nt, mapping use not-extension (ex. formParameters)
  • request that body is not JSON

the order of certain mappings

curl -i -X GET http://localhost:8080/__admin/mappings
outut
HTTP/1.1 200 OK
Vary: Origin
Content-Type: application/json
Transfer-Encoding: chunked

{
  "mappings" : [ {
    "id" : "a73f4166-035d-468d-841f-0113d08f80ec",
    "request" : {
      "urlPath" : "/foo/graphql",
      "method" : "POST",
      "customMatcher" : {
        "name" : "graphql-body-matcher",
        "parameters" : {
          "expectedJson" : "{ \"query\": \"query DemoQuery { demo { id name } }\" }"
        }
      }
    },
    "response" : {
      "status" : 200,
      "jsonBody" : {
        "data" : [ {
          "id" : 1,
          "name" : "foo"
        }, {
          "id" : 2,
          "name" : "bar"
        } ]
      }
    },
    "uuid" : "a73f4166-035d-468d-841f-0113d08f80ec"
  }, {
    "id" : "5868bbc6-6544-49e0-97d1-6a4941397723",
    "request" : {
      "urlPath" : "/foo",
      "method" : "POST",
      "formParameters" : {
        "name" : {
          "equalTo" : "baz"
        }
      }
    },
    "response" : {
      "status" : 201,
      "jsonBody" : {
        "message" : "Hello baz"
      }
    },
    "uuid" : "5868bbc6-6544-49e0-97d1-6a4941397723"
  } ],
  "meta" : {
    "total" : 2
  }
}

request that body is not JSON, then error occured

curl -i -X POST http://localhost:8080/foo -d 'name=baz'
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: 5106
Connection: close

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 500 org.json.JSONException: A JSONObject text must begin with &apos;{&apos; at 1 [character 2 line 1]</title>
</head>
<body><h2>HTTP ERROR 500 org.json.JSONException: A JSONObject text must begin with &apos;{&apos; at 1 [character 2 line 1]</h2>
<table>
<tr><th>URI:</th><td>/foo</td></tr>
<tr><th>STATUS:</th><td>500</td></tr>
<tr><th>MESSAGE:</th><td>org.json.JSONException: A JSONObject text must begin with &apos;{&apos; at 1 [character 2 line 1]</td></tr>
<tr><th>SERVLET:</th><td>com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet-4032d386</td></tr>
<tr><th>CAUSED BY:</th><td>org.json.JSONException: A JSONObject text must begin with &apos;{&apos; at 1 [character 2 line 1]</td></tr>
</table>
<h3>Caused by:</h3><pre>org.json.JSONException: A JSONObject text must begin with &apos;{&apos; at 1 [character 2 line 1]
	at org.json.JSONTokener.syntaxError(JSONTokener.java:497)
	at org.json.JSONObject.&lt;init&gt;(JSONObject.java:208)
	at org.json.JSONObject.&lt;init&gt;(JSONObject.java:404)
	at io.github.nilwurtz.GraphqlBodyMatcher.match(GraphqlBodyMatcher.kt:118)
	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>

request that body is GraphQL, then success

foo-graphql-request.json
{
  "query": "query DemoQuery {
    demo {
      id
      name
    }
  }"
}
curl -i -X POST http://localhost:8080/foo/graphql -d @foo-graphql-request.json
output
HTTP/1.1 200 OK
Matched-Stub-Id: a73f4166-035d-468d-841f-0113d08f80ec
Transfer-Encoding: chunked

{"data":[{"id":1,"name":"foo"},{"id":2,"name":"bar"}]}
このスクラップは2023/09/23にクローズされました