Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http driver #709

Merged
merged 4 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pkg/drivers/http/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package http
import (
"bytes"
"context"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/gobwas/glob"
"io"
"net/http"
"net/url"

"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/gobwas/glob"

"golang.org/x/net/html/charset"

"github.com/PuerkitoBio/goquery"
Expand All @@ -32,16 +33,18 @@ func NewDriver(opts ...Option) *Driver {
drv.options = NewOptions(opts)

drv.client = newHTTPClient(drv.options)
drv.client.Concurrency = drv.options.Concurrency
drv.client.MaxRetries = drv.options.MaxRetries
drv.client.Backoff = drv.options.Backoff

return drv
}

func newHTTPClient(options *Options) (httpClient *pester.Client) {
httpClient = pester.New()

httpClient.Concurrency = options.Concurrency
httpClient.MaxRetries = options.MaxRetries
httpClient.Backoff = options.Backoff
httpClient.Timeout = options.Timeout

if options.HTTPTransport != nil {
httpClient.Transport = options.HTTPTransport
}
Expand All @@ -54,8 +57,6 @@ func newHTTPClient(options *Options) (httpClient *pester.Client) {
return
}

httpClient = pester.NewExtendedClient(&http.Client{Transport: httpClient.Transport})

return
}

Expand Down Expand Up @@ -219,7 +220,7 @@ func (drv *Driver) makeRequest(ctx context.Context, req *http.Request, params dr
params.Headers.ForEach(func(value []string, key string) bool {
v := params.Headers.Get(key)

req.Header.Add(key, v)
req.Header.Set(key, v)

logger.
Debug().
Expand Down
27 changes: 0 additions & 27 deletions pkg/drivers/http/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ func Test_newHTTPClientWithTransport(t *testing.T) {
HTTPTransport: httpTransport,
}},
},
{
name: "check transport exist with pester.NewExtendedClient()",
args: args{options: &Options{
Options: &drivers.Options{
Proxy: "http://0.0.0.0",
},
HTTPTransport: httpTransport,
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -95,24 +86,6 @@ func Test_newHTTPClient(t *testing.T) {

So(hc, ShouldBeNil)
})

Convey("pester.NewExtend()", t, func() {
var (
client = newHTTPClient(&Options{
Options: &drivers.Options{
Proxy: "http://0.0.0.0",
},
})

rValue = reflect.ValueOf(client).Elem()
rField = rValue.Field(0)
)

rField = reflect.NewAt(rField.Type(), unsafe.Pointer(rField.UnsafeAddr())).Elem()
hc := rField.Interface().(*http.Client)

So(hc, ShouldNotBeNil)
})
}

func TestDriver_convertToUTF8(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/drivers/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
stdhttp "net/http"
"time"

"github.com/gobwas/glob"
"github.com/sethgrid/pester"
Expand All @@ -12,6 +13,7 @@ import (
var (
DefaultConcurrency = 1
DefaultMaxRetries = 5
DefaultTimeout = time.Second * 30
)

type (
Expand All @@ -29,6 +31,7 @@ type (
Concurrency int
HTTPCodesFilter []compiledStatusCodeFilter
HTTPTransport *stdhttp.Transport
Timeout time.Duration
}
)

Expand All @@ -39,6 +42,7 @@ func NewOptions(setters []Option) *Options {
opts.Backoff = pester.ExponentialBackoff
opts.Concurrency = DefaultConcurrency
opts.MaxRetries = DefaultMaxRetries
opts.Timeout = DefaultTimeout
opts.HTTPCodesFilter = make([]compiledStatusCodeFilter, 0, 5)

for _, setter := range setters {
Expand Down Expand Up @@ -143,3 +147,9 @@ func WithCustomTransport(transport *stdhttp.Transport) Option {
opts.HTTPTransport = transport
}
}

func WithTimeout(duration time.Duration) Option {
return func(opts *Options) {
opts.Timeout = duration
}
}
3 changes: 3 additions & 0 deletions pkg/drivers/http/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestNewOptions(t *testing.T) {
expectedMaxRetries := 2
expectedConcurrency := 10
expectedTransport := &stdhttp.Transport{}
expectedTimeout := time.Second * 5

opts := http.NewOptions([]http.Option{
http.WithCustomName(expectedName),
Expand Down Expand Up @@ -69,6 +70,7 @@ func TestNewOptions(t *testing.T) {
http.WithAllowedHTTPCode(401),
http.WithAllowedHTTPCodes([]int{403, 404}),
http.WithCustomTransport(expectedTransport),
http.WithTimeout(time.Second * 5),
})
So(opts.Options, ShouldNotBeNil)
So(opts.Name, ShouldEqual, expectedName)
Expand All @@ -81,5 +83,6 @@ func TestNewOptions(t *testing.T) {
So(opts.Concurrency, ShouldEqual, expectedConcurrency)
So(opts.HTTPCodesFilter, ShouldHaveLength, 3)
So(opts.HTTPTransport, ShouldEqual, expectedTransport)
So(opts.Timeout, ShouldEqual, expectedTimeout)
})
}