| 187 | } |
| 188 | |
| 189 | func TestProxyAuthorizationDial(t *testing.T) { |
| 190 | s := newServer(t) |
| 191 | defer s.Close() |
| 192 | |
| 193 | surl, _ := url.Parse(s.Server.URL) |
| 194 | surl.User = url.UserPassword("username", "password") |
| 195 | |
| 196 | cstDialer := cstDialer // make local copy for modification on next line. |
| 197 | cstDialer.Proxy = http.ProxyURL(surl) |
| 198 | |
| 199 | connect := false |
| 200 | origHandler := s.Server.Config.Handler |
| 201 | |
| 202 | // Capture the request Host header. |
| 203 | s.Server.Config.Handler = http.HandlerFunc( |
| 204 | func(w http.ResponseWriter, r *http.Request) { |
| 205 | proxyAuth := r.Header.Get("Proxy-Authorization") |
| 206 | expectedProxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password")) |
| 207 | if r.Method == http.MethodConnect && proxyAuth == expectedProxyAuth { |
| 208 | connect = true |
| 209 | w.WriteHeader(http.StatusOK) |
| 210 | return |
| 211 | } |
| 212 | |
| 213 | if !connect { |
| 214 | t.Log("connect with proxy authorization not received") |
| 215 | http.Error(w, "connect with proxy authorization not received", http.StatusMethodNotAllowed) |
| 216 | return |
| 217 | } |
| 218 | origHandler.ServeHTTP(w, r) |
| 219 | }) |
| 220 | |
| 221 | ws, _, err := cstDialer.Dial(s.URL, nil) |
| 222 | if err != nil { |
| 223 | t.Fatalf("Dial: %v", err) |
| 224 | } |
| 225 | defer ws.Close() |
| 226 | sendRecv(t, ws) |
| 227 | } |
| 228 | |
| 229 | func TestDial(t *testing.T) { |
| 230 | s := newServer(t) |