mirror of
https://gitee.com/johng/gf
synced 2026-06-28 02:06:20 +08:00
add function GetHeader for ghttp.Request
This commit is contained in:
@ -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 {
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user