fix: return url without port if default port protocol is used (#1511)

* fix: return url without port if default port protocol is used

* tests: set server port to 8080 and assert that all generated url use port 8080
This commit is contained in:
Florian BEZANNIER
2024-01-31 08:34:07 +01:00
committed by GitHub
parent 537a942021
commit a5dda8cb47
5 changed files with 61 additions and 11 deletions

View File

@@ -102,7 +102,7 @@ public class PropertyBasedArtifactUrlHandler implements ArtifactUrlHandler {
String urlPattern = protocol.getRef();
for (final Entry<String, String> entry : entrySet) {
if (entry.getKey().equals(PORT_PLACEHOLDER)) {
if (List.of(PORT_PLACEHOLDER,PORT_REQUEST_PLACEHOLDER).contains(entry.getKey())) {
urlPattern = urlPattern.replace(":{" + entry.getKey() + "}",
ObjectUtils.isEmpty(entry.getValue()) ? "" : (":" + entry.getValue()));
} else {
@@ -157,8 +157,8 @@ public class PropertyBasedArtifactUrlHandler implements ArtifactUrlHandler {
if (requestUri == null) {
return getPort(protocol);
}
return requestUri.getPort() > 0 ? String.valueOf(requestUri.getPort()) : getPort(protocol);
// if port undefined then default protocol port is used
return requestUri.getPort() > 0 ? String.valueOf(requestUri.getPort()) : "";
}
private static String getRequestHost(final UrlProtocol protocol, final URI requestUri) {

View File

@@ -181,6 +181,24 @@ public class PropertyBasedArtifactUrlHandlerTest {
+ SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE));
}
@Test
@Description("Verfies that if default protocol port in request is used then url is returned without port")
public void urlGenerationWithPortFromRequestForHttps() throws URISyntaxException {
String protocol = "https";
final UrlProtocol proto = new UrlProtocol();
proto.setRef(
"{protocolRequest}://{hostnameRequest}:{portRequest}/{tenant}/controller/v1/{controllerId}/softwaremodules/{softwareModuleId}/artifacts/{artifactFileName}");
proto.setProtocol(protocol);
properties.getProtocols().put("download-http", proto);
URI uri = new URI(protocol+"://anotherHost.com");
final List<ArtifactUrl> urls = urlHandlerUnderTest.getUrls(placeholder, ApiType.DDI, uri);
assertThat(urls).containsExactly(new ArtifactUrl(protocol.toUpperCase(), "download-http",
uri +"/" + TENANT + "/controller/v1/" + CONTROLLER_ID + "/softwaremodules/"
+ SOFTWAREMODULEID + "/artifacts/" + FILENAME_ENCODE));
}
@Test
@Description("Verfies that the domain of the statically defined hostname is replaced with the domain of the request.")
public void urlGenerationWithDomainFromRequest() throws URISyntaxException {