diff --git a/jekyll-www.mock-server.com/mock_server/_includes/forward_action_code_examples.html b/jekyll-www.mock-server.com/mock_server/_includes/forward_action_code_examples.html index 89d514320..5a43b1483 100644 --- a/jekyll-www.mock-server.com/mock_server/_includes/forward_action_code_examples.html +++ b/jekyll-www.mock-server.com/mock_server/_includes/forward_action_code_examples.html @@ -635,7 +635,7 @@
See REST API for full JSON specification
- +To use a class callback MockServer must be able to load the class from the classpath.
+The callback class must:
+If MockServer is started using the JUnit @Rule org.mockserver.junit.MockServerRule or using org.mockserver.integration.ClientAndServer or directly using the org.mockserver.mockserver.MockServerBuilder then any class present in the main or test classpaths will be visible to MockServer.
@@ -748,7 +754,7 @@java -Dfile.encoding=UTF-8 -cp mockserver-netty-5.8.0-jar-with-dependencies.jar:my-callback-dependency.jar org.mockserver.cli.Main -serverPort 1080
new MockServerClient("localhost", 1080)
+ .when(
+ request()
+ .withPath("/some/path")
+ )
+ .forward(
+ new ExpectationForwardCallback() {
+ @Override
+ public HttpRequest handle(HttpRequest httpRequest) throws Exception {
+ return request()
+ .withPath(httpRequest.getPath())
+ .withMethod("POST")
+ .withHeaders(
+ header("x-callback", "test_callback_header"),
+ header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
+ header("Connection", "keep-alive")
+ )
+ .withBody("a_callback_request");
+ }
+ },
+ new ExpectationForwardAndResponseCallback() {
+ @Override
+ public HttpResponse handle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
+ return httpResponse
+ .withHeader("x-response-test", "x-response-test")
+ .removeHeader(CONTENT_LENGTH.toString())
+ .withBody("some_overridden_response_body");
+ }
+ }
+ );
+ new MockServerClient("localhost", 1080)
+ .when(
+ request()
+ .withPath("/some/path")
+ )
+ .forward(
+ httpRequest ->
+ request()
+ .withPath(httpRequest.getPath())
+ .withMethod("POST")
+ .withHeaders(
+ header("x-callback", "test_callback_header"),
+ header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
+ header("Connection", "keep-alive")
+ )
+ .withBody("a_callback_request"),
+ (httpRequest, httpResponse) ->
+ httpResponse
+ .withHeader("x-response-test", "x-response-test")
+ .removeHeader(CONTENT_LENGTH.toString())
+ .withBody("some_overridden_response_body")
+ );
+ To use a class callback MockServer must be able to load the class from the classpath.
+The callback class must:
+If MockServer is started using the JUnit @Rule org.mockserver.junit.MockServerRule or using org.mockserver.integration.ClientAndServer or directly using the org.mockserver.mockserver.MockServerBuilder then any class present in the main or test classpaths will be visible to MockServer.
diff --git a/mockserver-examples/src/main/java/org/mockserver/examples/mockserver/CallbackActionExamples.java b/mockserver-examples/src/main/java/org/mockserver/examples/mockserver/CallbackActionExamples.java index eacc6f94c..a821d3bc4 100644 --- a/mockserver-examples/src/main/java/org/mockserver/examples/mockserver/CallbackActionExamples.java +++ b/mockserver-examples/src/main/java/org/mockserver/examples/mockserver/CallbackActionExamples.java @@ -2,12 +2,14 @@ import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; +import org.mockserver.mock.action.ExpectationForwardAndResponseCallback; import org.mockserver.mock.action.ExpectationForwardCallback; import org.mockserver.mock.action.ExpectationResponseCallback; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; import org.mockserver.model.HttpStatusCode; +import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; import static java.nio.charset.StandardCharsets.UTF_8; import static org.mockserver.model.Header.header; import static org.mockserver.model.HttpClassCallback.callback; @@ -45,6 +47,33 @@ public void forwardClassCallback() { ); } + @SuppressWarnings("Convert2Lambda") + public void responseObjectCallbackJava7() { + new MockServerClient("localhost", 1080) + .when( + request() + .withPath("/some/path") + ) + .respond( + new ExpectationResponseCallback() { + @Override + public HttpResponse handle(HttpRequest httpRequest) throws Exception { + if (httpRequest.getMethod().getValue().equals("POST")) { + return response() + .withStatusCode(ACCEPTED_202.code()) + .withHeaders( + header("x-object-callback", "test_object_callback_header") + ) + .withBody("an_object_callback_response"); + } else { + return notFoundResponse(); + } + } + } + ); + + } + public void responseObjectCallback() { new MockServerClient("localhost", 1080) .when( @@ -97,6 +126,32 @@ public void createExpectationWithinObjectCallback() { ); } + @SuppressWarnings("Convert2Lambda") + public void forwardObjectCallbackJava7() { + new MockServerClient("localhost", 1080) + .when( + request() + .withPath("/some/path") + ) + .forward( + new ExpectationForwardCallback() { + @Override + public HttpRequest handle(HttpRequest httpRequest) throws Exception { + return request() + .withPath(httpRequest.getPath()) + .withMethod("POST") + .withHeaders( + header("x-callback", "test_callback_header"), + header("Content-Length", "a_callback_request".getBytes(UTF_8).length), + header("Connection", "keep-alive") + ) + .withBody("a_callback_request"); + } + } + ); + + } + public void forwardObjectCallback() { new MockServerClient("localhost", 1080) .when( @@ -117,6 +172,65 @@ public void forwardObjectCallback() { } + @SuppressWarnings("Convert2Lambda") + public void forwardObjectCallbackWithResponseOverrideJava7() { + new MockServerClient("localhost", 1080) + .when( + request() + .withPath("/some/path") + ) + .forward( + new ExpectationForwardCallback() { + @Override + public HttpRequest handle(HttpRequest httpRequest) throws Exception { + return request() + .withPath(httpRequest.getPath()) + .withMethod("POST") + .withHeaders( + header("x-callback", "test_callback_header"), + header("Content-Length", "a_callback_request".getBytes(UTF_8).length), + header("Connection", "keep-alive") + ) + .withBody("a_callback_request"); + } + }, + new ExpectationForwardAndResponseCallback() { + @Override + public HttpResponse handle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception { + return httpResponse + .withHeader("x-response-test", "x-response-test") + .removeHeader(CONTENT_LENGTH.toString()) + .withBody("some_overridden_response_body"); + } + } + ); + } + + public void forwardObjectCallbackWithResponseOverride() { +new MockServerClient("localhost", 1080) + .when( + request() + .withPath("/some/path") + ) + .forward( + httpRequest -> + request() + .withPath(httpRequest.getPath()) + .withMethod("POST") + .withHeaders( + header("x-callback", "test_callback_header"), + header("Content-Length", "a_callback_request".getBytes(UTF_8).length), + header("Connection", "keep-alive") + ) + .withBody("a_callback_request"), + (httpRequest, httpResponse) -> + httpResponse + .withHeader("x-response-test", "x-response-test") + .removeHeader(CONTENT_LENGTH.toString()) + .withBody("some_overridden_response_body") + ); + } + public static class TestExpectationResponseCallback implements ExpectationResponseCallback { @Override