From 617c3f3534e47a5b7ef1b1cc72e15891643dc464 Mon Sep 17 00:00:00 2001 From: knotekt Date: Thu, 2 Nov 2023 13:36:21 +0100 Subject: [PATCH] Encode URL whitespace --- .../java/cz/smarteon/loxone/LoxoneHttp.java | 4 ++- src/test/kotlin/LoxoneHttpTest.kt | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/cz/smarteon/loxone/LoxoneHttp.java b/src/main/java/cz/smarteon/loxone/LoxoneHttp.java index 20644408..3e9e30ba 100644 --- a/src/main/java/cz/smarteon/loxone/LoxoneHttp.java +++ b/src/main/java/cz/smarteon/loxone/LoxoneHttp.java @@ -21,6 +21,8 @@ public class LoxoneHttp { private static final Logger LOG = LoggerFactory.getLogger(LoxoneHttp.class); private static final int MAX_REDIRECTS = 5; + private static final String URL_WHITESPACE = " "; + private static final String ENCODED_URL_WHITESPACE = "%20"; // temporarily relaxed visibility to allow deprecated LoxoneAuth constructor @SuppressWarnings("checkstyle:VisibilityModifier") @@ -109,7 +111,7 @@ T get(URL url, Command.Type type, Map properties, Class r private URL urlFromCommand(String command) { try { - return endpoint.httpUrl(command); + return endpoint.httpUrl(command.replace(URL_WHITESPACE, ENCODED_URL_WHITESPACE)); } catch (MalformedURLException e) { throw new IllegalArgumentException("Command " + command + " produces malformed URL"); } diff --git a/src/test/kotlin/LoxoneHttpTest.kt b/src/test/kotlin/LoxoneHttpTest.kt index 3cbfec0f..aa090711 100644 --- a/src/test/kotlin/LoxoneHttpTest.kt +++ b/src/test/kotlin/LoxoneHttpTest.kt @@ -72,4 +72,29 @@ class LoxoneHttpTest { get { loxoneHttp.lastUrl }.isEqualTo(URL(finalLocation)) } } + + @Test + fun `should encode whitespace`() { + onRequest() + .havingMethodEqualTo("GET") + .havingPathEqualTo("/test%20whitespace") + .respond() + .withStatus(200) + .withBody("\"testString\"") + + expectThat( + loxoneHttp.get( + Command( + "/test whitespace", + Command.Type.JSON, + String::class.java, + true, + false, + MiniserverType.KNOWN + ) + ) + ) { + isEqualTo("testString") + } + } }