|
| 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¶m2=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¶m1=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