Skip to content

Commit

Permalink
Merge pull request #7 from SimonBaeumer/add-http-header-support
Browse files Browse the repository at this point in the history
Add http header resource
  • Loading branch information
SimonBaeumer authored Jan 31, 2019
2 parents 5a03d37 + 5ebb080 commit df58e43
Show file tree
Hide file tree
Showing 29 changed files with 741 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/vendor/
/integration-tests/**/goss-linux-386
/integration-tests/**/goss-linux-amd64
development/http_test_server

# Random stuff for my local testing/development that I don't want checked in
tmp/
Expand Down
13 changes: 13 additions & 0 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
// Simple wrapper to add multiple resources
func AddResources(fileName, resourceName string, keys []string, c *cli.Context) error {
OutStoreFormat = getStoreFormatFromFileName(fileName)
header := extractHeaderArgument(c.String("header"))

config := util.Config{
IgnoreList: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
Expand All @@ -23,6 +25,7 @@ func AddResources(fileName, resourceName string, keys []string, c *cli.Context)
Server: c.String("server"),
Username: c.String("username"),
Password: c.String("password"),
Header: header,
}

var gossConfig GossConfig
Expand All @@ -44,6 +47,16 @@ func AddResources(fileName, resourceName string, keys []string, c *cli.Context)
return nil
}

func extractHeaderArgument(headerArg string) map[string][]string {
if headerArg == "" {
return make(map[string][]string)
}
rawHeaders := strings.Split(headerArg, ":")
headers := make(map[string][]string)
headers[rawHeaders[0]] = []string{strings.TrimSpace(rawHeaders[1])}
return headers
}

func AddResource(fileName string, gossConfig GossConfig, resourceName, key string, c *cli.Context, config util.Config, sys *system.System) error {
// Need to figure out a good way to refactor this
switch resourceName {
Expand Down
21 changes: 21 additions & 0 deletions add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package goss

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_ExtractHeaderArgument(t *testing.T) {
expected := make(map[string][]string)
expected["Set-Cookie"] = []string{"something"}

headerArgs := "Set-Cookie: something"
got := extractHeaderArgument(headerArgs)

assert.Equal(t, expected, got)
}

func Test_ExtractHeaderArgumentWithoutEmptyArg(t *testing.T) {
got := extractHeaderArgument("")
assert.Equal(t, make(map[string][]string), got)
}
4 changes: 4 additions & 0 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ func main() {
Name: "password, p",
Usage: "Password for basic auth",
},
cli.StringFlag{
Name: "header",
Usage: "Set-Cookie: Value",
},
},
Action: func(c *cli.Context) error {
goss.AddResources(c.GlobalString("gossfile"), "HTTP", c.Args(), c)
Expand Down
26 changes: 26 additions & 0 deletions development/http_test_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"log"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
for k, values := range r.Header {
for _, value := range values {
fmt.Println("key:", k, "val:", value)
}
}

w.Header().Add("key", "value")
}

func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":9090", nil)

if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
7 changes: 7 additions & 0 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,13 @@ http:
body: [] # Check http response content for these patterns
username: "" # username for basic auth
password: "" # password for basic auth
headers: # Check for http headers, is not support for add command
key:
- value
- another value
request-headers: # define headers should be send within the request
key:
- value
```


Expand Down
6 changes: 6 additions & 0 deletions integration-tests/goss/alpine3/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
6 changes: 6 additions & 0 deletions integration-tests/goss/alpine3/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
6 changes: 6 additions & 0 deletions integration-tests/goss/centos7/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
6 changes: 6 additions & 0 deletions integration-tests/goss/centos7/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
4 changes: 2 additions & 2 deletions integration-tests/goss/generate_goss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ goss a "${args[@]}" kernel-param kernel.ostype

goss a "${args[@]}" mount /dev

goss a "${args[@]}" http https://www.google.com
goss a "${args[@]}" http https://www.google.com --header "Server: gws"

goss a "${args[@]}" http http://google.com -r
goss a "${args[@]}" http http://google.com -r --header "Server: gws"

# Auto-add
# Validate that empty configs don't get created
Expand Down
6 changes: 6 additions & 0 deletions integration-tests/goss/precise/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
6 changes: 6 additions & 0 deletions integration-tests/goss/precise/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
6 changes: 6 additions & 0 deletions integration-tests/goss/wheezy/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
6 changes: 6 additions & 0 deletions integration-tests/goss/wheezy/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,15 @@ http:
no-follow-redirects: true
timeout: 5000
body: []
headers:
Server:
- gws
https://www.google.com:
status: 200
allow-insecure: false
no-follow-redirects: false
timeout: 5000
body: []
headers:
Server:
- gws
2 changes: 1 addition & 1 deletion integration-tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ seccomp_opts() {
cp "../release/goss-linux-$arch" "goss/$os/"
# Run build if Dockerfile has changed but hasn't been pushed to dockerhub
if ! md5sum -c "Dockerfile_${os}.md5"; then
docker build -t "simonbaeumer/goss_${os}:latest" - < "Dockerfile_$os"
docker build -q -t "simonbaeumer/goss_${os}:latest" - < "Dockerfile_$os"
# Pull if image doesn't exist locally
elif ! docker images | grep "SimonBaeumer/goss_$os";then
docker pull "simonbaeumer/goss_$os"
Expand Down
5 changes: 2 additions & 3 deletions resource/gomega_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resource

import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"testing"
Expand Down Expand Up @@ -163,8 +162,8 @@ func gomegaEqual(g, w interface{}, negateTester bool) bool {
}
gotMessage = sanitizeMatcherText(gotMessage)
wantMessage = sanitizeMatcherText(wantMessage)
fmt.Println("got:", gotMessage)
fmt.Println("want:", wantMessage)
//fmt.Println("got:", gotMessage)
//fmt.Println("want:", wantMessage)

return gotT == wantT &&
gotMessage == wantMessage
Expand Down
Loading

0 comments on commit df58e43

Please sign in to comment.