6
6
import java .io .File ;
7
7
import java .io .IOException ;
8
8
import java .net .HttpURLConnection ;
9
+ import java .net .MalformedURLException ;
9
10
import java .net .URL ;
10
11
import java .nio .charset .StandardCharsets ;
11
12
import java .time .Instant ;
@@ -157,6 +158,7 @@ public String getHttpResponse(String path, boolean allowError, Supplier<String>
157
158
public String getHttpResponse (String path , boolean allowError , Supplier <String > brokenReason , long timeout ,
158
159
TimeUnit tu ) {
159
160
AtomicReference <String > resp = new AtomicReference <>();
161
+
160
162
await ()
161
163
.pollDelay (1 , TimeUnit .SECONDS )
162
164
.atMost (timeout , tu ).until (() -> {
@@ -166,20 +168,25 @@ public String getHttpResponse(String path, boolean allowError, Supplier<String>
166
168
return true ;
167
169
}
168
170
try {
169
- URL url = new URL ("http://localhost:" + port + ((path .startsWith ("/" ) ? path : "/" + path )));
171
+ URL url = prepareUrl (path );
172
+
170
173
String content ;
171
174
if (!allowError ) {
172
175
content = IOUtils .toString (url , StandardCharsets .UTF_8 );
173
176
} else {
174
177
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
178
+ conn .setDefaultUseCaches (false );
179
+ conn .setUseCaches (false );
175
180
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.8
176
181
conn .setRequestProperty ("Accept" , "text/html, *; q=0.2, */*; q=0.2" );
177
182
if (conn .getResponseCode () >= 400 ) {
178
183
content = IOUtils .toString (conn .getErrorStream (), StandardCharsets .UTF_8 );
179
184
} else {
180
185
content = IOUtils .toString (conn .getInputStream (), StandardCharsets .UTF_8 );
181
186
}
187
+ conn .disconnect ();
182
188
}
189
+
183
190
resp .set (content );
184
191
return true ;
185
192
} catch (Exception e ) {
@@ -203,8 +210,10 @@ public boolean getHttpResponse(String path, int expectedStatus, long timeout, Ti
203
210
.pollDelay (1 , TimeUnit .SECONDS )
204
211
.atMost (timeout , tu ).until (() -> {
205
212
try {
206
- URL url = new URL ( "http://localhost:" + port + (( path . startsWith ( "/" ) ? path : "/" + path )) );
213
+ URL url = prepareUrl ( path );
207
214
HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
215
+ connection .setDefaultUseCaches (false );
216
+ connection .setUseCaches (false );
208
217
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2
209
218
connection .setRequestProperty ("Accept" , "text/html, *; q=0.2, */*; q=0.2" );
210
219
if (connection .getResponseCode () == expectedStatus ) {
@@ -230,8 +239,10 @@ public boolean getStrictHttpResponse(String path, int expectedStatus) {
230
239
.pollDelay (1 , TimeUnit .SECONDS )
231
240
.atMost (5 , TimeUnit .MINUTES ).until (() -> {
232
241
try {
233
- URL url = new URL ( "http://localhost:" + port + (( path . startsWith ( "/" ) ? path : "/" + path )) );
242
+ URL url = prepareUrl ( path );
234
243
HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
244
+ connection .setDefaultUseCaches (false );
245
+ connection .setUseCaches (false );
235
246
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2
236
247
connection .setRequestProperty ("Accept" , "text/html, *; q=0.2, */*; q=0.2" );
237
248
code .set (connection .getResponseCode () == expectedStatus );
@@ -258,8 +269,10 @@ public String get(String urlStr) throws IOException {
258
269
259
270
public boolean isCode (String path , int code ) {
260
271
try {
261
- URL url = new URL ( "http://localhost:" + port + (( path . startsWith ( "/" ) ? path : "/" + path )) );
272
+ URL url = prepareUrl ( path );
262
273
HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
274
+ connection .setDefaultUseCaches (false );
275
+ connection .setUseCaches (false );
263
276
// the default Accept header used by HttpURLConnection is not compatible with
264
277
// RESTEasy negotiation as it uses q=.2
265
278
connection .setRequestProperty ("Accept" , "text/html, *; q=0.2, */*; q=0.2" );
@@ -268,4 +281,16 @@ public boolean isCode(String path, int code) {
268
281
return false ;
269
282
}
270
283
}
284
+
285
+ private URL prepareUrl (String path ) throws MalformedURLException {
286
+ String urlString = "http://localhost:" + port + (path .startsWith ("/" ) ? path : "/" + path );
287
+ if (urlString .contains ("?" )) {
288
+ urlString += "&" ;
289
+ } else {
290
+ urlString += "?" ;
291
+ }
292
+ urlString += "_test_timestamp=" + System .currentTimeMillis ();
293
+
294
+ return new URL (urlString );
295
+ }
271
296
}
0 commit comments