Skip to content

Commit

Permalink
remove user-agent and add tests for http header setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmystewpot committed Jan 9, 2025
1 parent 7335190 commit 7141c1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
7 changes: 0 additions & 7 deletions syft/pkg/cataloger/rust/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ Package rust provides a concrete Cataloger implementation relating to packages w
package rust

import (
"fmt"

"github.com/anchore/syft/internal/mimetype"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/generic"
)

const (
toolName = "syft" // used for the user-agent string.
cargoAuditBinaryCatalogerName = "rust-cargo-auditable-binary-cataloger"
cargoLockCatalogerName = "rust-cargo-lock-cataloger"
)

var (
userAgent = fmt.Sprintf("%s/%s", toolName, syftVersion())
)

// NewCargoLockCataloger returns a new Rust Cargo lock file cataloger object.
func NewCargoLockCataloger(opts CatalogerConfig) pkg.Cataloger {
return generic.NewCataloger(cargoLockCatalogerName).
Expand Down
1 change: 0 additions & 1 deletion syft/pkg/cataloger/rust/crates_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,5 @@ func newCratesLookupClient(opts CatalogerConfig) *http.Client {
// to the values used when querying crates.io.
func setHeaders(request *http.Request) *http.Request {
request.Header.Set("Accept", "application/json; charset=utf8")
request.Header.Set("User-Agent", userAgent)
return request
}
25 changes: 25 additions & 0 deletions syft/pkg/cataloger/rust/crates_resolver_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rust

import (
"net/http"
"os"
"testing"

Expand Down Expand Up @@ -52,3 +53,27 @@ func Test_parseCratesResponse(t *testing.T) {
})
}
}

func Test_setHeaders(t *testing.T) {
req, _ := http.NewRequest("GET", "http://foo/bar", nil)
type args struct {
request *http.Request
}
tests := []struct {
name string
args args
}{
{
name: "set header",
args: args{
request: req,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := setHeaders(tt.args.request)
assert.Contains(t, got.Header, "Accept")
})
}
}

0 comments on commit 7141c1f

Please sign in to comment.