Skip to content

Commit fe39bc6

Browse files
baljeet20maibin
authored andcommitted
BAEL-1024 Introduction to MockServer (eugenp#2275)
1 parent fb7d3fe commit fe39bc6

4 files changed

Lines changed: 243 additions & 0 deletions

File tree

‎mockserver/pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>mockserver</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<properties>
11+
<mock-sever-netty-version>3.10.8</mock-sever-netty-version>
12+
<apche-http-version>4.4.1</apche-http-version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.mock-server</groupId>
18+
<artifactId>mockserver-netty</artifactId>
19+
<version>${mock-sever-netty-version}</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.mock-server</groupId>
24+
<artifactId>mockserver-client-java</artifactId>
25+
<version>${mock-sever-netty-version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.apache.httpcomponents</groupId>
30+
<artifactId>httpclient</artifactId>
31+
<version>${apche-http-version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.httpcomponents</groupId>
35+
<artifactId>httpcore</artifactId>
36+
<version>${apche-http-version}</version>
37+
</dependency>
38+
39+
</dependencies>
40+
41+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mock.server;
2+
3+
import org.mockserver.mock.action.ExpectationCallback;
4+
import org.mockserver.model.HttpRequest;
5+
import org.mockserver.model.HttpResponse;
6+
7+
import static org.mockserver.model.HttpResponse.notFoundResponse;
8+
import static org.mockserver.model.HttpResponse.response;
9+
10+
11+
public class ExpectationCallbackHandler implements ExpectationCallback {
12+
13+
public HttpResponse handle(HttpRequest httpRequest) {
14+
if (httpRequest.getPath().getValue().endsWith("/callback")) {
15+
return httpResponse;
16+
} else {
17+
return notFoundResponse();
18+
}
19+
}
20+
21+
public static HttpResponse httpResponse = response()
22+
.withStatusCode(200);
23+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.baeldung.mock.server;
2+
3+
import org.apache.http.client.HttpClient;
4+
import org.apache.http.client.methods.HttpGet;
5+
import org.apache.http.client.methods.HttpPost;
6+
import org.apache.http.entity.StringEntity;
7+
import org.apache.http.impl.client.HttpClientBuilder;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.mockserver.client.server.MockServerClient;
12+
import org.mockserver.integration.ClientAndProxy;
13+
import org.mockserver.integration.ClientAndServer;
14+
import org.mockserver.model.Header;
15+
import org.mockserver.model.HttpForward;
16+
import org.mockserver.verify.VerificationTimes;
17+
18+
import java.io.IOException;
19+
import java.util.concurrent.TimeUnit;
20+
21+
import static junit.framework.Assert.assertEquals;
22+
import static org.mockserver.integration.ClientAndProxy.startClientAndProxy;
23+
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
24+
import static org.mockserver.matchers.Times.exactly;
25+
import static org.mockserver.model.HttpClassCallback.callback;
26+
import static org.mockserver.model.HttpForward.forward;
27+
import static org.mockserver.model.HttpRequest.request;
28+
import static org.mockserver.model.HttpResponse.response;
29+
import static org.mockserver.model.StringBody.exact;
30+
31+
public class TestMockServer {
32+
33+
private ClientAndProxy proxy;
34+
private ClientAndServer mockServer;
35+
36+
@Before
37+
public void startProxy() {
38+
mockServer = startClientAndServer(1080);
39+
proxy = startClientAndProxy(1090);
40+
}
41+
42+
43+
@Test
44+
public void whenPostRequestMockServer_thenServerReceived(){
45+
createExpectationForInvalidAuth();
46+
hitTheServerWithPostRequest();
47+
verifyPostRequest();
48+
}
49+
50+
@Test
51+
public void whenPostRequestForInvalidAuth_then401Received(){
52+
createExpectationForInvalidAuth();
53+
org.apache.http.HttpResponse response = hitTheServerWithPostRequest();
54+
assertEquals(401, response.getStatusLine().getStatusCode());
55+
}
56+
57+
@Test
58+
public void whenGetRequest_ThenForward(){
59+
createExpectationForForward();
60+
hitTheServerWithGetRequest("index.html");
61+
verifyGetRequest();
62+
63+
}
64+
65+
@Test
66+
public void whenCallbackRequest_ThenCallbackMethodCalled(){
67+
createExpectationForCallBack();
68+
org.apache.http.HttpResponse response= hitTheServerWithGetRequest("/callback");
69+
assertEquals(200,response.getStatusLine().getStatusCode());
70+
}
71+
72+
private void verifyPostRequest() {
73+
new MockServerClient("localhost", 1080).verify(
74+
request()
75+
.withMethod("POST")
76+
.withPath("/validate")
77+
.withBody(exact("{username: 'foo', password: 'bar'}")),
78+
VerificationTimes.exactly(1)
79+
);
80+
}
81+
private void verifyGetRequest() {
82+
new MockServerClient("localhost", 1080).verify(
83+
request()
84+
.withMethod("GET")
85+
.withPath("/index.html"),
86+
VerificationTimes.exactly(1)
87+
);
88+
}
89+
90+
private org.apache.http.HttpResponse hitTheServerWithPostRequest() {
91+
String url = "http://127.0.0.1:1080/validate";
92+
HttpClient client = HttpClientBuilder.create().build();
93+
HttpPost post = new HttpPost(url);
94+
post.setHeader("Content-type", "application/json");
95+
org.apache.http.HttpResponse response=null;
96+
97+
try {
98+
StringEntity stringEntity = new StringEntity("{username: 'foo', password: 'bar'}");
99+
post.getRequestLine();
100+
post.setEntity(stringEntity);
101+
response=client.execute(post);
102+
103+
} catch (Exception e) {
104+
throw new RuntimeException(e);
105+
}
106+
return response;
107+
}
108+
109+
private org.apache.http.HttpResponse hitTheServerWithGetRequest(String page) {
110+
String url = "http://127.0.0.1:1080/"+page;
111+
HttpClient client = HttpClientBuilder.create().build();
112+
org.apache.http.HttpResponse response=null;
113+
HttpGet get = new HttpGet(url);
114+
try {
115+
response=client.execute(get);
116+
} catch (IOException e) {
117+
throw new RuntimeException(e);
118+
}
119+
120+
return response;
121+
}
122+
123+
private void createExpectationForInvalidAuth() {
124+
new MockServerClient("127.0.0.1", 1080)
125+
.when(
126+
request()
127+
.withMethod("POST")
128+
.withPath("/validate")
129+
.withHeader("\"Content-type\", \"application/json\"")
130+
.withBody(exact("{username: 'foo', password: 'bar'}")),
131+
exactly(1)
132+
)
133+
.respond(
134+
response()
135+
.withStatusCode(401)
136+
.withHeaders(
137+
new Header("Content-Type", "application/json; charset=utf-8"),
138+
new Header("Cache-Control", "public, max-age=86400")
139+
)
140+
.withBody("{ message: 'incorrect username and password combination' }")
141+
.withDelay(TimeUnit.SECONDS,1)
142+
);
143+
}
144+
145+
private void createExpectationForForward(){
146+
new MockServerClient("127.0.0.1", 1080)
147+
.when(
148+
request()
149+
.withMethod("GET")
150+
.withPath("/index.html"),
151+
exactly(1)
152+
)
153+
.forward(
154+
forward()
155+
.withHost("www.mock-server.com")
156+
.withPort(80)
157+
.withScheme(HttpForward.Scheme.HTTP)
158+
);
159+
}
160+
161+
private void createExpectationForCallBack(){
162+
mockServer
163+
.when(
164+
request()
165+
.withPath("/callback")
166+
)
167+
.callback(
168+
callback()
169+
.withCallbackClass("com.baeldung.mock.server.ExpectationCallbackHandler")
170+
);
171+
}
172+
173+
@After
174+
public void stopProxy() {
175+
proxy.stop();
176+
mockServer.stop();
177+
}
178+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
<module>drools</module>
231231
<module>liquibase</module>
232232
<module>spring-boot-property-exp</module>
233+
<module>mockserver</module>
233234
</modules>
234235

235236
<dependencies>

0 commit comments

Comments
 (0)