BAEL-276 - collectingAndThen

This commit is contained in:
slavisa-baeldung 2016-10-29 08:28:29 +02:00
parent 539e5fed63
commit 568a1d6f87
1 changed files with 7 additions and 5 deletions

View File

@ -16,6 +16,9 @@ import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.joining;
public class EncoderDecoderTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoderTest.class);
@ -58,11 +61,9 @@ public class EncoderDecoderTest {
requestParams.put("key2", "value@!$2");
requestParams.put("key3", "value%3");
String encodedQuery = requestParams.keySet().stream()
String encodedURL = requestParams.keySet().stream()
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
.collect(Collectors.joining("&"));
String encodedURL = "http://www.baeldung.com?" + encodedQuery;
.collect(collectingAndThen(joining("&"), params -> "http://www.baeldung.com?" + params));
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
}
@ -70,11 +71,12 @@ public class EncoderDecoderTest {
@Test
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
URL url = new URL(testUrl);
String query = url.getQuery();
String decodedQuery = Arrays.stream(query.split("&"))
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
.collect(Collectors.joining("&"));
.collect(joining("&"));
Assert.assertEquals(
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);