-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(RDF API): Incorrect internal IRIs (#4383)
* Possible fix for RDF API using incorrect base URL * Revert "Possible fix for RDF API using incorrect base URL" This reverts commit 77c5c38. * Removed unused returns * fixed variable naming * Updated tests to validate on bug * fix for incorrect internal IRI + some refactoring * Removed unneeded code (test on PR server shows no port in IRI while localhost did) * Revert "fixed variable naming" This reverts commit de0650c. * Reverted wildcard imports done by IDE * Added test (+ some refactoring to add tests properly) * formatting * auto-formatting * Tests do not use actual ports but only override value due to failing Azure (no permission to use port 80) * Some code adjustments for more coverage
- Loading branch information
1 parent
fddd2ad
commit 90565b5
Showing
5 changed files
with
174 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dependencies { | ||
implementation 'org.graalvm.js:js:23.0.0' | ||
implementation 'org.graalvm.js:js-scriptengine:23.0.0' | ||
testImplementation 'io.javalin:javalin-testtools:6.2.0' | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/molgenis-emx2/src/main/java/org/molgenis/emx2/utils/URLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.molgenis.emx2.utils; | ||
|
||
import io.javalin.http.Context; | ||
import java.util.Objects; | ||
|
||
public class URLUtils { | ||
public static String extractBaseURL(Context ctx) { | ||
String host = Objects.requireNonNull(ctx.host()); | ||
String[] hostSplit = host.split(":", 2); | ||
if (hostSplit.length == 2 && isDefaultPort(ctx.scheme(), hostSplit[1])) { | ||
host = hostSplit[0]; | ||
} | ||
|
||
return ctx.scheme() | ||
+ "://" | ||
+ host | ||
// ctx.contextPath() should start with "/" | ||
+ (!ctx.contextPath().isEmpty() ? ctx.contextPath() + "/" : "/"); | ||
} | ||
|
||
public static boolean isDefaultPort(String scheme, String port) { | ||
return (scheme.equals("http") && port.equals("80")) | ||
|| (scheme.equals("https") && port.equals("443")); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
backend/molgenis-emx2/src/test/java/org/molgenis/emx2/utils/URLUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.molgenis.emx2.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import io.javalin.Javalin; | ||
import io.javalin.http.Context; | ||
import io.javalin.http.Handler; | ||
import io.javalin.testtools.JavalinTest; | ||
import jakarta.servlet.ServletOutputStream; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Original test used `app.start(port)` instead to actually test when a port is set if the behaviour | ||
* was correct or not, but simplified the tests as Azure failed to build due to no permission to use | ||
* port 80. Current tests only validate by setting `config.contextResolver.host`. | ||
*/ | ||
class URLUtilsTest { | ||
private void runTestConfig(Handler handler, String expected, String host, String contextPath) { | ||
Javalin app = | ||
Javalin.create( | ||
config -> { | ||
config.router.ignoreTrailingSlashes = true; | ||
config.router.treatMultipleSlashesAsSingleSlash = true; | ||
if (host != null) config.contextResolver.host = ctx -> host; | ||
if (contextPath != null) config.router.contextPath = contextPath; | ||
}); | ||
app.get("/test", handler); | ||
|
||
JavalinTest.test( | ||
app, | ||
((server, client) -> { | ||
if (contextPath != null) { | ||
assertEquals(expected, client.get(contextPath + "/test").body().string()); | ||
} else { | ||
assertEquals(expected, client.get("/test").body().string()); | ||
} | ||
})); | ||
} | ||
|
||
private void contextWrapper(Context ctx, byte[] bytes) { | ||
ServletOutputStream outputStream = ctx.outputStream(); | ||
try { | ||
outputStream.write(bytes); | ||
outputStream.flush(); | ||
outputStream.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
void testBaseUrlMolgenis() { | ||
runTestConfig( | ||
(ctx) -> contextWrapper(ctx, URLUtils.extractBaseURL(ctx).getBytes()), | ||
"http://molgenis.org/", | ||
"molgenis.org", | ||
null); | ||
} | ||
|
||
@Test | ||
void testBaseUrlMolgenis80() { | ||
runTestConfig( | ||
(ctx) -> contextWrapper(ctx, URLUtils.extractBaseURL(ctx).getBytes()), | ||
"http://molgenis.org/", | ||
"molgenis.org:80", | ||
null); | ||
} | ||
|
||
@Test | ||
void testBaseUrlMolgenis8080() { | ||
runTestConfig( | ||
(ctx) -> contextWrapper(ctx, URLUtils.extractBaseURL(ctx).getBytes()), | ||
"http://molgenis.org:8080/", | ||
"molgenis.org:8080", | ||
null); | ||
} | ||
|
||
/** Most complex scenario. */ | ||
@Test | ||
void testBaseUrlMolgenisSubdir8080() { | ||
runTestConfig( | ||
(ctx) -> contextWrapper(ctx, URLUtils.extractBaseURL(ctx).getBytes()), | ||
"http://molgenis.org:8080/subdir/", | ||
"molgenis.org:8080", | ||
"/subdir"); | ||
} | ||
|
||
@Test | ||
void testContextPathTrailingSlash() { | ||
runTestConfig( | ||
(ctx) -> contextWrapper(ctx, URLUtils.extractBaseURL(ctx).getBytes()), | ||
"http://molgenis.org:8080/subdir/", | ||
"molgenis.org:8080", | ||
"/subdir/"); | ||
} | ||
|
||
@Test | ||
void testDefaultPort() { | ||
assertAll( | ||
() -> assertTrue(URLUtils.isDefaultPort("http", "80")), | ||
() -> assertFalse(URLUtils.isDefaultPort("http", "8080")), | ||
() -> assertTrue(URLUtils.isDefaultPort("https", "443")), | ||
() -> assertFalse(URLUtils.isDefaultPort("https", "80"))); | ||
} | ||
} |