From aa0fc088b0e9a56272bcb0b404372d8d81607b28 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Sat, 23 Nov 2019 12:27:43 -0800 Subject: [PATCH] Added request headers to http tests (#496) * 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 --- .travis.yml | 2 +- docs/manual.md | 2 ++ go.mod | 2 ++ integration-tests/goss/goss-shared.yaml | 6 ++++++ integration-tests/test.sh | 4 ++-- resource/http.go | 5 ++++- system/http.go | 16 +++++++++++++--- util/config.go | 1 + 8 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 51ad5eb73..6dd4a89c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: -- 1.11.x +- 1.13.x sudo: required dist: trusty diff --git a/docs/manual.md b/docs/manual.md index 9db5d8f10..4527eb6c1 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -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 diff --git a/go.mod b/go.mod index 7bbfc989e..2c68435a7 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index 5f642a880..93ff7e2fe 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -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 diff --git a/integration-tests/test.sh b/integration-tests/test.sh index 5520f3704..6ba06d68c 100755 --- a/integration-tests/test.sh +++ b/integration-tests/test.sh @@ -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 diff --git a/resource/http.go b/resource/http.go index 7b98d99d6..3dd260182 100644 --- a/resource/http.go +++ b/resource/http.go @@ -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"` @@ -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) @@ -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, diff --git a/system/http.go b/system/http.go index 09122f055..fc18225a4 100644 --- a/system/http.go +++ b/system/http.go @@ -4,7 +4,9 @@ import ( "crypto/tls" "io" "net/http" + "strings" "time" + "github.com/aelsabbahy/goss/util" ) @@ -22,6 +24,7 @@ type DefHTTP struct { allowInsecure bool noFollowRedirects bool resp *http.Response + RequestHeader http.Header Timeout int loaded bool err error @@ -29,13 +32,19 @@ type DefHTTP struct { 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, } } @@ -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) } diff --git a/util/config.go b/util/config.go index e968eef08..ed28ff8ce 100644 --- a/util/config.go +++ b/util/config.go @@ -10,6 +10,7 @@ import ( type Config struct { IgnoreList []string + RequestHeader []string Timeout int AllowInsecure bool NoFollowRedirects bool