Skip to content

feat: Add support for specifying URL Query params in HTTP resource #1013

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/gossfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ http:
timeout: 1000
request-headers: # Set request header values
- "Content-Type: text/html"
request-query-params:
foo: 'bar'
headers: [] # Check http response headers for these patterns (e.g. "Content-Type: text/html")
request-body: '{"key": "value"}' # request body
body: [] # Check http response content for these patterns
Expand Down
66 changes: 34 additions & 32 deletions resource/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@ import (
)

type HTTP struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
id string `json:"-" yaml:"-"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Method string `json:"method,omitempty" yaml:"method,omitempty"`
Status matcher `json:"status" yaml:"status"`
AllowInsecure bool `json:"allow-insecure" yaml:"allow-insecure"`
NoFollowRedirects bool `json:"no-follow-redirects" yaml:"no-follow-redirects"`
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
RequestHeader []string `json:"request-headers,omitempty" yaml:"request-headers,omitempty"`
RequestBody string `json:"request-body,omitempty" yaml:"request-body,omitempty"`
Headers matcher `json:"headers,omitempty" yaml:"headers,omitempty"`
Body matcher `json:"body,omitempty" yaml:"body,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
CAFile string `json:"ca-file,omitempty" yaml:"ca-file,omitempty"`
CertFile string `json:"cert-file,omitempty" yaml:"cert-file,omitempty"`
KeyFile string `json:"key-file,omitempty" yaml:"key-file,omitempty"`
Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"`
Proxy string `json:"proxy,omitempty" yaml:"proxy,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
id string `json:"-" yaml:"-"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Method string `json:"method,omitempty" yaml:"method,omitempty"`
Status matcher `json:"status" yaml:"status"`
AllowInsecure bool `json:"allow-insecure" yaml:"allow-insecure"`
NoFollowRedirects bool `json:"no-follow-redirects" yaml:"no-follow-redirects"`
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
RequestHeader []string `json:"request-headers,omitempty" yaml:"request-headers,omitempty"`
RequestBody string `json:"request-body,omitempty" yaml:"request-body,omitempty"`
RequestQueryParams map[string]string `json:"request-query-params,omitempty" yaml:"request-query-params,omitempty"`
Headers matcher `json:"headers,omitempty" yaml:"headers,omitempty"`
Body matcher `json:"body,omitempty" yaml:"body,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
CAFile string `json:"ca-file,omitempty" yaml:"ca-file,omitempty"`
CertFile string `json:"cert-file,omitempty" yaml:"cert-file,omitempty"`
KeyFile string `json:"key-file,omitempty" yaml:"key-file,omitempty"`
Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"`
Proxy string `json:"proxy,omitempty" yaml:"proxy,omitempty"`
}

const (
Expand Down Expand Up @@ -75,7 +76,7 @@ func (u *HTTP) Validate(sys *system.System) []TestResult {
KeyFile: u.KeyFile,
NoFollowRedirects: u.NoFollowRedirects,
Timeout: time.Duration(u.Timeout) * time.Millisecond, Username: u.Username, Password: u.Password, Proxy: u.Proxy,
RequestHeader: u.RequestHeader, RequestBody: u.RequestBody, Method: u.Method})
RequestHeader: u.RequestHeader, RequestBody: u.RequestBody, RequestQueryParams: u.RequestQueryParams, Method: u.Method})
sysHTTP.SetAllowInsecure(u.AllowInsecure)
sysHTTP.SetNoFollowRedirects(u.NoFollowRedirects)

Expand All @@ -98,17 +99,18 @@ func NewHTTP(sysHTTP system.HTTP, config util.Config) (*HTTP, error) {
http := sysHTTP.HTTP()
status, err := sysHTTP.Status()
u := &HTTP{
id: http,
Status: status,
RequestHeader: []string{},
Headers: nil,
Body: []string{},
AllowInsecure: config.AllowInsecure,
NoFollowRedirects: config.NoFollowRedirects,
Timeout: config.TimeOutMilliSeconds(),
Username: config.Username,
Password: config.Password,
Proxy: config.Proxy,
id: http,
Status: status,
RequestHeader: []string{},
RequestQueryParams: nil,
Headers: nil,
Body: []string{},
AllowInsecure: config.AllowInsecure,
NoFollowRedirects: config.NoFollowRedirects,
Timeout: config.TimeOutMilliSeconds(),
Username: config.Username,
Password: config.Password,
Proxy: config.Proxy,
}
return u, err
}
68 changes: 39 additions & 29 deletions system/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ type HTTP interface {
}

type DefHTTP struct {
http string
allowInsecure bool
noFollowRedirects bool
resp *http.Response
RequestHeader http.Header
RequestBody string
Timeout int
loaded bool
err error
Username string
Password string
CAFile string
CertFile string
KeyFile string
Method string
Proxy string
http string
allowInsecure bool
noFollowRedirects bool
resp *http.Response
RequestHeader http.Header
RequestBody string
RequestQueryParams map[string]string
Timeout int
loaded bool
err error
Username string
Password string
CAFile string
CertFile string
KeyFile string
Method string
Proxy string
}

func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP {
Expand All @@ -60,19 +61,20 @@ func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.C
headers.Add(str[0], str[1])
}
return &DefHTTP{
http: httpStr,
allowInsecure: config.AllowInsecure,
Method: config.Method,
noFollowRedirects: config.NoFollowRedirects,
RequestHeader: headers,
RequestBody: config.RequestBody,
Timeout: config.TimeOutMilliSeconds(),
Username: config.Username,
Password: config.Password,
CAFile: config.CAFile,
CertFile: config.CertFile,
KeyFile: config.KeyFile,
Proxy: config.Proxy,
http: httpStr,
allowInsecure: config.AllowInsecure,
Method: config.Method,
noFollowRedirects: config.NoFollowRedirects,
RequestHeader: headers,
RequestBody: config.RequestBody,
RequestQueryParams: config.RequestQueryParams,
Timeout: config.TimeOutMilliSeconds(),
Username: config.Username,
Password: config.Password,
CAFile: config.CAFile,
CertFile: config.CertFile,
KeyFile: config.KeyFile,
Proxy: config.Proxy,
}
}

Expand Down Expand Up @@ -162,6 +164,14 @@ func (u *DefHTTP) setupReal() error {
req.Host = host
}

if u.RequestQueryParams != nil {
qParams := req.URL.Query()
for k, v := range u.RequestQueryParams {
qParams.Add(k, v)
}
req.URL.RawQuery = qParams.Encode()
}

if u.Username != "" || u.Password != "" {
req.SetBasicAuth(u.Username, u.Password)
}
Expand Down
1 change: 1 addition & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Config struct {
RequestBody string
Proxy string
RequestHeader []string
RequestQueryParams map[string]string
RetryTimeout time.Duration
RunLevel string
Server string
Expand Down
Loading