BAEL-1024 Introduction to MockServer (#2275)
This commit is contained in:
parent
fb7d3fe8f4
commit
fe39bc6c3d
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mockserver</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<mock-sever-netty-version>3.10.8</mock-sever-netty-version>
|
||||
<apche-http-version>4.4.1</apche-http-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-netty</artifactId>
|
||||
<version>${mock-sever-netty-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-client-java</artifactId>
|
||||
<version>${mock-sever-netty-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${apche-http-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${apche-http-version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.mock.server;
|
||||
|
||||
import org.mockserver.mock.action.ExpectationCallback;
|
||||
import org.mockserver.model.HttpRequest;
|
||||
import org.mockserver.model.HttpResponse;
|
||||
|
||||
import static org.mockserver.model.HttpResponse.notFoundResponse;
|
||||
import static org.mockserver.model.HttpResponse.response;
|
||||
|
||||
|
||||
public class ExpectationCallbackHandler implements ExpectationCallback {
|
||||
|
||||
public HttpResponse handle(HttpRequest httpRequest) {
|
||||
if (httpRequest.getPath().getValue().endsWith("/callback")) {
|
||||
return httpResponse;
|
||||
} else {
|
||||
return notFoundResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpResponse httpResponse = response()
|
||||
.withStatusCode(200);
|
||||
}
|
|
@ -0,0 +1,178 @@
|
|||
package com.baeldung.mock.server;
|
||||
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockserver.client.server.MockServerClient;
|
||||
import org.mockserver.integration.ClientAndProxy;
|
||||
import org.mockserver.integration.ClientAndServer;
|
||||
import org.mockserver.model.Header;
|
||||
import org.mockserver.model.HttpForward;
|
||||
import org.mockserver.verify.VerificationTimes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.mockserver.integration.ClientAndProxy.startClientAndProxy;
|
||||
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
|
||||
import static org.mockserver.matchers.Times.exactly;
|
||||
import static org.mockserver.model.HttpClassCallback.callback;
|
||||
import static org.mockserver.model.HttpForward.forward;
|
||||
import static org.mockserver.model.HttpRequest.request;
|
||||
import static org.mockserver.model.HttpResponse.response;
|
||||
import static org.mockserver.model.StringBody.exact;
|
||||
|
||||
public class TestMockServer {
|
||||
|
||||
private ClientAndProxy proxy;
|
||||
private ClientAndServer mockServer;
|
||||
|
||||
@Before
|
||||
public void startProxy() {
|
||||
mockServer = startClientAndServer(1080);
|
||||
proxy = startClientAndProxy(1090);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPostRequestMockServer_thenServerReceived(){
|
||||
createExpectationForInvalidAuth();
|
||||
hitTheServerWithPostRequest();
|
||||
verifyPostRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostRequestForInvalidAuth_then401Received(){
|
||||
createExpectationForInvalidAuth();
|
||||
org.apache.http.HttpResponse response = hitTheServerWithPostRequest();
|
||||
assertEquals(401, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetRequest_ThenForward(){
|
||||
createExpectationForForward();
|
||||
hitTheServerWithGetRequest("index.html");
|
||||
verifyGetRequest();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallbackRequest_ThenCallbackMethodCalled(){
|
||||
createExpectationForCallBack();
|
||||
org.apache.http.HttpResponse response= hitTheServerWithGetRequest("/callback");
|
||||
assertEquals(200,response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
private void verifyPostRequest() {
|
||||
new MockServerClient("localhost", 1080).verify(
|
||||
request()
|
||||
.withMethod("POST")
|
||||
.withPath("/validate")
|
||||
.withBody(exact("{username: 'foo', password: 'bar'}")),
|
||||
VerificationTimes.exactly(1)
|
||||
);
|
||||
}
|
||||
private void verifyGetRequest() {
|
||||
new MockServerClient("localhost", 1080).verify(
|
||||
request()
|
||||
.withMethod("GET")
|
||||
.withPath("/index.html"),
|
||||
VerificationTimes.exactly(1)
|
||||
);
|
||||
}
|
||||
|
||||
private org.apache.http.HttpResponse hitTheServerWithPostRequest() {
|
||||
String url = "http://127.0.0.1:1080/validate";
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpPost post = new HttpPost(url);
|
||||
post.setHeader("Content-type", "application/json");
|
||||
org.apache.http.HttpResponse response=null;
|
||||
|
||||
try {
|
||||
StringEntity stringEntity = new StringEntity("{username: 'foo', password: 'bar'}");
|
||||
post.getRequestLine();
|
||||
post.setEntity(stringEntity);
|
||||
response=client.execute(post);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private org.apache.http.HttpResponse hitTheServerWithGetRequest(String page) {
|
||||
String url = "http://127.0.0.1:1080/"+page;
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
org.apache.http.HttpResponse response=null;
|
||||
HttpGet get = new HttpGet(url);
|
||||
try {
|
||||
response=client.execute(get);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private void createExpectationForInvalidAuth() {
|
||||
new MockServerClient("127.0.0.1", 1080)
|
||||
.when(
|
||||
request()
|
||||
.withMethod("POST")
|
||||
.withPath("/validate")
|
||||
.withHeader("\"Content-type\", \"application/json\"")
|
||||
.withBody(exact("{username: 'foo', password: 'bar'}")),
|
||||
exactly(1)
|
||||
)
|
||||
.respond(
|
||||
response()
|
||||
.withStatusCode(401)
|
||||
.withHeaders(
|
||||
new Header("Content-Type", "application/json; charset=utf-8"),
|
||||
new Header("Cache-Control", "public, max-age=86400")
|
||||
)
|
||||
.withBody("{ message: 'incorrect username and password combination' }")
|
||||
.withDelay(TimeUnit.SECONDS,1)
|
||||
);
|
||||
}
|
||||
|
||||
private void createExpectationForForward(){
|
||||
new MockServerClient("127.0.0.1", 1080)
|
||||
.when(
|
||||
request()
|
||||
.withMethod("GET")
|
||||
.withPath("/index.html"),
|
||||
exactly(1)
|
||||
)
|
||||
.forward(
|
||||
forward()
|
||||
.withHost("www.mock-server.com")
|
||||
.withPort(80)
|
||||
.withScheme(HttpForward.Scheme.HTTP)
|
||||
);
|
||||
}
|
||||
|
||||
private void createExpectationForCallBack(){
|
||||
mockServer
|
||||
.when(
|
||||
request()
|
||||
.withPath("/callback")
|
||||
)
|
||||
.callback(
|
||||
callback()
|
||||
.withCallbackClass("com.baeldung.mock.server.ExpectationCallbackHandler")
|
||||
);
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopProxy() {
|
||||
proxy.stop();
|
||||
mockServer.stop();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue