github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/botman_test.go (about) 1 package botman 2 3 import ( 4 "bytes" 5 "crypto/tls" 6 "crypto/x509" 7 "encoding/json" 8 "fmt" 9 "io/ioutil" 10 "net/http" 11 "net/http/httptest" 12 "net/url" 13 "testing" 14 15 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/edgegrid" 16 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session" 17 "github.com/stretchr/testify/assert" 18 "github.com/stretchr/testify/require" 19 ) 20 21 func mockAPIClient(t *testing.T, mockServer *httptest.Server) BotMan { 22 serverURL, err := url.Parse(mockServer.URL) 23 require.NoError(t, err) 24 certPool := x509.NewCertPool() 25 certPool.AddCert(mockServer.Certificate()) 26 httpClient := &http.Client{ 27 Transport: &http.Transport{ 28 TLSClientConfig: &tls.Config{ 29 RootCAs: certPool, 30 }, 31 }, 32 } 33 s, err := session.New(session.WithClient(httpClient), session.WithSigner(&edgegrid.Config{Host: serverURL.Host})) 34 assert.NoError(t, err) 35 return Client(s) 36 } 37 38 func dummyOpt() Option { 39 return func(*botman) { 40 41 } 42 } 43 44 func loadFixture(path string) string { 45 contents, err := ioutil.ReadFile(path) 46 if err != nil { 47 panic(err) 48 } 49 return compactJSON(contents) 50 } 51 52 // compactJSON converts a JSON-encoded byte slice to a compact form (so our JSON fixtures can be readable) 53 func compactJSON(encoded []byte) string { 54 buf := bytes.Buffer{} 55 if err := json.Compact(&buf, encoded); err != nil { 56 panic(fmt.Sprintf("%s: %s", err, string(encoded))) 57 } 58 59 return buf.String() 60 } 61 62 func TestClient(t *testing.T) { 63 sess, err := session.New() 64 require.NoError(t, err) 65 tests := map[string]struct { 66 options []Option 67 expected *botman 68 }{ 69 "no options provided, return default": { 70 options: nil, 71 expected: &botman{ 72 Session: sess, 73 }, 74 }, 75 "dummy option": { 76 options: []Option{dummyOpt()}, 77 expected: &botman{ 78 Session: sess, 79 }, 80 }, 81 } 82 for name, test := range tests { 83 t.Run(name, func(t *testing.T) { 84 res := Client(sess, test.options...) 85 assert.Equal(t, res, test.expected) 86 }) 87 } 88 }