Skip to content

Commit 6f3f3d0

Browse files
authored
Merge pull request #19 from exceptionless/testing
Added unit tests for utils package
2 parents 04721d0 + 957eac5 commit 6f3f3d0

File tree

2 files changed

+89
-2
lines changed
  • src
    • main/java/com/exceptionless/exceptionlessclient/utils
    • test/java/com/exceptionless/exceptionlessclient/utils

2 files changed

+89
-2
lines changed

src/main/java/com/exceptionless/exceptionlessclient/utils/Utils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ public static Map<String, String> getCookies(HttpRequest request) {
4444
}
4545

4646
public static Map<String, List<String>> getQueryParams(URI uri) {
47-
Map<String, List<String>> queryParams = new HashMap<>();
47+
String query = uri.getQuery();
48+
if (query == null || query.isBlank()) {
49+
return Map.of();
50+
}
4851

49-
String[] rawQueryParams = uri.getQuery().split("&");
52+
String[] rawQueryParams = query.split("&");
53+
Map<String, List<String>> queryParams = new HashMap<>();
5054
for (String queryParamsNameAndValue : rawQueryParams) {
5155
String[] queryParamsNameAndValuePair = queryParamsNameAndValue.split("=");
5256
if (!queryParams.containsKey(queryParamsNameAndValuePair[0])) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.exceptionless.exceptionlessclient.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.net.URI;
6+
import java.net.http.HttpRequest;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class UtilsTest {
13+
@Test
14+
public void itCanSafelyGetANull() {
15+
//noinspection ConstantConditions
16+
assertThat(Utils.safeGetAs(null, String.class)).isNull();
17+
}
18+
19+
@Test
20+
public void itCanSafelyGetANotCastableValue() {
21+
assertThat(Utils.safeGetAs(1L, String.class)).isNull();
22+
}
23+
24+
@Test
25+
public void itCanSafelyGetAValue() {
26+
assertThat(Utils.safeGetAs("abc", String.class)).isEqualTo("abc");
27+
}
28+
29+
@Test
30+
public void itCanGetEmptyCookiesIfCookieHeaderIsNotPresent() {
31+
assertThat(
32+
Utils.getCookies(
33+
HttpRequest.newBuilder().uri(URI.create("http://localhost:5000")).build()))
34+
.isEmpty();
35+
}
36+
37+
@Test
38+
public void itCanGetCookiesFromTheHeader() {
39+
assertThat(
40+
Utils.getCookies(
41+
HttpRequest.newBuilder()
42+
.uri(URI.create("http://localhost:5000"))
43+
.header("Cookie", "cookie1=value1;cookie2=value2")
44+
.build()))
45+
.isEqualTo(Map.of("cookie1", "value1", "cookie2", "value2"));
46+
}
47+
48+
@Test
49+
public void itCanGetEmptyQueryParams() {
50+
assertThat(Utils.getQueryParams(URI.create("http://localhost:5000"))).isEmpty();
51+
assertThat(Utils.getQueryParams(URI.create("http://localhost:5000?"))).isEmpty();
52+
}
53+
54+
@Test
55+
public void itCanGetQueryParams() {
56+
assertThat(
57+
Utils.getQueryParams(URI.create("http://localhost:5000?param1=value1&param2=value2")))
58+
.isEqualTo(Map.of("param1", List.of("value1"), "param2", List.of("value2")));
59+
}
60+
61+
@Test
62+
public void itCanGetArrayQueryParams() {
63+
assertThat(
64+
Utils.getQueryParams(URI.create("http://localhost:5000?param1=value1&param1=value2")))
65+
.isEqualTo(Map.of("param1", List.of("value1", "value2")));
66+
}
67+
68+
@Test
69+
public void itDontMatchForNullValue() {
70+
assertThat(Utils.match(null, "abc")).isFalse();
71+
}
72+
73+
@Test
74+
public void itDontMatchForNullPattern() {
75+
assertThat(Utils.match("abc", null)).isFalse();
76+
}
77+
78+
@Test
79+
public void itCanMatchAValueToAPattern() {
80+
assertThat(Utils.match("abc", "def")).isFalse();
81+
assertThat(Utils.match("ABC", "abc")).isTrue();
82+
}
83+
}

0 commit comments

Comments
 (0)