diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index d914e7656..4e9e8cbcb 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -20,8 +20,10 @@ import java.io.IOException; import java.net.URI; +import java.net.URLEncoder; import java.util.Map; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.opentripplanner.middleware.utils.ConfigUtils.getConfigPropertyAsText; /** @@ -327,7 +329,11 @@ public static int getPushInfo(String toUser) { try { Map headers = Map.of("Accept", "application/json"); var httpResponse = HttpUtils.httpRequestRawResponse( - URI.create(PUSH_API_URL + "/devices/get?api_key=" + PUSH_API_KEY + "&user=" + toUser), + URI.create(getPushDevicesUrl(String.format( + "%s/devices/get?api_key=%s&user=", + PUSH_API_URL, + PUSH_API_KEY + ), toUser)), 1000, HttpMethod.GET, headers, @@ -346,6 +352,10 @@ public static int getPushInfo(String toUser) { return 0; } + static String getPushDevicesUrl(String baseUrl, String toUser) { + return baseUrl + URLEncoder.encode(toUser, UTF_8); + } + /** * Poll the push middleware for the number of devices registered to receive push notifications * for the specified user, and update the corresponding field in memory and Mongo. diff --git a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java index 495f1e1bf..983fb438c 100644 --- a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java +++ b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; +import static org.opentripplanner.middleware.utils.NotificationUtils.getPushDevicesUrl; import static org.opentripplanner.middleware.utils.NotificationUtils.getTwilioLocale; /** @@ -30,4 +31,14 @@ void canGetTwilioLocale() { void twilioLocaleDefaultsToEnglish(String locale) { Assertions.assertEquals("en", getTwilioLocale(locale)); } + + @Test + void testGetPushDevicesUrl() { + String email = "first.last+suffix@example.com"; + String base = "https://get.example.com/devices/?user="; + Assertions.assertEquals( + "https://get.example.com/devices/?user=first.last%2Bsuffix%40example.com", + getPushDevicesUrl(base, email) + ); + } }