Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for Empty Flare Base Url and Catch Invalid URI Errors #83

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}