-
Notifications
You must be signed in to change notification settings - Fork 9
/
CookieMatchingTest.java
168 lines (139 loc) · 6.27 KB
/
CookieMatchingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package net.petrikainulainen.wiremock.configuration;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.*;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This class demonstrates how we can use request matchers that
* compare the actual cookie value with the expected cookie value.
*/
@DisplayName("Compare the actual cookie value with the expected cookie value")
class CookieMatchingTest {
private RestTemplate restTemplate;
private WireMockServer wireMockServer;
@BeforeEach
void configureSystemUnderTest() {
this.restTemplate = new RestTemplate();
this.wireMockServer = new WireMockServer(options()
.dynamicPort()
);
this.wireMockServer.start();
configureFor("localhost", this.wireMockServer.port());
}
@Nested
@DisplayName("When we want to ensure that the actual value is equal to the expected value")
class WhenWeWantToEnsureThatActualValueIsEqualToExpectedValue {
@Test
@DisplayName("Should compare the actual value with the exact expected value")
void shouldCompareActualUrlWithExactExpectedUrl() {
givenThat(get(urlEqualTo("/api/message?id=1"))
.withCookie("name", equalTo("Petri Kainulainen"))
.willReturn(aResponse().withStatus(200))
);
String apiMethodUrl = buildApiMethodUrl(1L);
HttpEntity<String> httpRequest = createHttpRequest();
ResponseEntity<String> response = restTemplate.exchange(apiMethodUrl,
HttpMethod.GET,
httpRequest,
String.class
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
@Nested
@DisplayName("When we want to ignore case and ensure that the actual value is equal to the expected value")
class WhenWeWantToIgnoreCaseEnsureThatActualValueIsEqualToExpectedValue {
@Test
@DisplayName("Should compare the actual value with the exact expected value")
void shouldCompareActualUrlWithExactExpectedUrl() {
givenThat(get(urlEqualTo("/api/message?id=1"))
.withCookie("name", equalToIgnoreCase("PETRI KAINULAINEN"))
.willReturn(aResponse().withStatus(200))
);
String apiMethodUrl = buildApiMethodUrl(1L);
HttpEntity<String> httpRequest = createHttpRequest();
ResponseEntity<String> response = restTemplate.exchange(apiMethodUrl,
HttpMethod.GET,
httpRequest,
String.class
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
@Nested
@DisplayName("When we want to ensure that the actual value contains the expected value")
class WhenWeWantEnsureThatActualValueContainsExpectedValue {
@Test
@DisplayName("Should ensure that the actual value contains the expected value")
void shouldEnsureThatActualValueContainsExpectedValue() {
givenThat(get(urlEqualTo("/api/message?id=1"))
.withCookie("name", containing("nulai"))
.willReturn(aResponse().withStatus(200))
);
String apiMethodUrl = buildApiMethodUrl(1L);
HttpEntity<String> httpRequest = createHttpRequest();
ResponseEntity<String> response = restTemplate.exchange(apiMethodUrl,
HttpMethod.GET,
httpRequest,
String.class
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
@Nested
@DisplayName("When we want to ensure that the actual value matches with the given regex")
class WhenWeWantToEnsureThatActualValueMatchesWithRegex {
@Test
@DisplayName("Should ensure that the actual value matches with the given regex")
void shouldCompareActualUrlWithExactExpectedUrl() {
givenThat(get(urlEqualTo("/api/message?id=1"))
.withCookie("name", matching("Petri Kai([a-z]{2})lainen"))
.willReturn(aResponse().withStatus(200))
);
String apiMethodUrl = buildApiMethodUrl(1L);
HttpEntity<String> httpRequest = createHttpRequest();
ResponseEntity<String> response = restTemplate.exchange(apiMethodUrl,
HttpMethod.GET,
httpRequest,
String.class
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
@Nested
@DisplayName("When we want to ensure that the actual value doesn't match with the given regex")
class WhenWeWantToEnsureThatActualValueDoesNotMatchWithRegex {
@Test
@DisplayName("Should ensure that the actual value doesn't match with the given regex")
void shouldCompareActualUrlWithExactExpectedUrl() {
givenThat(get(urlEqualTo("/api/message?id=1"))
.withCookie("name", notMatching("Petri Kai([a-z]{1})lainen"))
.willReturn(aResponse().withStatus(200))
);
String apiMethodUrl = buildApiMethodUrl(1L);
HttpEntity<String> httpRequest = createHttpRequest();
ResponseEntity<String> response = restTemplate.exchange(apiMethodUrl,
HttpMethod.GET,
httpRequest,
String.class
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
private HttpEntity<String> createHttpRequest() {
HttpHeaders headers = new HttpHeaders();
headers.set("Cookie", "name=Petri Kainulainen");
return new HttpEntity<>(headers);
}
private String buildApiMethodUrl(Long messageId) {
return String.format("http://localhost:%d/api/message?id=%d", this.wireMockServer.port(), messageId);
}
@AfterEach
void stopWireMockServer() {
this.wireMockServer.stop();
}
}