add function GetHeader for ghttp.Request

This commit is contained in:
john
2020-07-21 10:17:31 +08:00
parent ee5ddaab52
commit 8167a398fc
2 changed files with 26 additions and 0 deletions

View File

@ -126,6 +126,11 @@ func (r *Request) IsExited() bool {
return r.exit
}
// GetHeader retrieves and returns the header value with given <key>.
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 {

View File

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