github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/url_test.go (about) 1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package safehttp 16 17 import ( 18 "net/url" 19 "testing" 20 ) 21 22 func TestURLToString(t *testing.T) { 23 want := "http://www.example.com/asdf?fruit=apple" 24 25 netURL, err := url.Parse(want) 26 if err != nil { 27 t.Fatalf(`url.Parse("http://www.example.com/asdf?fruit=apple") got: %v want: nil`, err) 28 } 29 30 u := URL{url: netURL} 31 if got := u.String(); got != want { 32 t.Errorf("u.String() got: %v want: %v", got, want) 33 } 34 } 35 36 func TestURLHost(t *testing.T) { 37 var test = []struct { 38 name string 39 url string 40 want string 41 }{ 42 { 43 name: "Just host", 44 url: "http://www.example.com/asdf?fruit=apple", 45 want: "www.example.com", 46 }, 47 { 48 name: "Host and port", 49 url: "http://www.example.com:1337/asdf?fruit=apple", 50 want: "www.example.com:1337", 51 }, 52 } 53 54 for _, tt := range test { 55 t.Run(tt.name, func(t *testing.T) { 56 netURL, err := url.Parse(tt.url) 57 if err != nil { 58 t.Fatalf("url.Parse(tt.url) got err %v", err) 59 } 60 61 u := URL{url: netURL} 62 if got := u.Host(); got != tt.want { 63 t.Errorf("u.Host() got: %v want: %v", got, tt.want) 64 } 65 }) 66 } 67 } 68 69 func TestURLHostname(t *testing.T) { 70 var test = []struct { 71 name string 72 url string 73 want string 74 }{ 75 { 76 name: "Just host", 77 url: "http://www.example.com/asdf?fruit=apple", 78 want: "www.example.com", 79 }, 80 { 81 name: "Host and port", 82 url: "http://www.example.com:1337/asdf?fruit=apple", 83 want: "www.example.com", 84 }, 85 { 86 name: "Ipv6 and port", 87 url: "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000/asdf?fruit=apple", 88 want: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 89 }, 90 } 91 92 for _, tt := range test { 93 t.Run(tt.name, func(t *testing.T) { 94 netURL, err := url.Parse(tt.url) 95 if err != nil { 96 t.Fatalf("url.Parse(tt.url) got err %v", err) 97 } 98 99 u := URL{url: netURL} 100 if got := u.Hostname(); got != tt.want { 101 t.Errorf("u.Hostname() got: %v want: %v", got, tt.want) 102 } 103 }) 104 } 105 } 106 107 func TestURLPort(t *testing.T) { 108 var test = []struct { 109 name string 110 url string 111 want string 112 }{ 113 { 114 name: "Just host", 115 url: "http://www.example.com/asdf?fruit=apple", 116 want: "", 117 }, 118 { 119 name: "Host and port", 120 url: "http://www.example.com:1337/asdf?fruit=apple", 121 want: "1337", 122 }, 123 { 124 name: "HTTPS", 125 url: "https://www.example.com/asdf?fruit=apple", 126 want: "", 127 }, 128 } 129 130 for _, tt := range test { 131 t.Run(tt.name, func(t *testing.T) { 132 netURL, err := url.Parse(tt.url) 133 if err != nil { 134 t.Fatalf("url.Parse(tt.url) got: %v want: nil", err) 135 } 136 137 u := URL{url: netURL} 138 if got := u.Port(); got != tt.want { 139 t.Errorf("u.Port() got: %v want: %v", got, tt.want) 140 } 141 }) 142 } 143 } 144 145 func TestURLPath(t *testing.T) { 146 netURL, err := url.Parse("http://www.example.com/asdf?fruit=apple") 147 if err != nil { 148 t.Fatalf(`url.Parse("http://www.example.com/asdf?fruit=apple") got: %v want: nil`, err) 149 } 150 151 u := URL{url: netURL} 152 if got, want := u.Path(), "/asdf"; got != want { 153 t.Errorf("u.Path() got: %v want: %v", got, want) 154 } 155 } 156 157 func TestURLQuery(t *testing.T) { 158 var test = []struct { 159 name string 160 url string 161 want string 162 }{ 163 { 164 name: "Normal", 165 url: "http://www.example.com/asdf?fruit=apple", 166 want: "apple", 167 }, 168 { 169 name: "Empty", 170 url: "http://www.example.com/asdf", 171 want: "", 172 }, 173 } 174 175 for _, tt := range test { 176 t.Run(tt.name, func(t *testing.T) { 177 netURL, err := url.Parse(tt.url) 178 if err != nil { 179 t.Fatalf("url.Parse(tt.url) got: %v want: nil", err) 180 } 181 182 u := URL{url: netURL} 183 f, err := u.Query() 184 if err != nil { 185 t.Errorf("u.Query() got: %v want: nil", err) 186 } 187 188 if got := f.String("fruit", ""); got != tt.want { 189 t.Errorf(`f.String("fruit", "") got: %q want: %q`, got, tt.want) 190 } 191 }) 192 } 193 } 194 195 func TestURLInvalidQuery(t *testing.T) { 196 netURL, err := url.Parse("http://www.example.com/asdf?%xx=abc") 197 if err != nil { 198 t.Fatalf(`url.Parse("http://www.example.com/asdf?%%xx=abc") got: %v want: nil`, err) 199 } 200 201 u := URL{url: netURL} 202 if _, err = u.Query(); err == nil { 203 t.Error("u.Query() got: nil want: error") 204 } 205 } 206 207 func TestURLParseURL(t *testing.T) { 208 var tests = []struct { 209 name, url string 210 }{ 211 { 212 name: "Absolute url", 213 url: "http://www.example.com/path?foo=bar#tar", 214 }, 215 { 216 name: "Relative url", 217 url: "example.com/path", 218 }, 219 } 220 221 for _, test := range tests { 222 t.Run(test.name, func(t *testing.T) { 223 url, err := ParseURL(test.url) 224 if url == nil { 225 t.Error("ParseURL(test.url): got nil, want URL") 226 } 227 if err != nil { 228 t.Errorf("ParseURL(test.url): got err %v", err) 229 } 230 if got := url.String(); got != test.url { 231 t.Errorf("url.String(): got %v, want %v", got, test.url) 232 } 233 }) 234 } 235 } 236 237 func TestURLInvalidParseURL(t *testing.T) { 238 url, err := ParseURL("http://www.example.com/path%%%x=0") 239 if url != nil { 240 t.Errorf(`ParseURL: got err %v`, url) 241 } 242 if err == nil { 243 t.Errorf(`ParseURL: got nil, want error`) 244 } 245 }