github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/web_test.go (about) 1 package validation 2 3 import ( 4 "testing" 5 ) 6 7 func TestValidationIsURLWithHTTPS(t *testing.T) { 8 cases := map[string]struct { 9 Value interface{} 10 Error bool 11 }{ 12 "NotString": { 13 Value: 7, 14 Error: true, 15 }, 16 "Empty": { 17 Value: "", 18 Error: true, 19 }, 20 "NotUrl": { 21 Value: "this is not a url", 22 Error: true, 23 }, 24 "BareUrl": { 25 Value: "www.example.com", 26 Error: true, 27 }, 28 "FtpUrl": { 29 Value: "ftp://www.example.com", 30 Error: true, 31 }, 32 "HttpUrl": { 33 Value: "http://www.example.com", 34 Error: true, 35 }, 36 "HttpsUrl": { 37 Value: "https://www.example.com", 38 Error: false, 39 }, 40 } 41 42 for tn, tc := range cases { 43 t.Run(tn, func(t *testing.T) { 44 _, errors := IsURLWithHTTPS(tc.Value, tn) 45 46 if len(errors) > 0 && !tc.Error { 47 t.Errorf("IsURLWithHTTPS(%s) produced an unexpected error", tc.Value) 48 } else if len(errors) == 0 && tc.Error { 49 t.Errorf("IsURLWithHTTPS(%s) did not error", tc.Value) 50 } 51 }) 52 } 53 } 54 55 func TestValidationIsURLWithHTTPorHTTPS(t *testing.T) { 56 cases := map[string]struct { 57 Value interface{} 58 Error bool 59 }{ 60 "NotString": { 61 Value: 7, 62 Error: true, 63 }, 64 "Empty": { 65 Value: "", 66 Error: true, 67 }, 68 "NotUrl": { 69 Value: "this is not a url", 70 Error: true, 71 }, 72 "BareUrl": { 73 Value: "www.example.com", 74 Error: true, 75 }, 76 "FtpUrl": { 77 Value: "ftp://www.example.com", 78 Error: true, 79 }, 80 "HttpUrl": { 81 Value: "http://www.example.com", 82 Error: false, 83 }, 84 "HttpsUrl": { 85 Value: "https://www.example.com", 86 Error: false, 87 }, 88 } 89 90 for tn, tc := range cases { 91 t.Run(tn, func(t *testing.T) { 92 _, errors := IsURLWithHTTPorHTTPS(tc.Value, tn) 93 94 if len(errors) > 0 && !tc.Error { 95 t.Errorf("IsURLWithHTTPorHTTPS(%s) produced an unexpected error", tc.Value) 96 } else if len(errors) == 0 && tc.Error { 97 t.Errorf("IsURLWithHTTPorHTTPS(%s) did not error", tc.Value) 98 } 99 }) 100 } 101 }