Skip to content

Commit

Permalink
Added a host header override option
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kuklin <[email protected]>
  • Loading branch information
Anton Kuklin authored and anton-kuklin committed Jan 26, 2024
1 parent bc7247a commit 431b235
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type httpCallerOptions struct {
callFrequency time.Duration
maxConcurrentCalls int

host string
headers http.Header
method string
body []byte
Expand Down Expand Up @@ -75,6 +76,7 @@ func WithHTTPCallerMaxConcurrentCalls(max int) HTTPCallerOption {

// WithHTTPCallerHeaders is a functional parameter for a HTTPCaller which specifies headers that should be
// set in request.
// To override a Host header use a WithHTTPCallerHost method.
func WithHTTPCallerHeaders(headers http.Header) HTTPCallerOption {
return func(options *httpCallerOptions) {
options.headers = headers
Expand All @@ -90,6 +92,13 @@ func WithHTTPCallerMethod(method string) HTTPCallerOption {
}
}

// WithHTTPCallerHost is a functional parameter for a HTTPCaller which allowed to override a host header.
func WithHTTPCallerHost(host string) HTTPCallerOption {
return func(options *httpCallerOptions) {
options.host = host
}
}

// WithHTTPCallerBody is a functional parameter for a HTTPCaller which specifies a body that should be set
// in request.
func WithHTTPCallerBody(body []byte) HTTPCallerOption {
Expand Down Expand Up @@ -228,6 +237,7 @@ func NewHttpCaller(url string, options ...HTTPCallerOption) *HTTPCaller {
maxConcurrentCalls: opts.maxConcurrentCalls,

url: url,
host: opts.host,
headers: opts.headers,
method: opts.method,
body: opts.body,
Expand Down Expand Up @@ -269,6 +279,9 @@ type HTTPCaller struct {
// url is an url which will be used in all probe requests, mandatory in constructor.
url string

// host allows to override a Host header
host string

// headers are headers that which will be used in all probe requests, default are none.
headers http.Header

Expand Down Expand Up @@ -560,6 +573,9 @@ func (c *HTTPCaller) makeCall(ctx context.Context) error {
return err
}
req.Header = c.headers
if c.host != "" {
req.Host = c.host
}

if c.onReq != nil {
suite.mu.Lock()
Expand Down

0 comments on commit 431b235

Please sign in to comment.