Skip to content

Commit

Permalink
added documentation and example for forward with response override
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdbloom committed Dec 23, 2019
1 parent 1fb5ff2 commit 3eec99a
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@
<p>See <a href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/5.8.x#/expectation/put_expectation" target="_blank">REST API</a> for full JSON specification</p>
</div>
</div>
<button id="button_forward_class_callback" class="accordion">class callback</button>
<button id="button_forward_class_callback" class="accordion">class callback to override request & response</button>
<div class="panel">
<button class="accordion inner">Java</button>
<div class="panel">
Expand All @@ -649,7 +649,7 @@
)
.forward(
callback()
.withCallbackClass("org.mockserver.examples.mockserver.CallbackActionExamples$TestExpectationForwardCallback")
.withCallbackClass(TestExpectationForwardCallback.class)
);
}

Expand Down Expand Up @@ -702,6 +702,12 @@
}'</code></pre>
</div>
<p>To use a class callback MockServer must be able to <strong>load the class from the classpath</strong>.</p>
<p>The callback class must:</p>
<ul>
<li>implement <ul><li><strong><span class="this_value">org.mockserver.mock.action.ExpectationForwardCallback</span></strong> to dynamically override the <strong>request</strong> or</li><li><strong><span class="this_value">org.mockserver.mock.action.ExpectationForwardAndResponseCallback</span></strong> to dynamically override the <strong>request</strong> and the <strong>response</strong></li></ul></li>
<li>have a zero argument constructor</li>
<li>be available in the classpath of the MockServer</li>
</ul>

<p>If MockServer is started using the <a href="/mock_server/running_mock_server.html#junit_rule">JUnit @Rule</a> <strong>org.mockserver.junit.MockServerRule</strong> or using <strong>org.mockserver.integration.ClientAndServer</strong> or directly using the <strong>org.mockserver.mockserver.MockServerBuilder</strong> then any class present in the main or test classpaths will be visible to MockServer.</p>

Expand Down Expand Up @@ -748,7 +754,7 @@

<pre><code class="code">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</code></pre>
</div>
<button id="button_forward_method_or_closure_callback" class="accordion">method / closure callback</button>
<button id="button_forward_method_or_closure_callback" class="accordion">method / closure callback to override request</button>
<div class="panel">
<button class="accordion inner">Java 7</button>
<div class="panel">
Expand Down Expand Up @@ -793,4 +799,65 @@
.withBody("a_callback_request")
);</code></pre>
</div>
</div>
<button id="button_forward_method_or_closure_callback_with_response" class="accordion">method / closure callback to override request & response</button>
<div class="panel">
<button class="accordion inner">Java 7</button>
<div class="panel">
<pre class="prettyprint lang-java code"><code class="code">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");
}
}
);</code></pre>
</div>
<button class="accordion inner">Java 8+</button>
<div class="panel">
<pre class="prettyprint lang-java code"><code class="code">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")
);</code></pre>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@
)
.respond(
callback()
.withCallbackClass("org.mockserver.examples.mockserver.CallbackActionExamples$TestExpectationResponseCallback")
.withCallbackClass(TestExpectationResponseCallback.class)
);
}

Expand Down Expand Up @@ -829,6 +829,12 @@
}'</code></pre>
</div>
<p>To use a class callback MockServer must be able to <strong>load the class from the classpath</strong>.</p>
<p>The callback class must:</p>
<ul>
<li>implement <ul><li><strong><span class="this_value">org.mockserver.mock.action.ExpectationResponseCallback</span></strong> to dynamically override the <strong>response</strong></li></ul></li>
<li>have a zero argument constructor</li>
<li>be available in the classpath of the MockServer</li>
</ul>

<p>If MockServer is started using the <a href="/mock_server/running_mock_server.html#junit_rule">JUnit @Rule</a> <strong>org.mockserver.junit.MockServerRule</strong> or using <strong>org.mockserver.integration.ClientAndServer</strong> or directly using the <strong>org.mockserver.mockserver.MockServerBuilder</strong> then any class present in the main or test classpaths will be visible to MockServer.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 3eec99a

Please sign in to comment.