|
| 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 | +} |
0 commit comments