Skip to content
This repository was archived by the owner on Oct 30, 2022. It is now read-only.

Commit 8289c50

Browse files
committed
Fix HttpUrl.getPath()
1 parent 90e7102 commit 8289c50

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ hs_err_pid*
2727

2828
# IntelliJ files
2929
.idea/
30+
31+
.DS_Store

src/main/java/com/meeshkan/http/types/HttpUrl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,17 @@ public String getPath() {
9090
StringBuilder result = new StringBuilder();
9191
result.append(pathname);
9292
if (!queryParameters.isEmpty()) {
93-
result.append('?');
93+
boolean first = true;
9494
try {
9595
for (Map.Entry<String, List<String>> entry : queryParameters.entrySet()) {
9696
String encodedName = URLEncoder.encode(entry.getKey(), "utf-8");
9797
for (String value : entry.getValue()) {
98+
if (first) {
99+
first = false;
100+
result.append('?');
101+
} else {
102+
result.append('&');
103+
}
98104
String encodedValue = URLEncoder.encode(value, "utf-8");
99105
result.append(encodedName).append('=').append(encodedValue);
100106
}

src/test/java/com/meeshkan/http/types/HttpExchangeTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.OffsetDateTime;
88
import java.util.Arrays;
99
import java.util.Collections;
10+
import java.util.HashSet;
1011
import java.util.List;
1112
import java.util.function.Supplier;
1213
import java.util.stream.Collectors;
@@ -54,10 +55,10 @@ void loadFromJson() throws Exception {
5455

5556
assertEquals("/user/repos", exchange.getRequest().getUrl().getPathname());
5657
String path = exchange.getRequest().getUrl().getPath();
57-
assertTrue(path.startsWith("/user/repos?"));
58-
assertTrue(path.contains("mykey=myvalue"));
59-
assertTrue(path.contains("anotherkey=value1"));
60-
assertTrue(path.contains("anotherkey=value2"));
58+
String queryString = path.substring(path.indexOf('?') + 1);
59+
String[] queryParts = queryString.split("&");
60+
assertEquals(3, queryParts.length);
61+
assertEquals(new HashSet<>(Arrays.asList("mykey=myvalue", "anotherkey=value1", "anotherkey=value2")), new HashSet<>(Arrays.asList(queryParts)));
6162
assertNull(exchange.getRequest().getTimestamp());
6263
assertEquals("myvalue", exchange.getRequest().getUrl().getFirstQueryParameter("mykey"));
6364
assertEquals(Collections.singletonList("myvalue"), exchange.getRequest().getUrl().getAllQueryParameters("mykey"));
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"request": {
3-
"protocol": "http",
3+
"timestamp": "2018-11-13T20:20:39+02:00",
44
"method": "get",
5+
"protocol": "http",
6+
"host": "example.com",
57
"headers": {
68
"accept": "*/*",
79
"user-agent": "Mozilla/5.0 (pc-x86_64-linux-gnu) Siege/3.0.8"
810
},
9-
"timestamp": "2018-11-13T20:20:39+02:00",
1011
"path": "/user/repos?mykey=myvalue&anotherkey=value1&anotherkey=value2",
11-
"host": "example.com"
1212
},
1313
"response": {
14-
"statusCode": 200,
15-
"body": "...",
1614
"timestamp": "2019-11-13T20:20:39+02:00",
15+
"statusCode": 200,
1716
"headers": {
1817
"content-length": "1999",
1918
"content-type": "text/html; charset=utf-8"
20-
}
19+
},
20+
"body": "..."
2121
}
2222
}

0 commit comments

Comments
 (0)