[JAVA-11283] Fix and cleanup live test for HttpClient URL expansion

This commit is contained in:
Haroon Khan 2022-04-09 12:01:45 +01:00
parent 4bcb36ff41
commit 2712a77d08
2 changed files with 6 additions and 12 deletions

View File

@ -12,5 +12,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Reading an HTTP Response Body as a String in Java](https://www.baeldung.com/java-http-response-body-as-string) - [Reading an HTTP Response Body as a String in Java](https://www.baeldung.com/java-http-response-body-as-string)
- [How To Get Cookies From the Apache HttpClient Response](https://www.baeldung.com/java-apache-httpclient-cookies) - [How To Get Cookies From the Apache HttpClient Response](https://www.baeldung.com/java-apache-httpclient-cookies)
- [Enabling Logging for Apache HttpClient](https://www.baeldung.com/apache-httpclient-enable-logging) - [Enabling Logging for Apache HttpClient](https://www.baeldung.com/apache-httpclient-enable-logging)
- [Unshorten URLs with HttpClient](https://www.baeldung.com/unshorten-url-httpclient) - [Expand Shortened URLs with Apache HttpClient](https://www.baeldung.com/unshorten-url-httpclient)
- More articles: [[<-- prev]](../httpclient) - More articles: [[<-- prev]](../httpclient)

View File

@ -1,4 +1,4 @@
package com.baeldung.httpclient.rare; package com.baeldung.httpclient.expandurl;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@ -22,35 +22,29 @@ import java.util.List;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
public class HttpClientUnshortenLiveTest { public class HttpClientExpandUrlLiveTest {
private CloseableHttpClient client; private CloseableHttpClient client;
// fixtures
@Before @Before
public final void before() { public final void before() {
client = HttpClientBuilder.create().disableRedirectHandling().build(); client = HttpClientBuilder.create().disableRedirectHandling().build();
} }
// tests
@Test @Test
public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException { public final void givenShortenedOnce_whenUrlIsExpanded_thenCorrectResult() throws IOException {
final String expectedResult = "http://www.baeldung.com/rest-versioning"; final String expectedResult = "http://www.baeldung.com/rest-versioning";
final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1"); final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1");
assertThat(actualResult, equalTo(expectedResult)); assertThat(actualResult, equalTo(expectedResult));
} }
@Test @Test
public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException { public final void givenShortenedMultiple_whenUrlIsExpanded_thenCorrectResult() throws IOException {
final String expectedResult = "http://www.baeldung.com/rest-versioning"; final String expectedResult = "https://www.baeldung.com/rest-versioning";
final String actualResult = expand("http://t.co/e4rDDbnzmk"); final String actualResult = expand("http://t.co/e4rDDbnzmk");
assertThat(actualResult, equalTo(expectedResult)); assertThat(actualResult, equalTo(expectedResult));
} }
// API
private String expand(final String urlArg) throws IOException { private String expand(final String urlArg) throws IOException {
String originalUrl = urlArg; String originalUrl = urlArg;
String newUrl = expandSingleLevel(originalUrl); String newUrl = expandSingleLevel(originalUrl);