[2883] update swagger url/ add unit test

This commit is contained in:
katie_smilecdr 2021-08-13 13:54:27 -04:00
parent dfb8ec4680
commit 36d2a8f3ce
3 changed files with 25 additions and 4 deletions

View File

@ -267,10 +267,17 @@ public class OpenApiInterceptor {
return false;
}
public String removeTrailingSlash(String theUrl) {
while(theUrl.endsWith("/")) {
theUrl = theUrl.substring(0, theUrl.length() - 1);
}
return theUrl;
}
@SuppressWarnings("unchecked")
private void serveSwaggerUiHtml(ServletRequestDetails theRequestDetails, HttpServletResponse theResponse) throws IOException {
CapabilityStatement cs = getCapabilityStatement(theRequestDetails);
String baseUrl = removeTrailingSlash(cs.getImplementation().getUrl());
theResponse.setStatus(200);
theResponse.setContentType(Constants.CT_HTML);
@ -281,9 +288,9 @@ public class OpenApiInterceptor {
context.setVariable("DESCRIPTION", cs.getImplementation().getDescription());
context.setVariable("SERVER_NAME", cs.getSoftware().getName());
context.setVariable("SERVER_VERSION", cs.getSoftware().getVersion());
context.setVariable("BASE_URL", cs.getImplementation().getUrl());
context.setVariable("BASE_URL", baseUrl);
context.setVariable("BANNER_IMAGE_URL", getBannerImage());
context.setVariable("OPENAPI_DOCS", cs.getImplementation().getUrl() + "api-docs");
context.setVariable("OPENAPI_DOCS", baseUrl + "/api-docs");
context.setVariable("FHIR_VERSION", cs.getFhirVersion().toCode());
context.setVariable("FHIR_VERSION_CODENAME", FhirVersionEnum.forVersionString(cs.getFhirVersion().toCode()).name());

View File

@ -45,7 +45,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "[[@{/api-docs(page=${PAGE})}]]",
url: "[[${OPENAPI_DOCS} + '?page=' + ${PAGE}]]",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [

View File

@ -195,6 +195,20 @@ public class OpenApiInterceptorTest {
assertThat(buttonTexts.toString(), buttonTexts, Matchers.contains("All", "System Level Operations", "OperationDefinition 1", "Observation", "Patient"));
}
@Test
public void testRemoveTrailingSlash() {
OpenApiInterceptor interceptor = new OpenApiInterceptor();
String url1 = interceptor.removeTrailingSlash("http://localhost:8000");
String url2 = interceptor.removeTrailingSlash("http://localhost:8000/");
String url3 = interceptor.removeTrailingSlash("http://localhost:8000//");
String expect = "http://localhost:8000";
assertEquals(expect, url1);
assertEquals(expect, url2);
assertEquals(expect, url3);
}
private String fetchSwaggerUi(String url) throws IOException {
String resp;
HttpGet get = new HttpGet(url);