Skip to content

Commit

Permalink
Check for Empty Flare Base Url and Catch Invalid URI Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EmteZogaf committed Nov 23, 2023
1 parent 01176de commit 8ac6fa6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.net.ssl.SSLContext;

import static ca.uhn.fhir.rest.api.Constants.HEADER_AUTHORIZATION;
import static ca.uhn.fhir.rest.api.Constants.HEADER_AUTHORIZATION_VALPREFIX_BEARER;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;

@Configuration
@Import(BaseConfig.class)
Expand Down Expand Up @@ -67,7 +70,13 @@ public class FlareWebserviceClientSpringConfig {

@Bean
public FlareWebserviceClient flareWebserviceClient(HttpClient httpClient) {
return new FlareWebserviceClientImpl(httpClient, URI.create(flareBaseUrl));
checkArgument(!isNullOrEmpty(flareBaseUrl), "FLARE_BASE_URL is not set.");
try {
URI parsedFlareBaseUrl = new URI(flareBaseUrl);
return new FlareWebserviceClientImpl(httpClient, parsedFlareBaseUrl);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(format("Could not parse FLARE_BASE_URL '%s' as URI.", flareBaseUrl), e);
}
}

@Bean
Expand Down Expand Up @@ -97,32 +106,32 @@ public HttpClient flareHttpClient(@Qualifier("base-client") SSLContext sslContex

private final class BearerHttpClient extends CloseableHttpClient {
private CloseableHttpClient client;

public BearerHttpClient(CloseableHttpClient client) {
this.client = client;
}

@Override
public HttpParams getParams() {
return client.getParams();
}

@Override
public ClientConnectionManager getConnectionManager() {
return client.getConnectionManager();
}

@Override
public void close() throws IOException {
client.close();
}

@Override
protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context)
throws IOException, ClientProtocolException {
return client.execute(target, request, context);
}

@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
throws IOException, ClientProtocolException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void testRequestFeasibility() throws IOException, InterruptedException {
}

@Test
public void testName() throws Exception {
public void testBaseUrlPathIsKept() throws Exception {
String path = "/foo/bar/";
flareWebserviceClient = new FlareWebserviceClientImpl(httpClient, URI.create("http://foo.bar:1234" + path));
var structuredQuery = "foo".getBytes();
Expand All @@ -77,4 +78,26 @@ public void testName() throws Exception {

assertEquals(path + "query/execute", postCaptor.getValue().getURI().getPath());
}

@Test
void testNullBaseUrlFails() throws Exception {
FlareWebserviceClientSpringConfig config = new FlareWebserviceClientSpringConfig();

IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> config.flareWebserviceClient(httpClient));

assertEquals("FLARE_BASE_URL is not set.", e.getMessage());
}

@Test
void testIllegalBaseUrlFails() throws Exception {
FlareWebserviceClientSpringConfig config = new FlareWebserviceClientSpringConfig();
String invalidUrl = "{ßöäü;";
ReflectionTestUtils.setField(config, "flareBaseUrl", invalidUrl);

IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> config.flareWebserviceClient(httpClient));

assertEquals("Could not parse FLARE_BASE_URL '" + invalidUrl + "' as URI.", e.getMessage());
}
}

0 comments on commit 8ac6fa6

Please sign in to comment.