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

withBody() and withBodyFile() are ignored #59

Open
kalinows opened this issue May 29, 2020 · 0 comments
Open

withBody() and withBodyFile() are ignored #59

kalinows opened this issue May 29, 2020 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@kalinows
Copy link

kalinows commented May 29, 2020

Describe the bug
Version: knotx-junit5:1.5.0
When a mock server is created with the @KnotxWiremock annotation, the mapping methods withBody() and withBodyFile() of ResponseDefinitionBuilder are ignored and the mock tries to load a response from a file instead.

To Reproduce
Steps to reproduce the behavior:

  1. Create a mock server, using @KnotxWiremock, eg.
    @KnotxWiremock(port = 4000) protected WireMockServer mockServer;
  2. Create a mapping, using withBody() or withBodyFile(), eg.
    stubForServer(mockServer, get("/get-data.json") .willReturn(aResponse() .withStatus(HttpStatus.SC_OK) .withHeader("Content-Type", "application/json;charset=UTF-8") .withBody("custom response")));
  3. Create a test that calls the defined endpoint, eg. with RestAssured:
    given() .when() .header("Content-Type", "application/json;charset=UTF-8") .then() .body(equalTo("custom response"));
  4. The test fails because a file "get-data.json" is missing.
java.lang.AssertionError: 1 expectation failed.
Response body doesn't match expectation.
Expected: "custom response"
  Actual: <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 500 Server Error</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /get-data.json. Reason:
<pre>    Server Error</pre></p><h3>Caused by:</h3><pre>java.lang.IllegalArgumentException: resource get-data.json not found.
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:216)
	at com.google.common.io.Resources.getResource(Resources.java:195)
	at io.knotx.junit5.util.FileReader.readText(FileReader.java:33)
	at io.knotx.junit5.util.FileReader.readTextSafe(FileReader.java:45)
	at io.knotx.junit5.wiremock.KnotxFileSource.transform(KnotxFileSource.java:78)
	at com.github.tomakehurst.wiremock.http.StubResponseRenderer.applyTransformations(StubResponseRenderer.java:73)
	at com.github.tomakehurst.wiremock.http.StubResponseRenderer.render(StubResponseRenderer.java:52)
	at com.github.tomakehurst.wiremock.http.AbstractRequestHandler.handle(AbstractRequestHandler.java:47)
	at com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet.service(WireMockHandlerDispatchingServlet.java:108)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.servlets.gzip.GzipHandler.handle(GzipHandler.java:479)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:499)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.lang.Thread.run(Thread.java:748)
</pre>
<hr><i><small>Powered by Jetty://</small></i><hr/>

</body>
</html>

Expected behavior
The test should pass, regardless of existence of a file "get-data.json", as the mapping has defined body response.

As a workaround, the mock server can be created without @KnotxWiremock and then the test is going to pass.
protected WireMockServer mockServer; //... mockServer = new WireMockServer(4000); mockServer.start();

Screenshots

Additional context
A running test with the workaround:

//    @KnotxWiremock(port = 4000)
    protected WireMockServer mockServer;

    @Test
    @KnotxApplyConfiguration("some-config.conf")
    public void testForBug(Vertx vertx, VertxTestContext testContext) {

        mockServer = new WireMockServer(4000);
        mockServer.start();

        stubForServer(mockServer, get("/get-data.json")
                .willReturn(aResponse()
                        .withStatus(HttpStatus.SC_OK)
                        .withHeader("Content-Type", "application/json;charset=UTF-8")
                        .withBody("custom response")));

        given()
                .when()
                .header("Content-Type", "application/json;charset=UTF-8")
                .get("http://localhost:4000/get-data.json")
                .then()
                .body(equalTo("custom response"));

        testContext.completeNow();
    }
@tMaxx tMaxx added bug Something isn't working help wanted Extra attention is needed labels Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants