Skip to content

Commit

Permalink
Merge pull request #202 from ibi-group/encode-push-req-params
Browse files Browse the repository at this point in the history
fix(NotificationUtils): Encode user email to retrieve push devices.
  • Loading branch information
binh-dam-ibigroup authored Dec 20, 2023
2 parents b8786a7 + 0e8ba24 commit 86a6ef1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -327,7 +329,11 @@ public static int getPushInfo(String toUser) {
try {
Map<String, String> 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,
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -30,4 +31,14 @@ void canGetTwilioLocale() {
void twilioLocaleDefaultsToEnglish(String locale) {
Assertions.assertEquals("en", getTwilioLocale(locale));
}

@Test
void testGetPushDevicesUrl() {
String email = "[email protected]";
String base = "https://get.example.com/devices/?user=";
Assertions.assertEquals(
"https://get.example.com/devices/?user=first.last%2Bsuffix%40example.com",
getPushDevicesUrl(base, email)
);
}
}

0 comments on commit 86a6ef1

Please sign in to comment.