From b6ee074149a410879effd517a04eec533b21787d Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Thu, 10 Aug 2023 15:04:47 -0500 Subject: [PATCH] fix captcha shortening url path resolution --- .../captcha/ShortCodeExpander.java | 2 +- .../captcha/ShortCodeExpanderTest.java | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 service/src/test/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpanderTest.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpander.java b/service/src/main/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpander.java index ae867970c..546b6b3eb 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpander.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpander.java @@ -28,7 +28,7 @@ public ShortCodeExpander(final HttpClient client, final String shortenerHost) { } public Optional retrieve(final String shortCode) throws IOException { - final URI uri = shortenerHost.resolve("/" + shortCode); + final URI uri = shortenerHost.resolve(shortCode); final HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); try { diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpanderTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpanderTest.java new file mode 100644 index 000000000..4541174a4 --- /dev/null +++ b/service/src/test/java/org/whispersystems/textsecuregcm/captcha/ShortCodeExpanderTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2023 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.captcha; + +import com.google.api.Http; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import javax.net.ssl.SSLSession; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ShortCodeExpanderTest { + + @Test + public void testUriResolution() throws IOException, InterruptedException { + final HttpClient httpClient = mock(HttpClient.class); + final ShortCodeExpander expander = new ShortCodeExpander(httpClient, "https://www.example.org/shortener/"); + when(httpClient + .send(argThat(req -> req.uri().toString().equals("https://www.example.org/shortener/shorturl")), any())) + .thenReturn(new FakeResponse(200, "longurl")); + assertThat(expander.retrieve("shorturl").get()).isEqualTo("longurl"); + } + + private record FakeResponse(int statusCode, String body) implements HttpResponse { + + @Override + public HttpRequest request() { + return null; + } + + @Override + public Optional> previousResponse() { + return Optional.empty(); + } + + @Override + public HttpHeaders headers() { + return null; + } + + @Override + public Optional sslSession() { + return Optional.empty(); + } + + @Override + public URI uri() { + return null; + } + + @Override + public HttpClient.Version version() { + return null; + } + + } + +}