Skip to content

Commit

Permalink
added test in case scheme is https but backendbasepath is http
Browse files Browse the repository at this point in the history
  • Loading branch information
gcornacchia committed Jul 12, 2023
1 parent 62bfa66 commit 6fe32e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,20 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
}
URL url = new URL(backendBasePath);
String port = url.getPort() == -1 ? ":" + url.getDefaultPort() : ":" + url.getPort();
Boolean adjustedHost = false;
if (port.equals(":443") || port.equals(":80")) port = "";
if (swagger.get("host") == null) {
LOG.debug("Adding new host {}{} to Swagger-File based on backendBasePath: {}", url.getHost(), port, backendBasePath);
((ObjectNode) swagger).put("host", url.getHost() + port);
LOG.info("Used the backendBasePath: {} to adjust host the API-Specification.", backendBasePath);
adjustedHost = true;
}else {
if(swagger.get("host").asText().equals(url.getHost()+port)) {
LOG.debug("Swagger Host: '{}' already matches backendBasePath: '{}'. Nothing to do.",swagger.get("host").asText(),backendBasePath);
} else if (CoreParameters.getInstance().isOverrideSpecBasePath()){
LOG.debug("Replacing existing host: '"+swagger.get("host").asText()+"' in Swagger-File to '"+url.getHost()+port+"' based on configured backendBasePath: '"+backendBasePath+"'");
((ObjectNode)swagger).put("host", url.getHost()+port);
adjustedHost = true;
}
}
//what if the backendBasePath is http?
Expand All @@ -113,7 +116,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
LOG.debug("Adding protocol: {} to Swagger-Definition", url.getProtocol());
((ObjectNode) swagger).set("schemes", newSchemes);
} else {
if (CoreParameters.getInstance().isOverrideSpecBasePath()) {
if (CoreParameters.getInstance().isOverrideSpecBasePath() || adjustedHost) {
//I may have a situation where a backendbasepath in http must overwrite host but in swagger file it's declared a scheme in https which is not coherent
ArrayNode schemes = (ArrayNode) swagger.get("schemes");
schemes.removeAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,20 @@ public void testReplaceAlsoHostInSwagger() throws IOException{
Assert.assertEquals(swagger.get("schemes").size(), 1);
}

@Test
public void testSwaggerWithoutHost() throws IOException{
CoreParameters.getInstance().setOverrideSpecBasePath(false);
byte[] content = getSwaggerContent(testPackage + "/petstore-without-host.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "Test-API");
apiDefinition.configureBasePath("http://anotherHost/test", null);

Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Assert.assertEquals(swagger.get("host").asText(), "anotherHost");
Assert.assertEquals(swagger.get("basePath").asText(), "/v2");
Assert.assertEquals(swagger.get("schemes").get(0).asText(), "http");
Assert.assertEquals(swagger.get("schemes").size(), 1);
}


}

0 comments on commit 6fe32e6

Please sign in to comment.