🔥 v3 (feature): Adding GetReqHeaders and GetRespHeaders (#2831)

* 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>
pull/2835/head
Bruno 2024-02-05 06:11:29 -03:00 committed by GitHub
parent 926c537252
commit 9b0a99ba27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 120 additions and 0 deletions

24
ctx.go
View File

@ -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.

View File

@ -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.

View File

@ -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)
}