|
| 1 | +/* |
| 2 | + * ApplicationInsights-Java |
| 3 | + * Copyright (c) Microsoft Corporation |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * MIT License |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 8 | + * software and associated documentation files (the ""Software""), to deal in the Software |
| 9 | + * without restriction, including without limitation the rights to use, copy, modify, merge, |
| 10 | + * publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 11 | + * persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 13 | + * substantial portions of the Software. |
| 14 | + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 17 | + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | + * DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.microsoft.applicationinsights.agent.internal.exporter; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +public class UrlParserTargetTest { |
| 29 | + |
| 30 | + // there are more test cases here than needed, but they are just copied from UrlParserPathTest |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testGetTargetFromUrl() { |
| 34 | + assertThat(UrlParser.getTargetFromUrl("https://localhost")).isEqualTo("localhost"); |
| 35 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/")).isEqualTo("localhost"); |
| 36 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path")).isEqualTo("localhost"); |
| 37 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path/")).isEqualTo("localhost"); |
| 38 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path")).isEqualTo("localhost"); |
| 39 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/")).isEqualTo("localhost"); |
| 40 | + |
| 41 | + assertThat(UrlParser.getTargetFromUrl("https://localhost?")).isEqualTo("localhost"); |
| 42 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/?")).isEqualTo("localhost"); |
| 43 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path?")).isEqualTo("localhost"); |
| 44 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path/?")).isEqualTo("localhost"); |
| 45 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path?")).isEqualTo("localhost"); |
| 46 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/?")).isEqualTo("localhost"); |
| 47 | + |
| 48 | + assertThat(UrlParser.getTargetFromUrl("https://localhost?query")).isEqualTo("localhost"); |
| 49 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/?query")).isEqualTo("localhost"); |
| 50 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path?query")).isEqualTo("localhost"); |
| 51 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path/?query")).isEqualTo("localhost"); |
| 52 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path?query")) |
| 53 | + .isEqualTo("localhost"); |
| 54 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/?query")) |
| 55 | + .isEqualTo("localhost"); |
| 56 | + |
| 57 | + assertThat(UrlParser.getTargetFromUrl("https://localhost#")).isEqualTo("localhost"); |
| 58 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/#")).isEqualTo("localhost"); |
| 59 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path#")).isEqualTo("localhost"); |
| 60 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path/#")).isEqualTo("localhost"); |
| 61 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path#")).isEqualTo("localhost"); |
| 62 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/#")).isEqualTo("localhost"); |
| 63 | + |
| 64 | + assertThat(UrlParser.getTargetFromUrl("https://localhost#fragment")).isEqualTo("localhost"); |
| 65 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/#fragment")).isEqualTo("localhost"); |
| 66 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path#fragment")) |
| 67 | + .isEqualTo("localhost"); |
| 68 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/path/#fragment")) |
| 69 | + .isEqualTo("localhost"); |
| 70 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path#fragment")) |
| 71 | + .isEqualTo("localhost"); |
| 72 | + assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/#fragment")) |
| 73 | + .isEqualTo("localhost"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetTargetFromUrlWithPort() { |
| 78 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080")).isEqualTo("localhost:8080"); |
| 79 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/")).isEqualTo("localhost:8080"); |
| 80 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path")) |
| 81 | + .isEqualTo("localhost:8080"); |
| 82 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/")) |
| 83 | + .isEqualTo("localhost:8080"); |
| 84 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path")) |
| 85 | + .isEqualTo("localhost:8080"); |
| 86 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/")) |
| 87 | + .isEqualTo("localhost:8080"); |
| 88 | + |
| 89 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080?")).isEqualTo("localhost:8080"); |
| 90 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/?")).isEqualTo("localhost:8080"); |
| 91 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path?")) |
| 92 | + .isEqualTo("localhost:8080"); |
| 93 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/?")) |
| 94 | + .isEqualTo("localhost:8080"); |
| 95 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path?")) |
| 96 | + .isEqualTo("localhost:8080"); |
| 97 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/?")) |
| 98 | + .isEqualTo("localhost:8080"); |
| 99 | + |
| 100 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080?query")) |
| 101 | + .isEqualTo("localhost:8080"); |
| 102 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/?query")) |
| 103 | + .isEqualTo("localhost:8080"); |
| 104 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path?query")) |
| 105 | + .isEqualTo("localhost:8080"); |
| 106 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/?query")) |
| 107 | + .isEqualTo("localhost:8080"); |
| 108 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path?query")) |
| 109 | + .isEqualTo("localhost:8080"); |
| 110 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/?query")) |
| 111 | + .isEqualTo("localhost:8080"); |
| 112 | + |
| 113 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080#")).isEqualTo("localhost:8080"); |
| 114 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/#")).isEqualTo("localhost:8080"); |
| 115 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path#")) |
| 116 | + .isEqualTo("localhost:8080"); |
| 117 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/#")) |
| 118 | + .isEqualTo("localhost:8080"); |
| 119 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path#")) |
| 120 | + .isEqualTo("localhost:8080"); |
| 121 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/#")) |
| 122 | + .isEqualTo("localhost:8080"); |
| 123 | + |
| 124 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080#fragment")) |
| 125 | + .isEqualTo("localhost:8080"); |
| 126 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/#fragment")) |
| 127 | + .isEqualTo("localhost:8080"); |
| 128 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path#fragment")) |
| 129 | + .isEqualTo("localhost:8080"); |
| 130 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/#fragment")) |
| 131 | + .isEqualTo("localhost:8080"); |
| 132 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path#fragment")) |
| 133 | + .isEqualTo("localhost:8080"); |
| 134 | + assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/#fragment")) |
| 135 | + .isEqualTo("localhost:8080"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testGetTargetFromUrlWithNoAuthority() { |
| 140 | + assertThat(UrlParser.getTargetFromUrl("https:")).isNull(); |
| 141 | + assertThat(UrlParser.getTargetFromUrl("https:/")).isNull(); |
| 142 | + assertThat(UrlParser.getTargetFromUrl("https:/path")).isNull(); |
| 143 | + assertThat(UrlParser.getTargetFromUrl("https:/path/")).isNull(); |
| 144 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path")).isNull(); |
| 145 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path/")).isNull(); |
| 146 | + |
| 147 | + assertThat(UrlParser.getTargetFromUrl("https:?")).isNull(); |
| 148 | + assertThat(UrlParser.getTargetFromUrl("https:/?")).isNull(); |
| 149 | + assertThat(UrlParser.getTargetFromUrl("https:/path?")).isNull(); |
| 150 | + assertThat(UrlParser.getTargetFromUrl("https:/path/?")).isNull(); |
| 151 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path?")).isNull(); |
| 152 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path/?")).isNull(); |
| 153 | + |
| 154 | + assertThat(UrlParser.getTargetFromUrl("https:?query")).isNull(); |
| 155 | + assertThat(UrlParser.getTargetFromUrl("https:/?query")).isNull(); |
| 156 | + assertThat(UrlParser.getTargetFromUrl("https:/path?query")).isNull(); |
| 157 | + assertThat(UrlParser.getTargetFromUrl("https:/path/?query")).isNull(); |
| 158 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path?query")).isNull(); |
| 159 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path/?query")).isNull(); |
| 160 | + |
| 161 | + assertThat(UrlParser.getTargetFromUrl("https:#")).isNull(); |
| 162 | + assertThat(UrlParser.getTargetFromUrl("https:/#")).isNull(); |
| 163 | + assertThat(UrlParser.getTargetFromUrl("https:/path#")).isNull(); |
| 164 | + assertThat(UrlParser.getTargetFromUrl("https:/path/#")).isNull(); |
| 165 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path#")).isNull(); |
| 166 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path/#")).isNull(); |
| 167 | + |
| 168 | + assertThat(UrlParser.getTargetFromUrl("https:#fragment")).isNull(); |
| 169 | + assertThat(UrlParser.getTargetFromUrl("https:/#fragment")).isNull(); |
| 170 | + assertThat(UrlParser.getTargetFromUrl("https:/path#fragment")).isNull(); |
| 171 | + assertThat(UrlParser.getTargetFromUrl("https:/path/#fragment")).isNull(); |
| 172 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path#fragment")).isNull(); |
| 173 | + assertThat(UrlParser.getTargetFromUrl("https:/more/path/#fragment")).isNull(); |
| 174 | + } |
| 175 | +} |
0 commit comments