Skip to content
Permalink
Browse files

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
  • Loading branch information
dacamp authored and aelsabbahy committed Nov 23, 2019
1 parent 4aa1330 commit aa0fc088b0e9a56272bcb0b404372d8d81607b28
Showing with 31 additions and 7 deletions.
  1. +1 −1 .travis.yml
  2. +2 −0 docs/manual.md
  3. +2 −0 go.mod
  4. +6 −0 integration-tests/goss/goss-shared.yaml
  5. +2 −2 integration-tests/test.sh
  6. +4 −1 resource/http.go
  7. +13 −3 system/http.go
  8. +1 −0 util/config.go
@@ -1,7 +1,7 @@
language: go

go:
- 1.11.x
- 1.13.x

sudo: required
dist: trusty
@@ -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
2 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
@@ -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
@@ -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
@@ -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,
@@ -4,7 +4,9 @@ import (
"crypto/tls"
"io"
"net/http"
"strings"
"time"

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

@@ -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,
}
}
@@ -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)
}
@@ -10,6 +10,7 @@ import (

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

0 comments on commit aa0fc08

Please sign in to comment.
You can’t perform that action at this time.