github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/datastream/connectors_test.go (about) 1 package datastream 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestCustomHTPPSValidation(t *testing.T) { 11 baseConnectorStruct := CustomHTTPSConnector{ 12 DisplayName: "Test Connector", 13 AuthenticationType: AuthenticationTypeNone, 14 Endpoint: "https://example.com", 15 } 16 baseConnectorStruct.SetDestinationType() 17 18 tests := map[string]struct { 19 connectorBuilder func() CustomHTTPSConnector 20 expectValidationError bool 21 }{ 22 "AuthenticationType not in specified set": { 23 connectorBuilder: func() CustomHTTPSConnector { 24 var c = baseConnectorStruct 25 c.AuthenticationType = "NOTEXISTING" 26 return c 27 }, 28 expectValidationError: true, 29 }, 30 "UserName required for auth type BASIC": { 31 connectorBuilder: func() CustomHTTPSConnector { 32 var c = baseConnectorStruct 33 c.AuthenticationType = AuthenticationTypeBasic 34 c.Password = "password" 35 return c 36 }, 37 expectValidationError: true, 38 }, 39 "Password required for auth type BASIC": { 40 connectorBuilder: func() CustomHTTPSConnector { 41 var c = baseConnectorStruct 42 c.AuthenticationType = AuthenticationTypeBasic 43 c.UserName = "username" 44 return c 45 }, 46 expectValidationError: true, 47 }, 48 "UserName and Password not required for type NONE": { 49 connectorBuilder: func() CustomHTTPSConnector { 50 var c = baseConnectorStruct 51 c.AuthenticationType = AuthenticationTypeNone 52 return c 53 }, 54 expectValidationError: false, 55 }, 56 "UserName and Password required for type BASIC": { 57 connectorBuilder: func() CustomHTTPSConnector { 58 var c = baseConnectorStruct 59 c.AuthenticationType = AuthenticationTypeBasic 60 return c 61 }, 62 expectValidationError: true, 63 }, 64 "CustomHeaderName specified without CustomHeaderValue": { 65 connectorBuilder: func() CustomHTTPSConnector { 66 var c = baseConnectorStruct 67 c.CustomHeaderName = "Custom_Name" 68 return c 69 }, 70 expectValidationError: true, 71 }, 72 "CustomHeaderValue specified without CustomHeaderName": { 73 connectorBuilder: func() CustomHTTPSConnector { 74 var c = baseConnectorStruct 75 c.CustomHeaderValue = "Custom header value" 76 return c 77 }, 78 expectValidationError: true, 79 }, 80 "CustomHeaderValue and CustomHeaderName both specified": { 81 connectorBuilder: func() CustomHTTPSConnector { 82 var c = baseConnectorStruct 83 c.CustomHeaderName = "Custom_Name" 84 c.CustomHeaderValue = "Custom header value" 85 return c 86 }, 87 expectValidationError: false, 88 }, 89 "CustomHeaderName contains forbidden characters": { 90 connectorBuilder: func() CustomHTTPSConnector { 91 var c = baseConnectorStruct 92 c.CustomHeaderName = "azAZ09_-!?>" 93 c.CustomHeaderValue = "Custom header value" 94 return c 95 }, 96 expectValidationError: true, 97 }, 98 "CustomHeaderName contains only allowed characters": { 99 connectorBuilder: func() CustomHTTPSConnector { 100 var c = baseConnectorStruct 101 c.CustomHeaderName = "azAZ09_-" 102 c.CustomHeaderValue = "Custom header value" 103 return c 104 }, 105 expectValidationError: false, 106 }, 107 "CustomHeaderValue and CustomHeaderName are optional": { 108 connectorBuilder: func() CustomHTTPSConnector { 109 return baseConnectorStruct 110 }, 111 expectValidationError: false, 112 }, 113 } 114 115 for name, test := range tests { 116 t.Run(name, func(t *testing.T) { 117 connectorStructure := test.connectorBuilder() 118 err := connectorStructure.Validate() 119 fmt.Println(err) 120 assert.True(t, (err != nil) == test.expectValidationError, err) 121 }) 122 } 123 }