diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/ManagedWebAccessAuthTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/ManagedWebAccessAuthTests.java deleted file mode 100644 index 8198a614c..000000000 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/utils/client/ManagedWebAccessAuthTests.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.hl7.fhir.r5.utils.client; - -import org.hl7.fhir.utilities.http.ManagedWebAccess; -import org.junit.BeforeClass; - -public class ManagedWebAccessAuthTests { - @BeforeClass - public static void setUp() { - ManagedWebAccess.setUserAgent("hapi-fhir-testing-client"); - } -} diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/utilities/http/ManagedWebAccessAuthTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/utilities/http/ManagedWebAccessAuthTests.java new file mode 100644 index 000000000..89eed159e --- /dev/null +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/utilities/http/ManagedWebAccessAuthTests.java @@ -0,0 +1,164 @@ +package org.hl7.fhir.utilities.http; + +import okhttp3.HttpUrl; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.apache.commons.net.util.Base64; +import org.hl7.fhir.utilities.settings.ServerDetailsPOJO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ManagedWebAccessAuthTests { + + + public static final String DUMMY_AGENT = "dummyAgent"; + public static final String DUMMY_USERNAME = "dummy1"; + public static final String DUMMY_PASSWORD = "pass1"; + + public static final String DUMMY_TOKEN = "dummyToken"; + private static final String DUMMY_API_KEY = "dummyApiKey"; + private MockWebServer server; + + @BeforeEach + void setup() { + setupMockServer(); + } + + void setupMockServer() { + server = new MockWebServer(); + } + + @Test + public void testBaseCase() throws IOException, InterruptedException { + HttpUrl serverUrl = server.url("blah/blah/blah?arg=blah"); + + server.enqueue( + new MockResponse() + .setBody("Dummy Response").setResponseCode(200) + ); + + ManagedFhirWebAccessBuilder builder = new ManagedFhirWebAccessBuilder("dummyAgent", null); + HTTPResult result = builder.httpCall(new HTTPRequest().withUrl(serverUrl.toString()).withMethod(HTTPRequest.HttpMethod.GET)); + + assertThat(result.getCode()).isEqualTo(200); + assertThat(result.getContentAsString()).isEqualTo("Dummy Response"); + + RecordedRequest packageRequest = server.takeRequest(); + + assert packageRequest.getRequestUrl() != null; + assertExpectedHeaders(packageRequest, serverUrl.url().toString(), "GET"); + + } + + + @Test + public void testBasicAuthCase() throws IOException, InterruptedException { + + + ManagedFhirWebAccessBuilder builder = new ManagedFhirWebAccessBuilder("dummyAgent", null).withBasicAuth("dummy1", "pass1"); + + testBasicServerAuth(builder); + } + + private void testBasicServerAuth(ManagedFhirWebAccessBuilder builder) throws IOException, InterruptedException { + HttpUrl serverUrl = server.url("blah/blah/blah?arg=blah"); + + server.enqueue( + new MockResponse() + .setBody("Dummy Response").setResponseCode(200) + ); + HTTPResult result = builder.httpCall(new HTTPRequest().withUrl(serverUrl.toString()).withMethod(HTTPRequest.HttpMethod.GET)); + + assertThat(result.getCode()).isEqualTo(200); + assertThat(result.getContentAsString()).isEqualTo("Dummy Response"); + + RecordedRequest packageRequest = server.takeRequest(); + + assert packageRequest.getRequestUrl() != null; + assertExpectedHeaders(packageRequest, serverUrl.url().toString(), "GET"); + + byte[] b = Base64.encodeBase64((DUMMY_USERNAME + ":" + DUMMY_PASSWORD).getBytes(StandardCharsets.US_ASCII)); + String b64 = new String(b, StandardCharsets.US_ASCII); + + assertThat(packageRequest.getHeader("Authorization")).isEqualTo("Basic " + b64); + } + + @Test + public void testTokenAuthCase() throws IOException, InterruptedException { + HttpUrl serverUrl = server.url("blah/blah/blah?arg=blah"); + + server.enqueue( + new MockResponse() + .setBody("Dummy Response").setResponseCode(200) + ); + + ManagedFhirWebAccessBuilder builder = new ManagedFhirWebAccessBuilder("dummyAgent", null).withToken(DUMMY_TOKEN); + HTTPResult result = builder.httpCall(new HTTPRequest().withUrl(serverUrl.toString()).withMethod(HTTPRequest.HttpMethod.GET)); + + assertThat(result.getCode()).isEqualTo(200); + assertThat(result.getContentAsString()).isEqualTo("Dummy Response"); + + RecordedRequest packageRequest = server.takeRequest(); + + assert packageRequest.getRequestUrl() != null; + assertExpectedHeaders(packageRequest, serverUrl.url().toString(), "GET"); + + assertThat(packageRequest.getHeader("Authorization")).isEqualTo("Bearer " + DUMMY_TOKEN); + } + + private static void assertExpectedHeaders(RecordedRequest packageRequest, String expectedUrl, String expectedHttpMethod) { + assertThat(packageRequest.getRequestUrl().toString()).isEqualTo(expectedUrl); + assertThat(packageRequest.getMethod()).isEqualTo(expectedHttpMethod); + assertThat(packageRequest.getHeader("User-Agent")).isEqualTo(DUMMY_AGENT); + } + + @Test + public void testApiKeyAuthCase() throws IOException, InterruptedException { + HttpUrl serverUrl = server.url("blah/blah/blah?arg=blah"); + + server.enqueue( + new MockResponse() + .setBody("Dummy Response").setResponseCode(200) + ); + + ManagedFhirWebAccessBuilder builder = new ManagedFhirWebAccessBuilder("dummyAgent", null).withApiKey(DUMMY_API_KEY); + HTTPResult result = builder.httpCall(new HTTPRequest().withUrl(serverUrl.toString()).withMethod(HTTPRequest.HttpMethod.GET)); + + assertThat(result.getCode()).isEqualTo(200); + assertThat(result.getContentAsString()).isEqualTo("Dummy Response"); + + RecordedRequest packageRequest = server.takeRequest(); + + assert packageRequest.getRequestUrl() != null; + assertExpectedHeaders(packageRequest, serverUrl.url().toString(), "GET"); + + assertThat(packageRequest.getHeader("Api-Key")).isEqualTo(DUMMY_API_KEY); + } + + @Test + public void testServerAuthFromSettings() throws IOException, InterruptedException { + ManagedFhirWebAccessBuilder builder = new ManagedFhirWebAccessBuilder( + "dummyAgent", + List.of(getBasicAuthServerPojo())); + + testBasicServerAuth(builder); + } + + private ServerDetailsPOJO getBasicAuthServerPojo() { + ServerDetailsPOJO pojo = new ServerDetailsPOJO( + server.url("").toString(), + "basic", + "dummyServerType", + DUMMY_USERNAME, + DUMMY_PASSWORD, + null, null); + return pojo; + } +} diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedFhirWebAccessBuilder.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedFhirWebAccessBuilder.java index 5b3ff01d0..25a59750b 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedFhirWebAccessBuilder.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedFhirWebAccessBuilder.java @@ -46,6 +46,8 @@ public class ManagedFhirWebAccessBuilder extends ManagedWebAccessBuilderBase serverAuthDetails) { super(userAgent, serverAuthDetails); + this.timeout = 5000; + this.timeoutUnit = TimeUnit.MILLISECONDS; } protected HTTPRequest httpRequestWithDefaultHeaders(HTTPRequest request) { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccessBuilder.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccessBuilder.java index fdb15e7fc..d06193718 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccessBuilder.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccessBuilder.java @@ -29,6 +29,8 @@ public class ManagedWebAccessBuilder extends ManagedWebAccessBuilderBase