Skip to content

Commit

Permalink
Convenience function: AddHeaderOptional
Browse files Browse the repository at this point in the history
  • Loading branch information
yaronf committed Jun 13, 2024
1 parent c7bb950 commit fde4fb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func (fs *Fields) AddHeaderExt(hdr string, optional bool, binarySequence bool, a
return fs
}

// AddHeaderOptional appends a bare header name, e.g. "cache-control". However, the header is not required to exist
// in the message. This is a convenience function and AddHeaderExt is more general.
func (fs *Fields) AddHeaderOptional(hdr string) *Fields {
return fs.AddHeaderExt(hdr, true, false, false, false)
}

func fromQueryParam(qp string) *field {
i := httpsfv.NewItem("@query-param")
i.Params.Add("name", QueryEscapeForSignature(qp))
Expand Down
4 changes: 2 additions & 2 deletions signatures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ func TestVerifyResponse(t *testing.T) {

func TestOptionalSign(t *testing.T) {
req := readRequest(httpreq2)
f1 := NewFields().AddHeader("date").AddHeaderExt("x-optional", true, false, false, false)
f1 := NewFields().AddHeader("date").AddHeaderOptional("x-optional")
key1 := bytes.Repeat([]byte{0x55}, 64)
signer1, err := NewHMACSHA256Signer(key1, NewSignConfig().setFakeCreated(9999).SetKeyID("key1"), *f1)
assert.NoError(t, err, "Could not create signer")
Expand Down Expand Up @@ -1777,7 +1777,7 @@ func TestRequestBinding(t *testing.T) {
func TestOptionalVerify(t *testing.T) {
req := readRequest(httpreq2)
req.Header.Add("X-Opt1", "val1")
f1 := NewFields().AddHeader("date").AddHeaderExt("x-opt1", true, false, false, false)
f1 := NewFields().AddHeader("date").AddHeaderOptional("x-opt1")
key1 := bytes.Repeat([]byte{0x66}, 64)
signer, err := NewHMACSHA256Signer(key1, NewSignConfig().setFakeCreated(8888).SetKeyID("key1"), *f1)
assert.NoError(t, err, "Could not create signer")
Expand Down

0 comments on commit fde4fb2

Please sign in to comment.