зеркало из https://github.com/microsoft/moc.git
adding validation for proxy url
This commit is contained in:
Родитель
baabc91ed2
Коммит
d9a784671b
|
@ -0,0 +1,44 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the Apache v2.0 license.
|
||||
|
||||
package validations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"github.com/microsoft/moc/pkg/errors"
|
||||
)
|
||||
|
||||
func ValidateProxyURL(proxyURL string) error {
|
||||
parsedURL, err := url.Parse(proxyURL)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrapf(errors.InvalidInput, err)
|
||||
}
|
||||
|
||||
if parsedURL.Scheme != "http"{
|
||||
return errors.Wrapf(errors.InvalidInput, "Invalid proxy URL. The URL scheme should be http")
|
||||
}
|
||||
|
||||
// Create a transport
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyURL(parsedURL),
|
||||
}
|
||||
|
||||
// Create a client
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
// Test the HTTP GET request
|
||||
response, err := client.Get("http://bing.com")
|
||||
if err != nil {
|
||||
return errors.Wrapf(errors.InvalidInput, err)
|
||||
} else {
|
||||
defer response.Body.Close()
|
||||
fmt.Println("Connected successfully to the proxy server")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_ValidateProxyURL(t *testing.T){
|
||||
err := ValidateProxyURL("http://akse2e:akse2e@skyproxy.ceccloud1.selfhost.corp.microsoft.com:3128")
|
||||
|
||||
if(err!=nil){
|
||||
t.Fatalf("ValidateProxyURL returned error")
|
||||
}
|
||||
|
||||
err = ValidateProxyURL("https://akse2e:akse2e@skyproxy.ceccloud1.selfhost.corp.microsoft.com:3128")
|
||||
|
||||
if err != "Invalid proxy URL. The URL scheme should be http"{
|
||||
t.Fatalf("Didnt receive the expected error 'Invalid proxy URL. The URL scheme should be http'")
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче