github.com/microsoft/moc@v0.17.1/pkg/validations/proxy_validation_test.go (about) 1 // Copyright (c) Microsoft Corporation. All rights reserved. 2 // Licensed under the Apache v2.0 license. 3 package validations 4 5 import ( 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/microsoft/moc/pkg/certs" 11 commonproto "github.com/microsoft/moc/rpc/common" 12 ) 13 14 func Test_ValidateProxyURL(t *testing.T) { 15 // Empty proxy url 16 _, err := ValidateProxyURL("") 17 expectedResult := "parse \"\": empty url: Invalid Input" 18 if err.Error() != expectedResult { 19 t.Fatalf("Test_ValidateProxyURL test case failed. Expected error %s but got %s", expectedResult, err.Error()) 20 } 21 22 // Invalid proxy url 23 _, err = ValidateProxyURL("w3proxy.netscape.com:3128") 24 expectedResult = "Invalid proxy URL. The URL scheme should be http or https: Invalid Input" 25 if err.Error() != expectedResult { 26 t.Fatalf("Test_ValidateProxyURL test case failed. Expected error %s but got %s", expectedResult, err.Error()) 27 } 28 } 29 30 func Test_TestProxyUrlConnection(t *testing.T) { 31 caCert, _, err := certs.GenerateClientCertificate("ValidCertificate") 32 if err != nil { 33 t.Fatalf(err.Error()) 34 } 35 certBytes := certs.EncodeCertPEM(caCert) 36 caCertString := string(certBytes) 37 38 parsedUrl, _ := ValidateProxyURL("http://w3proxy.netscape.com:3128") 39 // Invalid hostname 40 err = TestProxyUrlConnection(parsedUrl, caCertString, "") 41 expectedResult := "Get \"https://mcr.microsoft.com\": proxyconnect tcp: dial tcp: lookup w3proxy.netscape.com: no such host: Invalid Input" 42 43 if err.Error() != expectedResult { 44 t.Fatalf("Test_TestProxyUrlConnection test case failed. Expected error %s but got %s", expectedResult, err.Error()) 45 } 46 47 // Valid case 48 proxy := NewProxy() 49 defer proxy.Target.Close() 50 parsedUrl, _ = ValidateProxyURL(proxy.Target.URL) 51 err = TestProxyUrlConnection(parsedUrl, "", "http://www.bing.com") 52 if err != nil { 53 t.Fatalf("Test_TestProxyUrlConnection test case failed. %s", err.Error()) 54 } 55 } 56 57 func Test_ValidateProxyParameters(t *testing.T) { 58 config := commonproto.ProxyConfiguration{} 59 proxy := NewProxy() 60 defer proxy.Target.Close() 61 config.HttpProxy = proxy.Target.URL 62 config.HttpsProxy = proxy.Target.URL 63 64 // valid case 65 err := ValidateProxyParameters(&config) 66 if err != nil { 67 t.Fatalf("Test_ValidateProxyParameters test case failed. %s", err.Error()) 68 } 69 70 // invalid case - invalid url 71 config.HttpProxy = "w3proxy.netscape.com:3128" 72 err = ValidateProxyParameters(&config) 73 expectedResult := "Invalid proxy URL. The URL scheme should be http or https: Invalid Input" 74 if err.Error() != expectedResult { 75 t.Fatalf("Test_ValidateProxyParameters test case failed. Expected error %s but got %s", expectedResult, err.Error()) 76 } 77 } 78 79 // Proxy is a simple proxy server for unit tests. 80 type Proxy struct { 81 Target *httptest.Server 82 } 83 84 // NewProxy creates a new proxy server for unit tests. 85 func NewProxy() *Proxy { 86 target := httptest.NewServer(http.DefaultServeMux) 87 return &Proxy{Target: target} 88 }