Skip to content

Commit

Permalink
Added request headers to http tests (#496)
Browse files Browse the repository at this point in the history
* Added request headers to http tests

* Pinning go 1.13

* Pinning Travis version of go to 1.13

* Bumping the count of tests being run

* Updating the docs to reflect new request headers
  • Loading branch information
dacamp authored and aelsabbahy committed Nov 23, 2019
1 parent 4aa1330 commit aa0fc08
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.11.x
- 1.13.x

sudo: required
dist: trusty
Expand Down
2 changes: 2 additions & 0 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ http:
allow-insecure: false
no-follow-redirects: false # Setting this to true will NOT follow redirects
timeout: 1000
request-header: # Optionally you can set request header values
- "Content-Type: text/html"
body: [] # Check http response content for these patterns
username: "" # username for basic auth
password: "" # password for basic auth
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ require (
golang.org/x/sys v0.0.0-20181210030007-2a47403f2ae5
gopkg.in/yaml.v2 v2.0.0-20160928153709-a5b47d31c556
)

go 1.13
6 changes: 6 additions & 0 deletions integration-tests/goss/goss-shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ http:
allow-insecure: false
timeout: 5000
body: []
https://google.com:
status: 200
allow-insecure: false
timeout: 5000
request-headers:
- "Host: www.google.com"
https://httpbin.org/basic-auth/username/secret:
status: 200
username: username
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" -g "/gos
echo "$out"

if [[ $os == "arch" ]]; then
egrep -q 'Count: 77, Failed: 0, Skipped: 3' <<<"$out"
egrep -q 'Count: 78, Failed: 0, Skipped: 3' <<<"$out"
else
egrep -q 'Count: 93, Failed: 0, Skipped: 5' <<<"$out"
egrep -q 'Count: 94, Failed: 0, Skipped: 5' <<<"$out"
fi

if [[ ! $os == "arch" ]]; then
Expand Down
5 changes: 4 additions & 1 deletion resource/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type HTTP struct {
AllowInsecure bool `json:"allow-insecure" yaml:"allow-insecure"`
NoFollowRedirects bool `json:"no-follow-redirects" yaml:"no-follow-redirects"`
Timeout int `json:"timeout" yaml:"timeout"`
RequestHeader []string `json:"request-headers,omitempty" yaml:"request-headers,omitempty"`
Body []string `json:"body" yaml:"body"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Expand All @@ -33,7 +34,8 @@ func (u *HTTP) Validate(sys *system.System) []TestResult {
}
sysHTTP := sys.NewHTTP(u.HTTP, sys, util.Config{
AllowInsecure: u.AllowInsecure, NoFollowRedirects: u.NoFollowRedirects,
Timeout: u.Timeout, Username: u.Username, Password: u.Password})
Timeout: u.Timeout, Username: u.Username, Password: u.Password,
RequestHeader: u.RequestHeader})
sysHTTP.SetAllowInsecure(u.AllowInsecure)
sysHTTP.SetNoFollowRedirects(u.NoFollowRedirects)

Expand All @@ -59,6 +61,7 @@ func NewHTTP(sysHTTP system.HTTP, config util.Config) (*HTTP, error) {
u := &HTTP{
HTTP: http,
Status: status,
RequestHeader: []string{},
Body: []string{},
AllowInsecure: config.AllowInsecure,
NoFollowRedirects: config.NoFollowRedirects,
Expand Down
16 changes: 13 additions & 3 deletions system/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"crypto/tls"
"io"
"net/http"
"strings"
"time"

"github.com/aelsabbahy/goss/util"
)

Expand All @@ -22,20 +24,27 @@ type DefHTTP struct {
allowInsecure bool
noFollowRedirects bool
resp *http.Response
RequestHeader http.Header
Timeout int
loaded bool
err error
Username string
Password string
}

func NewDefHTTP(http string, system *System, config util.Config) HTTP {
func NewDefHTTP(httpStr string, system *System, config util.Config) HTTP {
headers := http.Header{}
for _, r := range config.RequestHeader {
str := strings.SplitN(r, ": ", 2)
headers.Add(str[0], str[1])
}
return &DefHTTP{
http: http,
http: httpStr,
allowInsecure: config.AllowInsecure,
noFollowRedirects: config.NoFollowRedirects,
RequestHeader: headers,
Timeout: config.Timeout,
Username: config.Username,
Username: config.Username,
Password: config.Password,
}
}
Expand Down Expand Up @@ -65,6 +74,7 @@ func (u *DefHTTP) setup() error {
if err != nil {
return u.err
}
req.Header = u.RequestHeader.Clone()
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 @@ -10,6 +10,7 @@ import (

type Config struct {
IgnoreList []string
RequestHeader []string
Timeout int
AllowInsecure bool
NoFollowRedirects bool
Expand Down

0 comments on commit aa0fc08

Please sign in to comment.