diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index 5ea1c38c9..aab54b884 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -126,6 +126,11 @@ func (r *Request) IsExited() bool { return r.exit } +// GetHeader retrieves and returns the header value with given . +func (r *Request) GetHeader(key string) string { + return r.Header.Get(key) +} + // GetHost returns current request host name, which might be a domain or an IP without port. func (r *Request) GetHost() string { if len(r.parsedHost) == 0 { diff --git a/net/ghttp/ghttp_unit_param_test.go b/net/ghttp/ghttp_unit_param_test.go index 6e69e06ba..f345cd42a 100644 --- a/net/ghttp/ghttp_unit_param_test.go +++ b/net/ghttp/ghttp_unit_param_test.go @@ -404,6 +404,27 @@ func Test_Params_Basic(t *testing.T) { }) } +func Test_Params_Header(t *testing.T) { + p, _ := ports.PopRand() + s := g.Server(p) + s.BindHandler("/header", func(r *ghttp.Request) { + r.Response.Write(r.GetHeader("test")) + }) + s.SetPort(p) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + prefix := fmt.Sprintf("http://127.0.0.1:%d", p) + client := ghttp.NewClient() + client.SetPrefix(prefix) + + t.Assert(client.Header(g.MapStrStr{"test": "123456"}).GetContent("/header"), "123456") + }) +} + func Test_Params_SupportChars(t *testing.T) { p, _ := ports.PopRand() s := g.Server(p)