Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b5cc66

Browse files
authored
Fix handling of URI query params (#38)
1 parent ada8193 commit 6b5cc66

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

‎src/main/java/com/nordstrom/common/uri/UriUtils.java‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.net.URI;
44
import java.net.URISyntaxException;
55
import java.net.URL;
6+
import java.net.URLEncoder;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.IntStream;
610

711
public class UriUtils {
812

@@ -16,11 +20,11 @@ private UriUtils() {
1620
* and specified path string.
1721
*
1822
* @param context context URL
19-
* @param path path component
20-
* @return URI for the specified path within the provided context
23+
* @param pathAndParams path component and query parameters
24+
* @return URI for the specified path and parameters within the provided context
2125
*/
22-
public static URI uriForPath(final URL context, final Stringpath) {
23-
return makeBasicURI(context.getProtocol(), context.getHost(), context.getPort(), path);
26+
public static URI uriForPath(final URL context, final String... pathAndParams) {
27+
return makeBasicURI(context.getProtocol(), context.getHost(), context.getPort(), pathAndParams);
2428
}
2529

2630
/**
@@ -29,12 +33,29 @@ public static URI uriForPath(final URL context, final String path) {
2933
* @param scheme scheme name
3034
* @param host host name
3135
* @param port port number
32-
* @param path path
36+
* @param pathAndParams path and query parameters
3337
* @return assembled basic URI
3438
*/
35-
public static URI makeBasicURI(final String scheme, final String host, final int port, final String path) {
39+
public static URI makeBasicURI(final String scheme, final String host, final int port,
40+
final String... pathAndParams) {
3641
try {
37-
return new URI(scheme, null, host, port, path, null, null);
42+
String path = (pathAndParams.length > 0) ? pathAndParams[0] : null;
43+
String query = null;
44+
if (pathAndParams.length > 1) {
45+
query = IntStream.range(1, pathAndParams.length).mapToObj(i -> {
46+
try {
47+
String param = pathAndParams[i];
48+
int index = param.indexOf("=");
49+
if (index == -1) return URLEncoder.encode(param, StandardCharsets.UTF_8.toString());
50+
String key = URLEncoder.encode(param.substring(0, index), StandardCharsets.UTF_8.toString());
51+
String val = URLEncoder.encode(param.substring(index + 1), StandardCharsets.UTF_8.toString());
52+
return key + "=" + val;
53+
} catch (Exception e) {
54+
throw new IllegalArgumentException(e.getMessage(), e);
55+
}
56+
}).collect(Collectors.joining("&"));
57+
}
58+
return new URI(scheme, null, host, port, path, query, null);
3859
} catch (URISyntaxException e) {
3960
throw new IllegalArgumentException(e.getMessage(), e);
4061
}

‎src/test/java/com/nordstrom/common/uri/UriUtilsTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010

1111
public class UriUtilsTest {
1212

13+
@Test
14+
public void testUriWithoutPath() throws MalformedURLException {
15+
URL context = URI.create("http://user:pswd@host.com:80/context/path?id=123#frag").toURL();
16+
URI pathURI = UriUtils.uriForPath(context);
17+
assertEquals(pathURI.getScheme(), "http", "Scheme mismatch");
18+
assertEquals(pathURI.getUserInfo(), null, "User info mismatch");
19+
assertEquals(pathURI.getHost(), "host.com", "Host mismatch");
20+
assertEquals(pathURI.getPort(), 80, "Post mismatch");
21+
assertEquals(pathURI.getPath(), "", "Path mismatch");
22+
assertEquals(pathURI.getQuery(), null, "Query mismatch");
23+
assertEquals(pathURI.getFragment(), null, "Fragment mismatch");
24+
}
25+
1326
@Test
1427
public void testUriForPath() throws MalformedURLException {
1528
URL context = URI.create("http://user:pswd@host.com:80/context/path?id=123#frag").toURL();
@@ -23,8 +36,33 @@ public void testUriForPath() throws MalformedURLException {
2336
assertEquals(pathURI.getFragment(), null, "Fragment mismatch");
2437
}
2538

39+
@Test
40+
public void testUriForPathAndParams() throws MalformedURLException {
41+
URL context = URI.create("http://user:pswd@host.com:80/context/path?id=123#frag").toURL();
42+
URI pathURI = UriUtils.uriForPath(context, "/target", "id=321", "q=fresh & clean");
43+
assertEquals(pathURI.getScheme(), "http", "Scheme mismatch");
44+
assertEquals(pathURI.getUserInfo(), null, "User info mismatch");
45+
assertEquals(pathURI.getHost(), "host.com", "Host mismatch");
46+
assertEquals(pathURI.getPort(), 80, "Post mismatch");
47+
assertEquals(pathURI.getPath(), "/target", "Path mismatch");
48+
assertEquals(pathURI.getQuery(), "id=321&q=fresh+%26+clean", "Query mismatch");
49+
assertEquals(pathURI.getFragment(), null, "Fragment mismatch");
50+
}
51+
2652
@Test
2753
public void testMakeBasicURI() {
54+
URI basicURI = UriUtils.makeBasicURI("http", "host.com", 80);
55+
assertEquals(basicURI.getScheme(), "http", "Scheme mismatch");
56+
assertEquals(basicURI.getUserInfo(), null, "User info mismatch");
57+
assertEquals(basicURI.getHost(), "host.com", "Host mismatch");
58+
assertEquals(basicURI.getPort(), 80, "Post mismatch");
59+
assertEquals(basicURI.getPath(), "", "Path mismatch");
60+
assertEquals(basicURI.getQuery(), null, "Query mismatch");
61+
assertEquals(basicURI.getFragment(), null, "Fragment mismatch");
62+
}
63+
64+
@Test
65+
public void testMakeBasicURIWithPath() {
2866
URI basicURI = UriUtils.makeBasicURI("http", "host.com", 80, "/target");
2967
assertEquals(basicURI.getScheme(), "http", "Scheme mismatch");
3068
assertEquals(basicURI.getUserInfo(), null, "User info mismatch");
@@ -34,4 +72,16 @@ public void testMakeBasicURI() {
3472
assertEquals(basicURI.getQuery(), null, "Query mismatch");
3573
assertEquals(basicURI.getFragment(), null, "Fragment mismatch");
3674
}
75+
76+
@Test
77+
public void testMakeBasicURIWithPathAndParams() {
78+
URI basicURI = UriUtils.makeBasicURI("http", "host.com", 80, "/target", "id=123", "q=fresh & clean");
79+
assertEquals(basicURI.getScheme(), "http", "Scheme mismatch");
80+
assertEquals(basicURI.getUserInfo(), null, "User info mismatch");
81+
assertEquals(basicURI.getHost(), "host.com", "Host mismatch");
82+
assertEquals(basicURI.getPort(), 80, "Post mismatch");
83+
assertEquals(basicURI.getPath(), "/target", "Path mismatch");
84+
assertEquals(basicURI.getQuery(), "id=123&q=fresh+%26+clean", "Query mismatch");
85+
assertEquals(basicURI.getFragment(), null, "Fragment mismatch");
86+
}
3787
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /