Skip to content

Commit

Permalink
πŸ”₯ v3 (feature): Adding GetReqHeaders and GetRespHeaders (#2831)
Browse files Browse the repository at this point in the history
* Adding GetRespHeaders from v2

Signed-off-by: brunodmartins <bdm2943@icloud.com>

* Adding GetReqHeaders from v2

Signed-off-by: brunodmartins <bdm2943@icloud.com>

* Fixed linter on tests

Signed-off-by: brunodmartins <bdm2943@icloud.com>

---------

Signed-off-by: brunodmartins <bdm2943@icloud.com>
  • Loading branch information
brunodmartins committed Feb 5, 2024
1 parent 926c537 commit 9b0a99b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,30 @@ func (c *DefaultCtx) GetRespHeader(key string, defaultValue ...string) string {
return defaultString(c.app.getString(c.fasthttp.Response.Header.Peek(key)), defaultValue)
}

// GetRespHeaders returns the HTTP response headers.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
func (c *DefaultCtx) GetRespHeaders() map[string][]string {
headers := make(map[string][]string)
c.Response().Header.VisitAll(func(k, v []byte) {
key := c.app.getString(k)
headers[key] = append(headers[key], c.app.getString(v))
})
return headers
}

// GetReqHeaders returns the HTTP request headers.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
func (c *DefaultCtx) GetReqHeaders() map[string][]string {
headers := make(map[string][]string)
c.Request().Header.VisitAll(func(k, v []byte) {
key := c.app.getString(k)
headers[key] = append(headers[key], c.app.getString(v))
})
return headers
}

// Host contains the host derived from the X-Forwarded-Host or Host HTTP header.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
Expand Down
10 changes: 10 additions & 0 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ type Ctx interface {
// Make copies or use the Immutable setting instead.
GetRespHeader(key string, defaultValue ...string) string

// GetRespHeaders returns the HTTP response headers.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
GetRespHeaders() map[string][]string

// GetReqHeaders returns the HTTP request headers.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
GetReqHeaders() map[string][]string

// Host contains the host derived from the X-Forwarded-Host or Host HTTP header.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
Expand Down
86 changes: 86 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4853,3 +4853,89 @@ func Test_Ctx_extractIPsFromHeader_EnableValidateIp(t *testing.T) {
res := ips[len(ips)-2]
require.Equal(t, "42.118.81.169", res)
}

// go test -run Test_Ctx_GetRespHeaders
func Test_Ctx_GetRespHeaders(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})

c.Set("test", "Hello, World πŸ‘‹!")
c.Set("foo", "bar")
c.Response().Header.Set("multi", "one")
c.Response().Header.Add("multi", "two")
c.Response().Header.Set(HeaderContentType, "application/json")

require.Equal(t, map[string][]string{
"Content-Type": {"application/json"},
"Foo": {"bar"},
"Multi": {"one", "two"},
"Test": {"Hello, World πŸ‘‹!"},
}, c.GetRespHeaders())
}

func Benchmark_Ctx_GetRespHeaders(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})

c.Response().Header.Set("test", "Hello, World πŸ‘‹!")
c.Response().Header.Set("foo", "bar")
c.Response().Header.Set(HeaderContentType, "application/json")

b.ReportAllocs()
b.ResetTimer()

var headers map[string][]string
for n := 0; n < b.N; n++ {
headers = c.GetRespHeaders()
}

require.Equal(b, map[string][]string{
"Content-Type": {"application/json"},
"Foo": {"bar"},
"Test": {"Hello, World πŸ‘‹!"},
}, headers)
}

// go test -run Test_Ctx_GetReqHeaders
func Test_Ctx_GetReqHeaders(t *testing.T) {
t.Parallel()
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})

c.Request().Header.Set("test", "Hello, World πŸ‘‹!")
c.Request().Header.Set("foo", "bar")
c.Request().Header.Set("multi", "one")
c.Request().Header.Add("multi", "two")
c.Request().Header.Set(HeaderContentType, "application/json")

require.Equal(t, map[string][]string{
"Content-Type": {"application/json"},
"Foo": {"bar"},
"Test": {"Hello, World πŸ‘‹!"},
"Multi": {"one", "two"},
}, c.GetReqHeaders())
}

func Benchmark_Ctx_GetReqHeaders(b *testing.B) {
app := New()
c := app.NewCtx(&fasthttp.RequestCtx{})

c.Request().Header.Set("test", "Hello, World πŸ‘‹!")
c.Request().Header.Set("foo", "bar")
c.Request().Header.Set(HeaderContentType, "application/json")

b.ReportAllocs()
b.ResetTimer()

var headers map[string][]string
for n := 0; n < b.N; n++ {
headers = c.GetReqHeaders()
}

require.Equal(b, map[string][]string{
"Content-Type": {"application/json"},
"Foo": {"bar"},
"Test": {"Hello, World πŸ‘‹!"},
}, headers)
}

0 comments on commit 9b0a99b

Please sign in to comment.