mirror of https://github.com/gofiber/fiber.git
🔥 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
parent
926c537252
commit
9b0a99ba27
24
ctx.go
24
ctx.go
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
86
ctx_test.go
86
ctx_test.go
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue