Skip to content

Commit db9ecdb

Browse files
committed
introduction to wiremock
1 parent e8bbe6e commit db9ecdb

1 file changed

Lines changed: 84 additions & 10 deletions

File tree

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.baeldung.rest.wiremock.introduction;
22

33
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4-
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
55
import static com.github.tomakehurst.wiremock.client.WireMock.get;
66
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
7+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
8+
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
79
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
810
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
11+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
912
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
13+
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
14+
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
1015
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
1116
import static org.junit.Assert.assertEquals;
1217

@@ -16,6 +21,8 @@
1621

1722
import org.apache.http.HttpResponse;
1823
import org.apache.http.client.methods.HttpGet;
24+
import org.apache.http.client.methods.HttpPost;
25+
import org.apache.http.entity.StringEntity;
1926
import org.apache.http.impl.client.CloseableHttpClient;
2027
import org.apache.http.impl.client.HttpClients;
2128
import org.junit.Rule;
@@ -28,26 +35,93 @@ public class JUnitManaged {
2835
public WireMockRule wireMockRule = new WireMockRule();
2936

3037
@Test
31-
public void givenJUnitManagedServer_whenStubbingURL_thenCorrect() throws IOException {
32-
configureFor("localhost", 8080);
38+
public void givenJUnitManagedServer_whenMatchingURL_thenCorrect() throws IOException {
3339
stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("\"testing-library\": \"WireMock\"")));
3440

3541
CloseableHttpClient httpClient = HttpClients.createDefault();
3642
HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock");
3743
HttpResponse httpResponse = httpClient.execute(request);
38-
String stringResponse = convertResponseToString(httpResponse);
44+
String stringResponse = convertHttpResponseToString(httpResponse);
3945

4046
verify(getRequestedFor(urlEqualTo("/baeldung/wiremock")));
4147
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
4248
assertEquals("application/json", httpResponse.getFirstHeader("Content-Type").getValue());
4349
assertEquals("\"testing-library\": \"WireMock\"", stringResponse);
4450
}
4551

46-
private String convertResponseToString(HttpResponse response) throws IOException {
47-
InputStream responseStream = response.getEntity().getContent();
48-
Scanner scanner = new Scanner(responseStream, "UTF-8");
49-
String stringResponse = scanner.useDelimiter("\\Z").next();
52+
@Test
53+
public void givenJUnitManagedServer_whenMatchingHeaders_thenCorrect() throws IOException {
54+
stubFor(get(urlPathEqualTo("/baeldung/wiremock")).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503).withBody("!!! Service Unavailable !!!")));
55+
56+
CloseableHttpClient httpClient = HttpClients.createDefault();
57+
HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock");
58+
request.addHeader("Accept", "text/xml");
59+
HttpResponse httpResponse = httpClient.execute(request);
60+
String stringResponse = convertHttpResponseToString(httpResponse);
61+
62+
verify(getRequestedFor(urlEqualTo("/baeldung/wiremock")));
63+
assertEquals(503, httpResponse.getStatusLine().getStatusCode());
64+
assertEquals("!!! Service Unavailable !!!", stringResponse);
65+
}
66+
67+
// @Test
68+
public void givenJUnitManagedServer_whenMatchingBody_thenCorrect() throws IOException {
69+
stubFor(post(urlEqualTo("/baeldung/wiremock")).withHeader("Content-Type", equalTo("application/json")).withRequestBody(containing("\"testing-library\": \"WireMock\"")).withRequestBody(containing("\"creator\": \"Tom Akehurst\""))
70+
.withRequestBody(containing("\"website\": \"wiremock.org\"")).willReturn(aResponse().withStatus(200)));
71+
72+
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("wiremock_intro.json");
73+
String jsonString = convertInputStreamToString(jsonInputStream);
74+
StringEntity entity = new StringEntity(jsonString);
75+
76+
CloseableHttpClient httpClient = HttpClients.createDefault();
77+
HttpPost request = new HttpPost("http://localhost:8080/baeldung/wiremock");
78+
79+
request.addHeader("Content-Type", "application/json");
80+
request.setEntity(entity);
81+
HttpResponse response = httpClient.execute(request);
82+
83+
assertEquals(200, response.getStatusLine().getStatusCode());
84+
verify(postRequestedFor(urlEqualTo("/baeldung/wiremock")).withHeader("Content-Type", equalTo("application/json")));
85+
}
86+
87+
@Test
88+
public void givenJUnitManagedServer_whenNotUsingPriority_thenCorrect() throws IOException {
89+
stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200)));
90+
stubFor(get(urlPathEqualTo("/baeldung/wiremock")).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503)));
91+
92+
HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests();
93+
94+
verify(getRequestedFor(urlEqualTo("/baeldung/wiremock")));
95+
assertEquals(503, httpResponse.getStatusLine().getStatusCode());
96+
}
97+
98+
@Test
99+
public void givenJUnitManagedServer_whenUsingPriority_thenCorrect() throws IOException {
100+
stubFor(get(urlPathMatching("/baeldung/.*")).atPriority(1).willReturn(aResponse().withStatus(200)));
101+
stubFor(get(urlPathEqualTo("/baeldung/wiremock")).atPriority(2).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503)));
102+
103+
HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests();
104+
105+
verify(getRequestedFor(urlEqualTo("/baeldung/wiremock")));
106+
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
107+
}
108+
109+
private String convertHttpResponseToString(HttpResponse httpResponse) throws IOException {
110+
InputStream inputStream = httpResponse.getEntity().getContent();
111+
return convertInputStreamToString(inputStream);
112+
}
113+
114+
private String convertInputStreamToString(InputStream inputStream) {
115+
Scanner scanner = new Scanner(inputStream, "UTF-8");
116+
String string = scanner.useDelimiter("\\Z").next();
50117
scanner.close();
51-
return stringResponse;
118+
return string;
119+
}
120+
121+
private HttpResponse generateClientAndReceiveResponseForPriorityTests() throws IOException {
122+
CloseableHttpClient httpClient = HttpClients.createDefault();
123+
HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock");
124+
request.addHeader("Accept", "text/xml");
125+
return httpClient.execute(request);
52126
}
53-
}
127+
}

0 commit comments

Comments
 (0)