BAEL-5771 Add code for url query manipulation in Java (#13820)
This commit is contained in:
parent
7a426829cb
commit
ae52ba3968
|
@ -24,6 +24,30 @@
|
||||||
<artifactId>jsoup</artifactId>
|
<artifactId>jsoup</artifactId>
|
||||||
<version>${jsoup.version}</version>
|
<version>${jsoup.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>javax.ws.rs-api</artifactId>
|
||||||
|
<version>2.1.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.core</groupId>
|
||||||
|
<artifactId>jersey-common</artifactId>
|
||||||
|
<version>2.22.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
<version>6.0.6</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.urlquerymanipulation;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.UriBuilder;
|
||||||
|
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
|
public class UrlQueryManipulationUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingApacheUriBuilder_thenParametersAreCorrectlyAdded() throws URISyntaxException {
|
||||||
|
String url = "baeldung.com";
|
||||||
|
String key = "article";
|
||||||
|
String value = "alpha";
|
||||||
|
URI uri = new URIBuilder(url).addParameter(key, value)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals("baeldung.com?article=alpha", uri.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingJavaUriBuilder_thenParametersAreCorrectlyAdded() {
|
||||||
|
String url = "baeldung.com";
|
||||||
|
String key = "article";
|
||||||
|
String value = "beta";
|
||||||
|
URI uri = UriBuilder.fromUri(url)
|
||||||
|
.queryParam(key, value)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals("baeldung.com?article=beta", uri.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingSpringUriComponentsBuilder_thenParametersAreCorrectlyAdded() {
|
||||||
|
String url = "baeldung.com";
|
||||||
|
String key = "article";
|
||||||
|
String value = "charlie";
|
||||||
|
URI uri = UriComponentsBuilder.fromUriString(url)
|
||||||
|
.queryParam(key, value)
|
||||||
|
.build()
|
||||||
|
.toUri();
|
||||||
|
|
||||||
|
assertEquals("baeldung.com?article=charlie", uri.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue