Improved basic SimpleHttpClientTest and add one for redirects
This commit is contained in:
parent
ee3024c1b7
commit
8094d72314
|
@ -90,9 +90,13 @@ public class SimpleHTTPClient {
|
||||||
setHeaders(c);
|
setHeaders(c);
|
||||||
c.setInstanceFollowRedirects(false);
|
c.setInstanceFollowRedirects(false);
|
||||||
|
|
||||||
switch (c.getResponseCode()) {
|
int responseCode = c.getResponseCode();
|
||||||
|
System.out.println("Processing response code "+ responseCode);
|
||||||
|
switch (responseCode) {
|
||||||
case HttpURLConnection.HTTP_MOVED_PERM:
|
case HttpURLConnection.HTTP_MOVED_PERM:
|
||||||
case HttpURLConnection.HTTP_MOVED_TEMP:
|
case HttpURLConnection.HTTP_MOVED_TEMP:
|
||||||
|
case 308: // Same as HTTP_MOVED_PERM, but does not allow changing the request method from POST to GET
|
||||||
|
System.out.println("Processing redirect to "+c.getHeaderField("Location"));
|
||||||
String location = c.getHeaderField("Location");
|
String location = c.getHeaderField("Location");
|
||||||
location = URLDecoder.decode(location, "UTF-8");
|
location = URLDecoder.decode(location, "UTF-8");
|
||||||
URL base = new URL(url);
|
URL base = new URL(url);
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue