Manage 404s when configured servers don't have package

This commit is contained in:
dotasek 2023-07-19 16:07:32 -04:00
parent 1c79531d31
commit c9dd40640c
4 changed files with 50 additions and 9 deletions

View File

@ -50,23 +50,21 @@ public class PackageClient {
return fetchCached(getPackageTarballUrl(id, ver));
}
private String getPackageTarballUrl(String id, String ver) {
private String getPackageTarballUrl(String id, String ver) throws IOException {
if (server.getServerType() == PackageServer.PackageServerType.NPM) {
return getNpmServerTarballUrl(id, ver);
}
return Utilities.pathURL(address, id, ver);
}
private String getNpmServerTarballUrl(String id, String ver) {
private String getNpmServerTarballUrl(String id, String ver) throws IOException {
String packageDescriptorUrl = Utilities.pathURL(address, id, ver);
JsonObject json;
try {
json = fetchJson(packageDescriptorUrl);
JsonObject dist = json.getJsonObject("dist");
return dist.getJsonString("tarball").asString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public InputStream fetch(PackageInfo info) throws IOException {

View File

@ -213,7 +213,7 @@ public class FhirSettings {
public static boolean isIgnoreDefaultPackageServers() {
getInstance();
if (instance.fhirSettings.getPackageManagement() == null) {
if (instance.fhirSettings.getPackageManagement() == null || instance.fhirSettings.getPackageManagement().getIgnoreDefaultServers() == null) {
return false;
}
return instance.fhirSettings.getPackageManagement().getIgnoreDefaultServers();

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.utilities.npm;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
@ -14,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class BasePackageCacheManagerTests {
@Test
public void testPrivatePackageBasicAuth() throws IOException {
public void testPackageBasicAuth() throws IOException {
BasePackageCacheManager basePackageCacheManager = getFakeBasePackageCacheManager();
MockPackageServer server = new MockPackageServer();
@ -34,13 +35,51 @@ public class BasePackageCacheManagerTests {
BasePackageCacheManager.InputStreamWithSrc inputWithSrc = basePackageCacheManager.loadFromPackageServer("example.fhir.uv.myig", "0.2.0");
assertCorrectPackageContent(inputWithSrc);
server.shutdown();
}
@Test
@DisplayName("Test that package management moves to next server after 404")
public void testPackageWithConfiguredServer404() throws IOException {
BasePackageCacheManager basePackageCacheManager = getFakeBasePackageCacheManager();
MockPackageServer serverA = new MockPackageServer();
serverA.enqueueResponseCode(404);
MockPackageServer serverB = new MockPackageServer();
serverB.enqueueDummyPackageDescription();
serverB.enqueueDummyPackage();
String packageServerAUrl = serverA.getPackageServerUrl();
String packageServerBUrl = serverB.getPackageServerUrl();
PackageServer testServerA = new PackageServer(packageServerAUrl)
.withAuthenticationMode(SimpleHTTPClient.AuthenticationMode.BASIC)
.withServerType(PackageServer.PackageServerType.NPM);
PackageServer testServerB = new PackageServer(packageServerBUrl)
.withAuthenticationMode(SimpleHTTPClient.AuthenticationMode.BASIC)
.withServerType(PackageServer.PackageServerType.NPM);
basePackageCacheManager.addPackageServer(testServerA);
basePackageCacheManager.addPackageServer(testServerB);
basePackageCacheManager.myPackageServers.addAll(PackageServer.defaultServers());
BasePackageCacheManager.InputStreamWithSrc inputWithSrc = basePackageCacheManager.loadFromPackageServer("example.fhir.uv.myig", "0.2.0");
assertCorrectPackageContent(inputWithSrc);
serverA.shutdown();
serverB.shutdown();
}
private static void assertCorrectPackageContent(BasePackageCacheManager.InputStreamWithSrc inputWithSrc) throws IOException {
NpmPackage npmPackage = NpmPackage.fromPackage(inputWithSrc.stream, inputWithSrc.url, false);
assertEquals("Dummy IG For Testing", npmPackage.title())
;
assertEquals("Dummy IG description (built Thu, Jul 6, 2023 15:16-0400-04:00)", npmPackage.description());
server.shutdown();
}
@Nonnull

View File

@ -48,6 +48,10 @@ public class MockPackageServer {
server.enqueue(new MockResponse().setBody(getDummyPackageAsBuffer()));
}
public void enqueueResponseCode(int code) throws IOException {
server.enqueue(new MockResponse().setResponseCode(code));
}
private Buffer getDummyPackageAsBuffer() throws IOException {
byte[] fileData = this.getClass().getResourceAsStream("/npm/dummy-package.tgz").readAllBytes();