github.com/MontFerret/ferret@v0.18.0/pkg/drivers/http/options_test.go (about) 1 package http_test 2 3 import ( 4 stdhttp "net/http" 5 "testing" 6 "time" 7 8 "github.com/sethgrid/pester" 9 . "github.com/smartystreets/goconvey/convey" 10 11 "github.com/MontFerret/ferret/pkg/drivers" 12 "github.com/MontFerret/ferret/pkg/drivers/http" 13 ) 14 15 func TestNewOptions(t *testing.T) { 16 Convey("Should create driver options with initial values", t, func() { 17 opts := http.NewOptions([]http.Option{}) 18 So(opts.Options, ShouldNotBeNil) 19 So(opts.Name, ShouldEqual, http.DriverName) 20 So(opts.Backoff, ShouldEqual, pester.ExponentialBackoff) 21 So(opts.Concurrency, ShouldEqual, http.DefaultConcurrency) 22 So(opts.MaxRetries, ShouldEqual, http.DefaultMaxRetries) 23 So(opts.HTTPCodesFilter, ShouldHaveLength, 0) 24 }) 25 26 Convey("Should use setters to set values", t, func() { 27 expectedName := http.DriverName + "2" 28 expectedUA := "Mozilla" 29 expectedProxy := "https://proxy.com" 30 expectedMaxRetries := 2 31 expectedConcurrency := 10 32 expectedTransport := &stdhttp.Transport{} 33 expectedTimeout := time.Second * 5 34 35 opts := http.NewOptions([]http.Option{ 36 http.WithCustomName(expectedName), 37 http.WithUserAgent(expectedUA), 38 http.WithProxy(expectedProxy), 39 http.WithCookie(drivers.HTTPCookie{ 40 Name: "Session", 41 Value: "fsdfsdfs", 42 Path: "dfsdfsd", 43 Domain: "sfdsfs", 44 Expires: time.Time{}, 45 MaxAge: 0, 46 Secure: false, 47 HTTPOnly: false, 48 SameSite: 0, 49 }), 50 http.WithCookies([]drivers.HTTPCookie{ 51 { 52 Name: "Use", 53 Value: "Foos", 54 Path: "", 55 Domain: "", 56 Expires: time.Time{}, 57 MaxAge: 0, 58 Secure: false, 59 HTTPOnly: false, 60 SameSite: 0, 61 }, 62 }), 63 http.WithHeader("Authorization", []string{"Bearer dfsd7f98sd9fsd9fsd"}), 64 http.WithHeaders(drivers.NewHTTPHeadersWith(map[string][]string{ 65 "x-correlation-id": {"232483833833839"}, 66 })), 67 http.WithDefaultBackoff(), 68 http.WithMaxRetries(expectedMaxRetries), 69 http.WithConcurrency(expectedConcurrency), 70 http.WithAllowedHTTPCode(401), 71 http.WithAllowedHTTPCodes([]int{403, 404}), 72 http.WithCustomTransport(expectedTransport), 73 http.WithTimeout(time.Second * 5), 74 }) 75 So(opts.Options, ShouldNotBeNil) 76 So(opts.Name, ShouldEqual, expectedName) 77 So(opts.UserAgent, ShouldEqual, expectedUA) 78 So(opts.Proxy, ShouldEqual, expectedProxy) 79 So(opts.Cookies.Length(), ShouldEqual, 2) 80 So(opts.Headers.Length(), ShouldEqual, 2) 81 So(opts.Backoff, ShouldEqual, pester.DefaultBackoff) 82 So(opts.MaxRetries, ShouldEqual, expectedMaxRetries) 83 So(opts.Concurrency, ShouldEqual, expectedConcurrency) 84 So(opts.HTTPCodesFilter, ShouldHaveLength, 3) 85 So(opts.HTTPTransport, ShouldEqual, expectedTransport) 86 So(opts.Timeout, ShouldEqual, expectedTimeout) 87 }) 88 }