Improved basic SimpleHttpClientTest and add one for redirects

This commit is contained in:
dotasek 2024-08-22 15:42:18 -04:00
parent ee3024c1b7
commit 8094d72314
2 changed files with 100 additions and 17 deletions

View File

@ -90,17 +90,21 @@ public class SimpleHTTPClient {
setHeaders(c); setHeaders(c);
c.setInstanceFollowRedirects(false); c.setInstanceFollowRedirects(false);
switch (c.getResponseCode()) { int responseCode = c.getResponseCode();
case HttpURLConnection.HTTP_MOVED_PERM: System.out.println("Processing response code "+ responseCode);
case HttpURLConnection.HTTP_MOVED_TEMP: switch (responseCode) {
String location = c.getHeaderField("Location"); case HttpURLConnection.HTTP_MOVED_PERM:
location = URLDecoder.decode(location, "UTF-8"); case HttpURLConnection.HTTP_MOVED_TEMP:
URL base = new URL(url); case 308: // Same as HTTP_MOVED_PERM, but does not allow changing the request method from POST to GET
URL next = new URL(base, location); // Deal with relative URLs System.out.println("Processing redirect to "+c.getHeaderField("Location"));
url = next.toExternalForm(); String location = c.getHeaderField("Location");
continue; location = URLDecoder.decode(location, "UTF-8");
default: URL base = new URL(url);
done = true; URL next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
default:
done = true;
} }
} }

View File

@ -1,24 +1,103 @@
package org.hl7.fhir.utilities; package org.hl7.fhir.utilities;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.util.stream.Stream;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.hl7.fhir.utilities.http.HTTPResult; import org.hl7.fhir.utilities.http.HTTPResult;
import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.http.SimpleHTTPClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
public class SimpleHTTPClientTest { public class SimpleHTTPClientTest {
private MockWebServer server;
@BeforeEach
void setup() {
setupMockServer();
}
void setupMockServer() {
server = new MockWebServer();
}
@Test @Test
public void testSimpleHTTPClient() throws IOException { public void testGetApplicationJson() throws IOException, InterruptedException {
HttpUrl serverUrl = server.url("fhir/us/core/package-list.json?nocache=1724353440974");
server.enqueue(
new MockResponse()
.setBody("Monkeys").setResponseCode(200)
);
SimpleHTTPClient http = new SimpleHTTPClient(); SimpleHTTPClient http = new SimpleHTTPClient();
String url = "https://hl7.org/fhir/us/core/package-list.json?nocache=" + System.currentTimeMillis();
HTTPResult res = http.get(url, "application/json"); HTTPResult res = http.get(serverUrl.url().toString(), "application/json");
assertThat(res.getCode()).isEqualTo(200);
RecordedRequest packageRequest = server.takeRequest();
assert packageRequest.getRequestUrl() != null;
assertThat(packageRequest.getRequestUrl().toString()).isEqualTo(serverUrl.url().toString());
assertThat(packageRequest.getMethod()).isEqualTo("GET");
assertThat(packageRequest.getHeader("Accept")).isEqualTo("application/json");
}
public static Stream<Arguments> getRedirectArgs() {
return Stream.of(
Arguments.of(301, new String[]{"url1", "url2"}),
Arguments.of(301, new String[]{"url1", "url2", "url3"}),
Arguments.of(301, new String[]{"url1", "url2", "url3", "url4"}),
Arguments.of(302, new String[]{"url1", "url2"}),
Arguments.of(302, new String[]{"url1", "url2", "url3"}),
Arguments.of(302, new String[]{"url1", "url2", "url3", "url4"}),
Arguments.of(308, new String[]{"url1", "url2"}),
Arguments.of(308, new String[]{"url1", "url2", "url3"}),
Arguments.of(308, new String[]{"url1", "url2", "url3", "url4"})
);
}
@ParameterizedTest
@MethodSource("getRedirectArgs")
public void testRedirects(int code, String[] urlArgs) throws IOException, InterruptedException {
HttpUrl[] url = new HttpUrl[urlArgs.length];
for (int i = 0; i < urlArgs.length; i++) {
url[i] = server.url(urlArgs[i]);
if (i > 0 && i < urlArgs.length - 1) {
server.enqueue(
new MockResponse()
.setResponseCode(code)
.addHeader("Location", url[i].url().toString()));
} else if (i == urlArgs.length - 1) {
server.enqueue(
new MockResponse()
.setBody("Monkeys").setResponseCode(200)
);
}
}
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(url[0].url().toString(), "application/json");
assertThat(res.getCode()).isEqualTo(200);
// System.out.println(res.getCode());
// System.out.println(new String(res.getContent(), StandardCharsets.UTF_8));
assertTrue(res.getCode() != 400);
} }
} }