From 10c6dd2f3122c126eb193af91b34a4939fdf34b8 Mon Sep 17 00:00:00 2001 From: brunodmartins Date: Sun, 4 Feb 2024 22:53:13 -0300 Subject: [PATCH 1/3] Adding GetRespHeaders from v2 Signed-off-by: brunodmartins --- ctx.go | 12 ++++++++++++ ctx_interface.go | 5 +++++ ctx_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/ctx.go b/ctx.go index b292ea7b42..78ee8127f4 100644 --- a/ctx.go +++ b/ctx.go @@ -560,6 +560,18 @@ 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 +} + // 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. diff --git a/ctx_interface.go b/ctx_interface.go index ae8b171238..be2ff27ccc 100644 --- a/ctx_interface.go +++ b/ctx_interface.go @@ -134,6 +134,11 @@ 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 + // 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. diff --git a/ctx_test.go b/ctx_test.go index eb66dfae96..c7dbd4e542 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -4853,3 +4853,46 @@ 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, c.GetRespHeaders(), map[string][]string{ + "Content-Type": {"application/json"}, + "Foo": {"bar"}, + "Multi": {"one", "two"}, + "Test": {"Hello, World 👋!"}, + }) +} + +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, headers, map[string][]string{ + "Content-Type": {"application/json"}, + "Foo": {"bar"}, + "Test": {"Hello, World 👋!"}, + }) +} From 1fa8c5127486b96e393fa8fc133b876174f97184 Mon Sep 17 00:00:00 2001 From: brunodmartins Date: Sun, 4 Feb 2024 22:59:59 -0300 Subject: [PATCH 2/3] Adding GetReqHeaders from v2 Signed-off-by: brunodmartins --- ctx.go | 12 ++++++++++++ ctx_interface.go | 5 +++++ ctx_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/ctx.go b/ctx.go index 78ee8127f4..9257825e7b 100644 --- a/ctx.go +++ b/ctx.go @@ -572,6 +572,18 @@ func (c *DefaultCtx) GetRespHeaders() map[string][]string { 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. diff --git a/ctx_interface.go b/ctx_interface.go index be2ff27ccc..799be5601f 100644 --- a/ctx_interface.go +++ b/ctx_interface.go @@ -139,6 +139,11 @@ type Ctx interface { // 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. diff --git a/ctx_test.go b/ctx_test.go index c7dbd4e542..a04db14a9a 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -4896,3 +4896,46 @@ func Benchmark_Ctx_GetRespHeaders(b *testing.B) { "Test": {"Hello, World 👋!"}, }) } + +// 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, c.GetReqHeaders(), map[string][]string{ + "Content-Type": {"application/json"}, + "Foo": {"bar"}, + "Test": {"Hello, World 👋!"}, + "Multi": {"one", "two"}, + }) +} + +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, headers, map[string][]string{ + "Content-Type": {"application/json"}, + "Foo": {"bar"}, + "Test": {"Hello, World 👋!"}, + }) +} From e2d9fb8d678db1f30f4993ddd32673839c688efc Mon Sep 17 00:00:00 2001 From: brunodmartins Date: Sun, 4 Feb 2024 23:06:43 -0300 Subject: [PATCH 3/3] Fixed linter on tests Signed-off-by: brunodmartins --- ctx_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ctx_test.go b/ctx_test.go index a04db14a9a..df896920e3 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -4866,12 +4866,12 @@ func Test_Ctx_GetRespHeaders(t *testing.T) { c.Response().Header.Add("multi", "two") c.Response().Header.Set(HeaderContentType, "application/json") - require.Equal(t, c.GetRespHeaders(), map[string][]string{ + 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) { @@ -4890,11 +4890,11 @@ func Benchmark_Ctx_GetRespHeaders(b *testing.B) { headers = c.GetRespHeaders() } - require.Equal(b, headers, map[string][]string{ + require.Equal(b, map[string][]string{ "Content-Type": {"application/json"}, "Foo": {"bar"}, "Test": {"Hello, World 👋!"}, - }) + }, headers) } // go test -run Test_Ctx_GetReqHeaders @@ -4909,12 +4909,12 @@ func Test_Ctx_GetReqHeaders(t *testing.T) { c.Request().Header.Add("multi", "two") c.Request().Header.Set(HeaderContentType, "application/json") - require.Equal(t, c.GetReqHeaders(), map[string][]string{ + 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) { @@ -4933,9 +4933,9 @@ func Benchmark_Ctx_GetReqHeaders(b *testing.B) { headers = c.GetReqHeaders() } - require.Equal(b, headers, map[string][]string{ + require.Equal(b, map[string][]string{ "Content-Type": {"application/json"}, "Foo": {"bar"}, "Test": {"Hello, World 👋!"}, - }) + }, headers) }