Skip to content

Commit

Permalink
add a default user-agent header (#892)
Browse files Browse the repository at this point in the history
* add a default user-agent header

* add Version to util package; set util.Version as part of the release-build.sh script; use util.Version to make user-agent for http action

* Update check to lower case user input

* Remove setting version in main

* Remove version variable from main; use util.Version in place of mains version variable

* Update goss-shared.yaml with httpbin check for default user-agent

* Update goss-shared.yaml

Moved header tests into 1

* go fmt over whole project

* changed string array back to brackets instead of dashes

* sanity check

* Added: user-agent test back into test file

---------

Co-authored-by: Ahmed Elsabbahy <elsabbahyahmed@yahoo.com>
  • Loading branch information
catdevman and aelsabbahy authored May 16, 2024
1 parent ce8221d commit 1b523c0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 1 addition & 3 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/urfave/cli"
)

var version string

// converts a cli context into a goss Config
func newRuntimeConfigFromCLI(c *cli.Context) *util.Config {
cfg := &util.Config{
Expand Down Expand Up @@ -69,7 +67,7 @@ func timeoutFlag(value time.Duration) cli.DurationFlag {
func main() {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = version
app.Version = util.Version
app.Name = "goss"
app.Usage = "Quick and Easy server validation"
app.Flags = []cli.Flag{
Expand Down
4 changes: 3 additions & 1 deletion integration-tests/goss/goss-shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ http:
request-headers:
- "Foo: bar"
headers: ["Content-Type: application/json"]
body: ['"Foo": "bar"']
body:
- '"Foo": "bar"'
- '"User-Agent": "goss/0.0.0"'
http://httpbin/headers?host:
status: 200
timeout: 60000
Expand Down
2 changes: 1 addition & 1 deletion release-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fi
output="${output_dir}/${output_fname}"

GOOS="${os}" GOARCH="${arch}" CGO_ENABLED=0 go build \
-ldflags "-X main.version=${version_stamp} -s -w" \
-ldflags "-X github.com/goss-org/goss/util.Version=${version_stamp} -s -w" \
-o "${output}" \
github.com/goss-org/goss/cmd/goss

Expand Down
17 changes: 17 additions & 0 deletions system/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/goss-org/goss/util"
)

const USER_AGENT_HEADER_PREFIX = "user-agent:"
const DEFAULT_USER_AGENT_PREFIX = "goss/"

type HTTP interface {
HTTP() string
Status() (int, error)
Expand Down Expand Up @@ -47,6 +50,11 @@ type DefHTTP struct {

func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP {
headers := http.Header{}

if !hasUserAgentHeader(config.RequestHeader) {
config.RequestHeader = append(config.RequestHeader, fmt.Sprintf("%s %s%s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT_PREFIX, util.Version))
}

for _, r := range config.RequestHeader {
str := strings.SplitN(r, ": ", 2)
headers.Add(str[0], str[1])
Expand Down Expand Up @@ -209,3 +217,12 @@ func (u *DefHTTP) Body() (io.Reader, error) {

return u.resp.Body, nil
}

func hasUserAgentHeader(headers []string) bool {
for _, header := range headers {
if strings.HasPrefix(strings.ToLower(header), USER_AGENT_HEADER_PREFIX) {
return true
}
}
return false
}
3 changes: 3 additions & 0 deletions util/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util

var Version string

0 comments on commit 1b523c0

Please sign in to comment.