Skip to content

Commit

Permalink
Merge pull request #39638 from gsmet/devmojoit
Browse files Browse the repository at this point in the history
Avoid all caching in DevModeClient
  • Loading branch information
gsmet authored Mar 23, 2024
2 parents 8443338 + 3ff8fb7 commit 4f59db4
Showing 1 changed file with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
Expand Down Expand Up @@ -157,6 +158,7 @@ public String getHttpResponse(String path, boolean allowError, Supplier<String>
public String getHttpResponse(String path, boolean allowError, Supplier<String> brokenReason, long timeout,
TimeUnit tu) {
AtomicReference<String> resp = new AtomicReference<>();

await()
.pollDelay(1, TimeUnit.SECONDS)
.atMost(timeout, tu).until(() -> {
Expand All @@ -166,20 +168,25 @@ public String getHttpResponse(String path, boolean allowError, Supplier<String>
return true;
}
try {
URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path)));
URL url = prepareUrl(path);

String content;
if (!allowError) {
content = IOUtils.toString(url, StandardCharsets.UTF_8);
} else {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDefaultUseCaches(false);
conn.setUseCaches(false);
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.8
conn.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2");
if (conn.getResponseCode() >= 400) {
content = IOUtils.toString(conn.getErrorStream(), StandardCharsets.UTF_8);
} else {
content = IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8);
}
conn.disconnect();
}

resp.set(content);
return true;
} catch (Exception e) {
Expand All @@ -203,8 +210,10 @@ public boolean getHttpResponse(String path, int expectedStatus, long timeout, Ti
.pollDelay(1, TimeUnit.SECONDS)
.atMost(timeout, tu).until(() -> {
try {
URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path)));
URL url = prepareUrl(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDefaultUseCaches(false);
connection.setUseCaches(false);
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2
connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2");
if (connection.getResponseCode() == expectedStatus) {
Expand All @@ -230,8 +239,10 @@ public boolean getStrictHttpResponse(String path, int expectedStatus) {
.pollDelay(1, TimeUnit.SECONDS)
.atMost(5, TimeUnit.MINUTES).until(() -> {
try {
URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path)));
URL url = prepareUrl(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDefaultUseCaches(false);
connection.setUseCaches(false);
// the default Accept header used by HttpURLConnection is not compatible with RESTEasy negotiation as it uses q=.2
connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2");
code.set(connection.getResponseCode() == expectedStatus);
Expand All @@ -258,8 +269,10 @@ public String get(String urlStr) throws IOException {

public boolean isCode(String path, int code) {
try {
URL url = new URL("http://localhost:" + port + ((path.startsWith("/") ? path : "/" + path)));
URL url = prepareUrl(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDefaultUseCaches(false);
connection.setUseCaches(false);
// the default Accept header used by HttpURLConnection is not compatible with
// RESTEasy negotiation as it uses q=.2
connection.setRequestProperty("Accept", "text/html, *; q=0.2, */*; q=0.2");
Expand All @@ -268,4 +281,16 @@ public boolean isCode(String path, int code) {
return false;
}
}

private URL prepareUrl(String path) throws MalformedURLException {
String urlString = "http://localhost:" + port + (path.startsWith("/") ? path : "/" + path);
if (urlString.contains("?")) {
urlString += "&";
} else {
urlString += "?";
}
urlString += "_test_timestamp=" + System.currentTimeMillis();

return new URL(urlString);
}
}

0 comments on commit 4f59db4

Please sign in to comment.