|
10 | 10 | import org.junit.jupiter.api.extension.ExtendWith; |
11 | 11 | import org.mockito.Mock; |
12 | 12 | import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | +import org.prebid.server.exception.PreBidException; |
13 | 14 | import org.prebid.server.model.CaseInsensitiveMultiMap; |
14 | 15 | import org.prebid.server.model.HttpRequestContext; |
15 | 16 |
|
|
18 | 19 |
|
19 | 20 | import static java.util.Collections.singletonMap; |
20 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
21 | 23 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
22 | 24 | import static org.assertj.core.api.Assertions.entry; |
23 | 25 | import static org.assertj.core.api.Assertions.tuple; |
@@ -62,6 +64,75 @@ public void validateUrlShouldReturnExpectedUrl() { |
62 | 64 | assertThat(url).isEqualTo("http://domain.org/query-string?a=1"); |
63 | 65 | } |
64 | 66 |
|
| 67 | + @Test |
| 68 | + public void validateDomainNameShouldReturnExpectedDomainName() { |
| 69 | + // when and then |
| 70 | + assertThat(HttpUtil.validateDomainName("example.com")).isEqualTo("example.com"); |
| 71 | + assertThat(HttpUtil.validateDomainName("sub.domain-example.com")).isEqualTo("sub.domain-example.com"); |
| 72 | + assertThat(HttpUtil.validateDomainName("127.0.0.1")).isEqualTo("127.0.0.1"); |
| 73 | + assertThat(HttpUtil.validateDomainName("example.com:8080")).isEqualTo("example.com:8080"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void validateDomainNameShouldFailOnInvalidCharacters() { |
| 78 | + // when and then |
| 79 | + assertThatExceptionOfType(PreBidException.class) |
| 80 | + .isThrownBy(() -> HttpUtil.validateDomainName("example.com/path")) |
| 81 | + .withMessage("Domain name example.com/path contains invalid characters"); |
| 82 | + |
| 83 | + assertThatExceptionOfType(PreBidException.class) |
| 84 | + .isThrownBy(() -> HttpUtil.validateDomainName("example@com")) |
| 85 | + .withMessage("Domain name example@com contains invalid characters"); |
| 86 | + |
| 87 | + assertThatExceptionOfType(PreBidException.class) |
| 88 | + .isThrownBy(() -> HttpUtil.validateDomainName("example.com:port")) |
| 89 | + .withMessage("Domain name example.com:port contains invalid characters"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void validatePathSegmentShouldReturnExpectedPathSegment() { |
| 94 | + // when and then |
| 95 | + assertThat(HttpUtil.validatePathSegment("path")).isEqualTo("path"); |
| 96 | + assertThat(HttpUtil.validatePathSegment("path/to/resource")).isEqualTo("path/to/resource"); |
| 97 | + assertThat(HttpUtil.validatePathSegment(".")).isEqualTo("."); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void validatePathSegmentShouldFailOnForbiddenCharacter() { |
| 102 | + // when and then |
| 103 | + assertThatExceptionOfType(PreBidException.class) |
| 104 | + .isThrownBy(() -> HttpUtil.validatePathSegment("path?query")) |
| 105 | + .withMessage("Path segment path?query contains forbidden character ?"); |
| 106 | + |
| 107 | + assertThatExceptionOfType(PreBidException.class) |
| 108 | + .isThrownBy(() -> HttpUtil.validatePathSegment("path#fragment")) |
| 109 | + .withMessage("Path segment path#fragment contains forbidden character #"); |
| 110 | + |
| 111 | + assertThatExceptionOfType(PreBidException.class) |
| 112 | + .isThrownBy(() -> HttpUtil.validatePathSegment("path\\segment")) |
| 113 | + .withMessage("Path segment path\\segment contains forbidden character \\"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void validatePathSegmentShouldFailOnDoubleSlash() { |
| 118 | + // when and then |
| 119 | + assertThatExceptionOfType(PreBidException.class) |
| 120 | + .isThrownBy(() -> HttpUtil.validatePathSegment("path//segment")) |
| 121 | + .withMessage("Path segment path//segment contains forbidden sequence //"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void validatePathSegmentShouldFailOnSegmentContainingTwoDots() { |
| 126 | + // when and then |
| 127 | + assertThatExceptionOfType(PreBidException.class) |
| 128 | + .isThrownBy(() -> HttpUtil.validatePathSegment("path/../segment")) |
| 129 | + .withMessage("Path segment path/../segment contains forbidden segment .."); |
| 130 | + |
| 131 | + assertThatExceptionOfType(PreBidException.class) |
| 132 | + .isThrownBy(() -> HttpUtil.validatePathSegment("..")) |
| 133 | + .withMessage("Path segment .. contains forbidden segment .."); |
| 134 | + } |
| 135 | + |
65 | 136 | @Test |
66 | 137 | public void encodeUrlShouldReturnExpectedValue() { |
67 | 138 | // when |
|
0 commit comments