Skip to content

Commit

Permalink
feat: AddHeader function that appends the headers list
Browse files Browse the repository at this point in the history
  • Loading branch information
lihe07 committed Oct 31, 2024
1 parent cdde9e1 commit 2d40891
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hijack.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ func (ctx *HijackResponse) SetHeader(pairs ...string) *HijackResponse {
return ctx
}

// Append key-value pairs to the end of the response headers.

Check failure on line 381 in hijack.go

View workflow job for this annotation

GitHub Actions / test-linux

exported: comment on exported method HijackResponse.AddHeader should be of the form "AddHeader ..." (revive)
// Duplicate keys will be preserved.
func (ctx *HijackResponse) AddHeader(pairs ...string) *HijackResponse {
for i := 0; i < len(pairs); i += 2 {
ctx.payload.ResponseHeaders = append(ctx.payload.ResponseHeaders, &proto.FetchHeaderEntry{
Name: pairs[i],
Value: pairs[i+1],
})
}
return ctx
}

// SetBody of the payload, if obj is []byte or string, raw body will be used, else it will be encoded as json.
func (ctx *HijackResponse) SetBody(obj interface{}) *HijackResponse {
switch body := obj.(type) {
Expand Down
2 changes: 2 additions & 0 deletions hijack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func TestHijack(t *testing.T) {
g.Has(ctx.Response.Headers().Get("Content-Type"), "text/html; charset=utf-8")

// override response header
ctx.Response.AddHeader("Set-Cookie", "key=val1")
// This should override the previous one
ctx.Response.SetHeader("Set-Cookie", "key=val")

// override response body
Expand Down

0 comments on commit 2d40891

Please sign in to comment.