Skip to content

Commit 3eec99a

Browse files
committed
added documentation and example for forward with response override
1 parent 1fb5ff2 commit 3eec99a

File tree

3 files changed

+191
-4
lines changed

3 files changed

+191
-4
lines changed

jekyll-www.mock-server.com/mock_server/_includes/forward_action_code_examples.html

+70-3
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@
635635
<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>
636636
</div>
637637
</div>
638-
<button id="button_forward_class_callback" class="accordion">class callback</button>
638+
<button id="button_forward_class_callback" class="accordion">class callback to override request & response</button>
639639
<div class="panel">
640640
<button class="accordion inner">Java</button>
641641
<div class="panel">
@@ -649,7 +649,7 @@
649649
)
650650
.forward(
651651
callback()
652-
.withCallbackClass("org.mockserver.examples.mockserver.CallbackActionExamples$TestExpectationForwardCallback")
652+
.withCallbackClass(TestExpectationForwardCallback.class)
653653
);
654654
}
655655

@@ -702,6 +702,12 @@
702702
}'</code></pre>
703703
</div>
704704
<p>To use a class callback MockServer must be able to <strong>load the class from the classpath</strong>.</p>
705+
<p>The callback class must:</p>
706+
<ul>
707+
<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>
708+
<li>have a zero argument constructor</li>
709+
<li>be available in the classpath of the MockServer</li>
710+
</ul>
705711

706712
<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>
707713

@@ -748,7 +754,7 @@
748754

749755
<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>
750756
</div>
751-
<button id="button_forward_method_or_closure_callback" class="accordion">method / closure callback</button>
757+
<button id="button_forward_method_or_closure_callback" class="accordion">method / closure callback to override request</button>
752758
<div class="panel">
753759
<button class="accordion inner">Java 7</button>
754760
<div class="panel">
@@ -793,4 +799,65 @@
793799
.withBody("a_callback_request")
794800
);</code></pre>
795801
</div>
802+
</div>
803+
<button id="button_forward_method_or_closure_callback_with_response" class="accordion">method / closure callback to override request & response</button>
804+
<div class="panel">
805+
<button class="accordion inner">Java 7</button>
806+
<div class="panel">
807+
<pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
808+
.when(
809+
request()
810+
.withPath("/some/path")
811+
)
812+
.forward(
813+
new ExpectationForwardCallback() {
814+
@Override
815+
public HttpRequest handle(HttpRequest httpRequest) throws Exception {
816+
return request()
817+
.withPath(httpRequest.getPath())
818+
.withMethod("POST")
819+
.withHeaders(
820+
header("x-callback", "test_callback_header"),
821+
header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
822+
header("Connection", "keep-alive")
823+
)
824+
.withBody("a_callback_request");
825+
}
826+
},
827+
new ExpectationForwardAndResponseCallback() {
828+
@Override
829+
public HttpResponse handle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
830+
return httpResponse
831+
.withHeader("x-response-test", "x-response-test")
832+
.removeHeader(CONTENT_LENGTH.toString())
833+
.withBody("some_overridden_response_body");
834+
}
835+
}
836+
);</code></pre>
837+
</div>
838+
<button class="accordion inner">Java 8+</button>
839+
<div class="panel">
840+
<pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
841+
.when(
842+
request()
843+
.withPath("/some/path")
844+
)
845+
.forward(
846+
httpRequest ->
847+
request()
848+
.withPath(httpRequest.getPath())
849+
.withMethod("POST")
850+
.withHeaders(
851+
header("x-callback", "test_callback_header"),
852+
header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
853+
header("Connection", "keep-alive")
854+
)
855+
.withBody("a_callback_request"),
856+
(httpRequest, httpResponse) ->
857+
httpResponse
858+
.withHeader("x-response-test", "x-response-test")
859+
.removeHeader(CONTENT_LENGTH.toString())
860+
.withBody("some_overridden_response_body")
861+
);</code></pre>
862+
</div>
796863
</div>

jekyll-www.mock-server.com/mock_server/_includes/response_action_code_examples.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@
773773
)
774774
.respond(
775775
callback()
776-
.withCallbackClass("org.mockserver.examples.mockserver.CallbackActionExamples$TestExpectationResponseCallback")
776+
.withCallbackClass(TestExpectationResponseCallback.class)
777777
);
778778
}
779779

@@ -829,6 +829,12 @@
829829
}'</code></pre>
830830
</div>
831831
<p>To use a class callback MockServer must be able to <strong>load the class from the classpath</strong>.</p>
832+
<p>The callback class must:</p>
833+
<ul>
834+
<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>
835+
<li>have a zero argument constructor</li>
836+
<li>be available in the classpath of the MockServer</li>
837+
</ul>
832838

833839
<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>
834840

mockserver-examples/src/main/java/org/mockserver/examples/mockserver/CallbackActionExamples.java

