зеркало из https://github.com/microsoft/moc.git
Merge pull request #222 from djshah19/user/dhwanishah/move-proxy-validations
moving proxy validation function here
This commit is contained in:
Коммит
ff28e1168c
|
@ -11,6 +11,7 @@ import (
|
|||
"net/url"
|
||||
|
||||
"github.com/microsoft/moc/pkg/errors"
|
||||
commonproto "github.com/microsoft/moc/rpc/common"
|
||||
)
|
||||
|
||||
func ValidateProxyURL(proxyURL string) (*url.URL, error) {
|
||||
|
@ -61,3 +62,25 @@ func TestProxyUrlConnection(parsedURL *url.URL, certContent string, getRequestUr
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateProxyParameters(proxyConfig *commonproto.ProxyConfiguration) error {
|
||||
if proxyConfig == nil {
|
||||
return nil
|
||||
}
|
||||
// Validations for proxy parameters
|
||||
if len(proxyConfig.HttpProxy) > 0 {
|
||||
_, err := ValidateProxyURL(proxyConfig.HttpProxy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(proxyConfig.HttpsProxy) > 0 {
|
||||
_, err := ValidateProxyURL(proxyConfig.HttpsProxy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/microsoft/moc/pkg/certs"
|
||||
commonproto "github.com/microsoft/moc/rpc/common"
|
||||
)
|
||||
|
||||
func Test_ValidateProxyURL(t *testing.T) {
|
||||
|
@ -19,7 +20,7 @@ func Test_ValidateProxyURL(t *testing.T) {
|
|||
}
|
||||
|
||||
// Invalid proxy url
|
||||
_, err = ValidateProxyURL("//akse2e:akse2e@skyproxy.ceccloud1.selfhost.corp.microsoft.com:3128")
|
||||
_, err = ValidateProxyURL("w3proxy.netscape.com:3128")
|
||||
expectedResult = "Invalid proxy URL. The URL scheme should be http or https: Invalid Input"
|
||||
if err.Error() != expectedResult {
|
||||
t.Fatalf("Test_ValidateProxyURL test case failed. Expected error %s but got %s", expectedResult, err.Error())
|
||||
|
@ -34,10 +35,10 @@ func Test_TestProxyUrlConnection(t *testing.T) {
|
|||
certBytes := certs.EncodeCertPEM(caCert)
|
||||
caCertString := string(certBytes)
|
||||
|
||||
parsedUrl, _ := ValidateProxyURL("http://akse2e:akse2e@.ceccloud1.selfhost.corp.microsoft.com:3128")
|
||||
parsedUrl, _ := ValidateProxyURL("http://w3proxy.netscape.com:3128")
|
||||
// Invalid hostname
|
||||
err = TestProxyUrlConnection(parsedUrl, caCertString, "")
|
||||
expectedResult := "Get \"https://mcr.microsoft.com\": proxyconnect tcp: dial tcp: lookup .ceccloud1.selfhost.corp.microsoft.com: no such host: Invalid Input"
|
||||
expectedResult := "Get \"https://mcr.microsoft.com\": proxyconnect tcp: dial tcp: lookup w3proxy.netscape.com: no such host: Invalid Input"
|
||||
if err.Error() != expectedResult {
|
||||
t.Fatalf("Test_TestProxyUrlConnection test case failed. Expected error %s but got %s", expectedResult, err.Error())
|
||||
}
|
||||
|
@ -52,6 +53,28 @@ func Test_TestProxyUrlConnection(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_ValidateProxyParameters(t *testing.T) {
|
||||
config := commonproto.ProxyConfiguration{}
|
||||
proxy := NewProxy()
|
||||
defer proxy.Target.Close()
|
||||
config.HttpProxy = proxy.Target.URL
|
||||
config.HttpsProxy = proxy.Target.URL
|
||||
|
||||
// valid case
|
||||
err := ValidateProxyParameters(&config)
|
||||
if err != nil {
|
||||
t.Fatalf("Test_ValidateProxyParameters test case failed. %s", err.Error())
|
||||
}
|
||||
|
||||
// invalid case - invalid url
|
||||
config.HttpProxy = "w3proxy.netscape.com:3128"
|
||||
err = ValidateProxyParameters(&config)
|
||||
expectedResult := "Invalid proxy URL. The URL scheme should be http or https: Invalid Input"
|
||||
if err.Error() != expectedResult {
|
||||
t.Fatalf("Test_ValidateProxyParameters test case failed. Expected error %s but got %s", expectedResult, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Proxy is a simple proxy server for unit tests.
|
||||
type Proxy struct {
|
||||
Target *httptest.Server
|
||||
|
|
Загрузка…
Ссылка в новой задаче