Skip to content

Commit

Permalink
Merge pull request #208 from Smarteon/encodespace
Browse files Browse the repository at this point in the history
Encode URL whitespace
  • Loading branch information
jimirocks authored Nov 2, 2023
2 parents 3bbedd3 + 617c3f3 commit a0c08b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/cz/smarteon/loxone/LoxoneHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -109,7 +111,7 @@ <T> T get(URL url, Command.Type type, Map<String, String> properties, Class<T> 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");
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/kotlin/LoxoneHttpTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

0 comments on commit a0c08b2

Please sign in to comment.