-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
HttpWaitStrategyTest.java
285 lines (260 loc) · 10.2 KB
/
HttpWaitStrategyTest.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package org.testcontainers.junit.wait.strategy;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.rnorth.ducttape.RetryCountExceededException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import java.time.Duration;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
* Tests for {@link HttpWaitStrategy}.
*
* @author Pete Cornish {@literal <outofcoffee@gmail.com>}
*/
public class HttpWaitStrategyTest extends AbstractWaitStrategyTest<HttpWaitStrategy> {
/**
* newline sequence indicating end of the HTTP header.
*/
private static final String NEWLINE = "\r\n";
private static final String GOOD_RESPONSE_BODY = "Good Response Body";
/**
* Expects that the WaitStrategy returns successfully after receiving an HTTP 200 response from the container.
*/
@Test
public void testWaitUntilReadyWithSuccess() {
waitUntilReadyAndSucceed(createShellCommand("200 OK", GOOD_RESPONSE_BODY));
}
/**
* Ensures that HTTP requests made with the HttpWaitStrategy can be enriched with user defined headers,
* although the test web server does not depend on the header to response with a 200, by checking the
* logs we can ensure the HTTP request was correctly sent.
*/
@Test
public void testWaitUntilReadyWithSuccessWithCustomHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("baz", "boo");
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).withHeader("foo", "bar").withHeaders(headers)
)
) {
waitUntilReadyAndSucceed(container);
String logs = container.getLogs();
assertThat(logs, containsString("foo: bar"));
assertThat(logs, containsString("baz: boo"));
}
}
/**
* Ensures that HTTPS requests made with the HttpWaitStrategy can skip the
* certificate validation chains (to support self-signed certificates for example).
*/
@Test
public void testWaitUntilReadyWithTlsAndAllowUnsecure() {
try (
GenericContainer<?> container = startContainerWithCommand(
createHttpsShellCommand("200 OK", GOOD_RESPONSE_BODY, 8080),
createHttpWaitStrategy(ready).usingTls().allowInsecure()
)
) {
waitUntilReadyAndSucceed(container);
}
}
/**
* Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.
* This 401 response is checked with a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}
*/
@Test
public void testWaitUntilReadyWithUnauthorizedWithLambda() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 200 && it < 300 || it == 401)
)
) {
waitUntilReadyAndSucceed(container);
}
}
/**
* Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.
* This 401 response is checked with many status codes using {@link HttpWaitStrategy#forStatusCode(int)}
*/
@Test
public void testWaitUntilReadyWithManyStatusCodes() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCode(300).forStatusCode(401).forStatusCode(500)
)
) {
waitUntilReadyAndSucceed(container);
}
}
/**
* Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.
* This 401 response is checked with with many status codes using {@link HttpWaitStrategy#forStatusCode(int)}
* and a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}
*/
@Test
public void testWaitUntilReadyWithManyStatusCodesAndLambda() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready)
.forStatusCode(300)
.forStatusCode(500)
.forStatusCodeMatching(it -> it == 401)
)
) {
waitUntilReadyAndSucceed(container);
}
}
/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving any of the
* error code defined with {@link HttpWaitStrategy#forStatusCode(int)}
* and {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}
*/
@Test
public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCode(300).forStatusCodeMatching(it -> it == 500)
)
) {
waitUntilReadyAndTimeout(container);
}
}
/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving any of the
* error code defined with {@link HttpWaitStrategy#forStatusCode(int)}
* and {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}. Note that a 200 status code should not
* be considered as a successful return as not explicitly set.
* Test case for: https://github.com/testcontainers/testcontainers-java/issues/880
*/
@Test
public void testWaitUntilReadyWithTimeoutAndWithLambdaShouldNotMatchOk() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 300)
)
) {
waitUntilReadyAndTimeout(container);
}
}
/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving an HTTP 200
* response from the container within the timeout period.
*/
@Test
public void testWaitUntilReadyWithTimeout() {
waitUntilReadyAndTimeout(createShellCommand("400 Bad Request", GOOD_RESPONSE_BODY));
}
/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not the expected response body
* from the container within the timeout period.
*/
@Test
public void testWaitUntilReadyWithTimeoutAndBadResponseBody() {
waitUntilReadyAndTimeout(createShellCommand("200 OK", "Bad Response"));
}
/**
* Expects the WaitStrategy probing the right port.
*/
@Test
public void testWaitUntilReadyWithSpecificPort() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY, 9090),
createHttpWaitStrategy(ready).forPort(9090),
7070,
8080,
9090
)
) {
waitUntilReadyAndSucceed(container);
}
}
@Test
public void testWaitUntilReadyWithTimoutCausedByReadTimeout() {
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("0 Connection Refused", GOOD_RESPONSE_BODY, 9090),
createHttpWaitStrategy(ready).forPort(9090).withReadTimeout(Duration.ofMillis(1)),
9090
)
) {
waitUntilReadyAndTimeout(container);
}
}
/**
* @param ready the AtomicBoolean on which to indicate success
* @return the WaitStrategy under test
*/
@NotNull
protected HttpWaitStrategy buildWaitStrategy(final AtomicBoolean ready) {
return createHttpWaitStrategy(ready).forResponsePredicate(s -> s.equals(GOOD_RESPONSE_BODY));
}
/**
* Create a HttpWaitStrategy instance with a waitUntilReady implementation
*
* @param ready Indicates that the WaitStrategy has completed waiting successfully.
* @return the HttpWaitStrategy instance
*/
private HttpWaitStrategy createHttpWaitStrategy(final AtomicBoolean ready) {
return new HttpWaitStrategy() {
@Override
protected void waitUntilReady() {
// blocks until ready or timeout occurs
super.waitUntilReady();
ready.set(true);
}
};
}
private String createShellCommand(String header, String responseBody) {
return createShellCommand(header, responseBody, 8080);
}
private String createShellCommand(String header, String responseBody, int port) {
int length = responseBody.getBytes().length;
return (
"while true; do { echo -e \"HTTP/1.1 " +
header +
NEWLINE +
"Content-Type: text/html" +
NEWLINE +
"Content-Length: " +
length +
NEWLINE +
"\";" +
" echo \"" +
responseBody +
"\";} | nc -lp " +
port +
"; done"
);
}
private String createHttpsShellCommand(String header, String responseBody, int port) {
int length = responseBody.getBytes().length;
return (
"apk add nmap-ncat; while true; do { echo -e \"HTTP/1.1 " +
header +
NEWLINE +
"Content-Type: text/html" +
NEWLINE +
"Content-Length: " +
length +
NEWLINE +
"\";" +
" echo \"" +
responseBody +
"\";} | ncat -lp " +
port +
" --ssl; done"
);
}
}