github.com/alibabacloud-go/tea@v1.3.10/dara/url_test.go (about) 1 package dara 2 3 import ( 4 "testing" 5 ) 6 7 func TestNewURL(t *testing.T) { 8 tests := []struct { 9 urlString string 10 wantErr bool 11 }{ 12 {"http://example.com", false}, 13 {"ftp://user:pass@host:21/path", false}, 14 {"://example.com", true}, // Invalid URL 15 } 16 17 for _, tt := range tests { 18 _, err := NewURL(tt.urlString) 19 if (err != nil) != tt.wantErr { 20 t.Errorf("NewURL(%q) error = %v, wantErr %v", tt.urlString, err, tt.wantErr) 21 } 22 } 23 } 24 25 func TestDaraURL_Path(t *testing.T) { 26 tests := []struct { 27 urlString string 28 want string 29 }{ 30 {"http://example.com/path?query=1", "/path?query=1"}, 31 {"https://example.com/", "/"}, 32 } 33 34 for _, tt := range tests { 35 tu, _ := NewURL(tt.urlString) 36 if got := tu.Path(); got != tt.want { 37 t.Errorf("DaraURL.Path() = %v, want %v", got, tt.want) 38 } 39 } 40 } 41 42 func TestDaraURL_Pathname(t *testing.T) { 43 tests := []struct { 44 urlString string 45 want string 46 }{ 47 {"http://example.com/path?query=1", "/path"}, 48 {"https://example.com/another/path/", "/another/path/"}, 49 } 50 51 for _, tt := range tests { 52 tu, _ := NewURL(tt.urlString) 53 if got := tu.Pathname(); got != tt.want { 54 t.Errorf("DaraURL.Pathname() = %v, want %v", got, tt.want) 55 } 56 } 57 } 58 59 func TestDaraURL_Protocol(t *testing.T) { 60 tests := []struct { 61 urlString string 62 want string 63 }{ 64 {"http://example.com", "http"}, 65 {"ftp://example.com", "ftp"}, 66 } 67 68 for _, tt := range tests { 69 tu, _ := NewURL(tt.urlString) 70 if got := tu.Protocol(); got != tt.want { 71 t.Errorf("DaraURL.Protocol() = %v, want %v", got, tt.want) 72 } 73 } 74 } 75 76 func TestDaraURL_Hostname(t *testing.T) { 77 tests := []struct { 78 urlString string 79 want string 80 }{ 81 {"http://example.com", "example.com"}, 82 {"https://user@subdomain.example.com:443", "subdomain.example.com"}, 83 } 84 85 for _, tt := range tests { 86 tu, _ := NewURL(tt.urlString) 87 if got := tu.Hostname(); got != tt.want { 88 t.Errorf("DaraURL.Hostname() = %v, want %v", got, tt.want) 89 } 90 } 91 } 92 93 func TestDaraURL_Host(t *testing.T) { 94 tests := []struct { 95 urlString string 96 want string 97 }{ 98 {"http://example.com", "example.com"}, 99 {"http://example.com:8080", "example.com:8080"}, 100 } 101 102 for _, tt := range tests { 103 tu, _ := NewURL(tt.urlString) 104 if got := tu.Host(); got != tt.want { 105 t.Errorf("DaraURL.Host() = %v, want %v", got, tt.want) 106 } 107 } 108 } 109 110 func TestDaraURL_Port(t *testing.T) { 111 tests := []struct { 112 urlString string 113 want string 114 }{ 115 {"http://example.com", "80"}, 116 {"https://example.com", "443"}, 117 {"ftp://example.com:21", "21"}, 118 {"gopher://example.com", "70"}, 119 {"ws://example.com", "80"}, 120 {"wss://example.com", "443"}, 121 {"http://example.com:8080", "8080"}, 122 } 123 124 for _, tt := range tests { 125 tu, _ := NewURL(tt.urlString) 126 if got := tu.Port(); got != tt.want { 127 t.Errorf("DaraURL.Port() = %v, want %v", got, tt.want) 128 } 129 } 130 } 131 132 func TestDaraURL_Hash(t *testing.T) { 133 tests := []struct { 134 urlString string 135 want string 136 }{ 137 {"http://example.com#section", "section"}, 138 {"http://example.com", ""}, 139 } 140 141 for _, tt := range tests { 142 tu, _ := NewURL(tt.urlString) 143 if got := tu.Hash(); got != tt.want { 144 t.Errorf("DaraURL.Hash() = %v, want %v", got, tt.want) 145 } 146 } 147 } 148 149 func TestDaraURL_Search(t *testing.T) { 150 tests := []struct { 151 urlString string 152 want string 153 }{ 154 {"http://example.com?query=1", "query=1"}, 155 {"http://example.com", ""}, 156 } 157 158 for _, tt := range tests { 159 tu, _ := NewURL(tt.urlString) 160 if got := tu.Search(); got != tt.want { 161 t.Errorf("DaraURL.Search() = %v, want %v", got, tt.want) 162 } 163 } 164 } 165 166 func TestDaraURL_Href(t *testing.T) { 167 tests := []struct { 168 urlString string 169 want string 170 }{ 171 {"http://example.com", "http://example.com"}, 172 {"https://user:pass@host:443/path?query=1#section", "https://user:pass@host:443/path?query=1#section"}, 173 } 174 175 for _, tt := range tests { 176 tu, _ := NewURL(tt.urlString) 177 if got := tu.Href(); got != tt.want { 178 t.Errorf("DaraURL.Href() = %v, want %v", got, tt.want) 179 } 180 } 181 } 182 183 func TestDaraURL_Auth(t *testing.T) { 184 tests := []struct { 185 urlString string 186 want string 187 }{ 188 {"http://user:pass@example.com", "user:pass"}, 189 {"http://user@example.com", "user:"}, 190 {"http://example.com", ""}, 191 } 192 193 for _, tt := range tests { 194 tu, _ := NewURL(tt.urlString) 195 if got := tu.Auth(); got != tt.want { 196 t.Errorf("DaraURL.Auth() = %v, want %v", got, tt.want) 197 } 198 } 199 } 200 201 func TestEncodeURL(t *testing.T) { 202 tests := []struct { 203 urlString string 204 want string 205 }{ 206 {"hello world", "hello%20world"}, 207 {"test*abcd++", "test%2Aabcd%2B%2B"}, 208 {"", ""}, 209 } 210 211 for _, tt := range tests { 212 got := EncodeURL(tt.urlString) 213 if got != tt.want { 214 t.Errorf("EncodeURL(%q) = %v, want %v", tt.urlString, got, tt.want) 215 } 216 } 217 } 218 219 func TestPercentEncode(t *testing.T) { 220 tests := []struct { 221 raw string 222 want string 223 }{ 224 {"hello world", "hello%20world"}, 225 {"hello*world", "hello%2Aworld"}, 226 {"hello~world", "hello~world"}, 227 {"", ""}, 228 } 229 230 for _, tt := range tests { 231 got := PercentEncode(tt.raw) 232 if got != tt.want { 233 t.Errorf("PercentEncode(%q) = %v, want %v", tt.raw, got, tt.want) 234 } 235 } 236 } 237 238 func TestPathEncode(t *testing.T) { 239 tests := []struct { 240 path string 241 want string 242 }{ 243 {"hello/world", "hello/world"}, 244 {"hello world", "hello%20world"}, 245 {"", ""}, 246 {"/", "/"}, 247 } 248 249 for _, tt := range tests { 250 got := PathEncode(tt.path) 251 if got != tt.want { 252 t.Errorf("PathEncode(%q) = %v, want %v", tt.path, got, tt.want) 253 } 254 } 255 }