Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode URL whitespace #208

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
}