+114
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import org.mockserver.client.MockServerClient;
44
import org.mockserver.integration.ClientAndServer;
5+
import org.mockserver.mock.action.ExpectationForwardAndResponseCallback;
56
import org.mockserver.mock.action.ExpectationForwardCallback;
67
import org.mockserver.mock.action.ExpectationResponseCallback;
78
import org.mockserver.model.HttpRequest;
89
import org.mockserver.model.HttpResponse;
910
import org.mockserver.model.HttpStatusCode;
1011

12+
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
1113
import static java.nio.charset.StandardCharsets.UTF_8;
1214
import static org.mockserver.model.Header.header;
1315
import static org.mockserver.model.HttpClassCallback.callback;
@@ -45,6 +47,33 @@ public void forwardClassCallback() {
4547
);
4648
}
4749

50+
@SuppressWarnings("Convert2Lambda")
51+
public void responseObjectCallbackJava7() {
52+
new MockServerClient("localhost", 1080)
53+
.when(
54+
request()
55+
.withPath("/some/path")
56+
)
57+
.respond(
58+
new ExpectationResponseCallback() {
59+
@Override
60+
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
61+
if (httpRequest.getMethod().getValue().equals("POST")) {
62+
return response()
63+
.withStatusCode(ACCEPTED_202.code())
64+
.withHeaders(
65+
header("x-object-callback", "test_object_callback_header")
66+
)
67+
.withBody("an_object_callback_response");
68+
} else {
69+
return notFoundResponse();
70+
}
71+
}
72+
}
73+
);
74+
75+
}
76+
4877
public void responseObjectCallback() {
4978
new MockServerClient("localhost", 1080)
5079
.when(
@@ -97,6 +126,32 @@ public void createExpectationWithinObjectCallback() {
97126
);
98127
}
99128

129+
@SuppressWarnings("Convert2Lambda")
130+
public void forwardObjectCallbackJava7() {
131+
new MockServerClient("localhost", 1080)
132+
.when(
133+
request()
134+
.withPath("/some/path")
135+
)
136+
.forward(
137+
new ExpectationForwardCallback() {
138+
@Override
139+
public HttpRequest handle(HttpRequest httpRequest) throws Exception {
140+
return request()
141+
.withPath(httpRequest.getPath())
142+
.withMethod("POST")
143+
.withHeaders(
144+
header("x-callback", "test_callback_header"),
145+
header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
146+
header("Connection", "keep-alive")
147+
)
148+
.withBody("a_callback_request");
149+
}
150+
}
151+
);
152+
153+
}
154+
100155
public void forwardObjectCallback() {
101156
new MockServerClient("localhost", 1080)
102157
.when(
@@ -117,6 +172,65 @@ public void forwardObjectCallback() {
117172

118173
}
119174

175+
@SuppressWarnings("Convert2Lambda")
176+
public void forwardObjectCallbackWithResponseOverrideJava7() {
177+
new MockServerClient("localhost", 1080)
178+
.when(
179+
request()
180+
.withPath("/some/path")
181+
)
182+
.forward(
183+
new ExpectationForwardCallback() {
184+
@Override
185+
public HttpRequest handle(HttpRequest httpRequest) throws Exception {
186+
return request()
187+
.withPath(httpRequest.getPath())
188+
.withMethod("POST")
189+
.withHeaders(
190+
header("x-callback", "test_callback_header"),
191+
header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
192+
header("Connection", "keep-alive")
193+
)
194+
.withBody("a_callback_request");
195+
}
196+
},
197+
new ExpectationForwardAndResponseCallback() {
198+
@Override
199+
public HttpResponse handle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
200+
return httpResponse
201+
.withHeader("x-response-test", "x-response-test")
202+
.removeHeader(CONTENT_LENGTH.toString())
203+
.withBody("some_overridden_response_body");
204+
}
205+
}
206+
);
207+
}
208+
209+
public void forwardObjectCallbackWithResponseOverride() {
210+
new MockServerClient("localhost", 1080)
211+
.when(
212+
request()
213+
.withPath("/some/path")
214+
)
215+
.forward(
216+
httpRequest ->
217+
request()
218+
.withPath(httpRequest.getPath())
219+
.withMethod("POST")
220+
.withHeaders(
221+
header("x-callback", "test_callback_header"),
222+
header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
223+
header("Connection", "keep-alive")
224+
)
225+
.withBody("a_callback_request"),
226+
(httpRequest, httpResponse) ->
227+
httpResponse
228+
.withHeader("x-response-test", "x-response-test")
229+
.removeHeader(CONTENT_LENGTH.toString())
230+
.withBody("some_overridden_response_body")
231+
);
232+
}
233+
120234
public static class TestExpectationResponseCallback implements ExpectationResponseCallback {
121235

122236
@Override

0 commit comments

Comments
 (0)