2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-10-09 15:26:50 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2021-01-12 18:08:50 +08:00
|
|
|
"bytes"
|
2020-03-25 17:13:05 +08:00
|
|
|
"context"
|
2019-10-09 15:26:50 +08:00
|
|
|
"fmt"
|
2021-01-12 18:08:50 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-10-09 15:26:50 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-02 19:12:01 +08:00
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
2021-01-17 18:05:29 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
2020-07-02 19:12:01 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/util/guid"
|
2019-10-09 15:26:50 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/net/ghttp"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Client_Basic(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-10-09 15:26:50 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/hello", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("hello")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-10-09 15:26:50 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-09 15:26:50 +08:00
|
|
|
url := fmt.Sprintf("http://127.0.0.1:%d", p)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-10-09 15:26:50 +08:00
|
|
|
client.SetPrefix(url)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(ghttp.GetContent(""), ``)
|
|
|
|
t.Assert(client.GetContent("/hello"), `hello`)
|
2019-10-09 15:26:50 +08:00
|
|
|
|
|
|
|
_, err := ghttp.Post("")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:36:49 +08:00
|
|
|
func Test_Client_BasicAuth(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-29 19:36:49 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/auth", func(r *ghttp.Request) {
|
|
|
|
if r.BasicAuth("admin", "admin") {
|
|
|
|
r.Response.Write("1")
|
|
|
|
} else {
|
|
|
|
r.Response.Write("0")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
t.Assert(c.BasicAuth("admin", "123").GetContent("/auth"), "0")
|
|
|
|
t.Assert(c.BasicAuth("admin", "admin").GetContent("/auth"), "1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
func Test_Client_Cookie(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-19 22:56:12 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/cookie", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Cookie.Get("test"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-19 22:56:12 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
c.SetCookie("test", "0123456789")
|
|
|
|
t.Assert(c.PostContent("/cookie"), "0123456789")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:46:52 +08:00
|
|
|
func Test_Client_MapParam(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/map", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("test"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-04-04 22:46:52 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
t.Assert(c.GetContent("/map", g.Map{"test": "1234567890"}), "1234567890")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
func Test_Client_Cookies(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-19 22:56:12 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/cookie", func(r *ghttp.Request) {
|
|
|
|
r.Cookie.Set("test1", "1")
|
|
|
|
r.Cookie.Set("test2", "2")
|
|
|
|
r.Response.Write("ok")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-19 22:56:12 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
resp, err := c.Get("/cookie")
|
|
|
|
t.Assert(err, nil)
|
|
|
|
defer resp.Close()
|
|
|
|
|
|
|
|
t.AssertNE(resp.Header.Get("Set-Cookie"), "")
|
|
|
|
|
|
|
|
m := resp.GetCookieMap()
|
|
|
|
t.Assert(len(m), 2)
|
|
|
|
t.Assert(m["test1"], 1)
|
|
|
|
t.Assert(m["test2"], 2)
|
|
|
|
t.Assert(resp.GetCookie("test1"), 1)
|
|
|
|
t.Assert(resp.GetCookie("test2"), 2)
|
2019-10-09 15:26:50 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-25 17:13:05 +08:00
|
|
|
|
|
|
|
func Test_Client_Chain_Header(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-25 17:13:05 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/header1", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Header.Get("test1"))
|
|
|
|
})
|
|
|
|
s.BindHandler("/header2", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Header.Get("test2"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-25 17:13:05 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
t.Assert(c.Header(g.MapStrStr{"test1": "1234567890"}).GetContent("/header1"), "1234567890")
|
|
|
|
t.Assert(c.HeaderRaw("test1: 1234567890\ntest2: abcdefg").GetContent("/header1"), "1234567890")
|
|
|
|
t.Assert(c.HeaderRaw("test1: 1234567890\ntest2: abcdefg").GetContent("/header2"), "abcdefg")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Client_Chain_Context(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-25 17:13:05 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/context", func(r *ghttp.Request) {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
r.Response.Write("ok")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-25 17:13:05 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
t.Assert(c.Ctx(ctx).GetContent("/context"), "")
|
|
|
|
|
|
|
|
ctx, _ = context.WithTimeout(context.Background(), 2000*time.Millisecond)
|
|
|
|
t.Assert(c.Ctx(ctx).GetContent("/context"), "ok")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Client_Chain_Timeout(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-25 17:13:05 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/timeout", func(r *ghttp.Request) {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
r.Response.Write("ok")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-25 17:13:05 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
t.Assert(c.Timeout(100*time.Millisecond).GetContent("/timeout"), "")
|
|
|
|
t.Assert(c.Timeout(2000*time.Millisecond).GetContent("/timeout"), "ok")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Client_Chain_ContentJson(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-25 17:13:05 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/json", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("name"), r.Get("score"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-25 17:13:05 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
t.Assert(c.ContentJson().PostContent("/json", g.Map{
|
|
|
|
"name": "john",
|
|
|
|
"score": 100,
|
|
|
|
}), "john100")
|
|
|
|
t.Assert(c.ContentJson().PostContent("/json", `{"name":"john", "score":100}`), "john100")
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Score int `json:"score"`
|
|
|
|
}
|
|
|
|
t.Assert(c.ContentJson().PostContent("/json", User{"john", 100}), "john100")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Client_Chain_ContentXml(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-25 17:13:05 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/xml", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("name"), r.Get("score"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-03-25 17:13:05 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
t.Assert(c.ContentXml().PostContent("/xml", g.Map{
|
|
|
|
"name": "john",
|
|
|
|
"score": 100,
|
|
|
|
}), "john100")
|
|
|
|
t.Assert(c.ContentXml().PostContent("/xml", `{"name":"john", "score":100}`), "john100")
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Score int `json:"score"`
|
|
|
|
}
|
|
|
|
t.Assert(c.ContentXml().PostContent("/xml", User{"john", 100}), "john100")
|
|
|
|
})
|
|
|
|
}
|
2020-06-28 23:23:09 +08:00
|
|
|
|
|
|
|
func Test_Client_Param_Containing_Special_Char(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("k1=", r.Get("k1"), "&k2=", r.Get("k2"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c := g.Client()
|
2020-06-28 23:23:09 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
t.Assert(c.PostContent("/", "k1=MTIxMg==&k2=100"), "k1=MTIxMg==&k2=100")
|
|
|
|
t.Assert(c.PostContent("/", g.Map{
|
|
|
|
"k1": "MTIxMg==",
|
|
|
|
"k2": 100,
|
|
|
|
}), "k1=MTIxMg==&k2=100")
|
|
|
|
})
|
|
|
|
}
|
2020-07-02 19:12:01 +08:00
|
|
|
|
2020-10-26 20:21:09 +08:00
|
|
|
// It posts data along with file uploading.
|
|
|
|
// It does not url-encodes the parameters.
|
2020-07-02 19:12:01 +08:00
|
|
|
func Test_Client_File_And_Param(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
tmpPath := gfile.TempDir(guid.S())
|
|
|
|
err := gfile.Mkdir(tmpPath)
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
defer gfile.Remove(tmpPath)
|
|
|
|
|
|
|
|
file := r.GetUploadFile("file")
|
|
|
|
_, err = file.Save(tmpPath)
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
r.Response.Write(
|
2020-10-26 20:21:09 +08:00
|
|
|
r.Get("json"),
|
2020-07-02 19:12:01 +08:00
|
|
|
gfile.GetContents(gfile.Join(tmpPath, gfile.Basename(file.Filename))),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-10-26 20:21:09 +08:00
|
|
|
path := gdebug.TestDataPath("upload", "file1.txt")
|
|
|
|
data := g.Map{
|
|
|
|
"file": "@file:" + path,
|
|
|
|
"json": `{"uuid": "luijquiopm", "isRelative": false, "fileName": "test111.xls"}`,
|
|
|
|
}
|
2020-07-02 19:12:01 +08:00
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2020-10-26 20:21:09 +08:00
|
|
|
t.Assert(c.PostContent("/", data), data["json"].(string)+gfile.GetContents(path))
|
2020-07-02 19:12:01 +08:00
|
|
|
})
|
|
|
|
}
|
2021-01-12 18:08:50 +08:00
|
|
|
|
|
|
|
func Test_Client_Middleware(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
isServerHandler := false
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
isServerHandler = true
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-01-22 01:06:44 +08:00
|
|
|
var (
|
|
|
|
str1 = ""
|
|
|
|
str2 = "resp body"
|
|
|
|
)
|
|
|
|
c := g.Client().SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
c.Use(func(c *ghttp.Client, r *http.Request) (resp *ghttp.ClientResponse, err error) {
|
|
|
|
str1 += "a"
|
2021-01-25 14:54:38 +08:00
|
|
|
resp, err = c.Next(r)
|
2021-01-22 01:06:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
str1 += "b"
|
2021-01-12 18:08:50 +08:00
|
|
|
return
|
2021-01-22 01:06:44 +08:00
|
|
|
})
|
|
|
|
c.Use(func(c *ghttp.Client, r *http.Request) (resp *ghttp.ClientResponse, err error) {
|
|
|
|
str1 += "c"
|
2021-01-25 14:54:38 +08:00
|
|
|
resp, err = c.Next(r)
|
2021-01-22 01:06:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
str1 += "d"
|
2021-01-12 18:08:50 +08:00
|
|
|
return
|
2021-01-22 01:06:44 +08:00
|
|
|
})
|
|
|
|
c.Use(func(c *ghttp.Client, r *http.Request) (resp *ghttp.ClientResponse, err error) {
|
|
|
|
str1 += "e"
|
2021-01-25 14:54:38 +08:00
|
|
|
resp, err = c.Next(r)
|
2021-01-22 01:06:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-12 18:08:50 +08:00
|
|
|
resp.Response.Body = ioutil.NopCloser(bytes.NewBufferString(str2))
|
2021-01-22 01:06:44 +08:00
|
|
|
str1 += "f"
|
2021-01-12 18:08:50 +08:00
|
|
|
return
|
|
|
|
})
|
|
|
|
resp, err := c.Get("/")
|
2021-01-22 01:06:44 +08:00
|
|
|
t.Assert(str1, "acefdb")
|
2021-01-12 18:08:50 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp.ReadAllString(), str2)
|
|
|
|
t.Assert(isServerHandler, true)
|
|
|
|
|
|
|
|
// test abort, abort will not send
|
2021-01-22 01:06:44 +08:00
|
|
|
var (
|
|
|
|
str3 = ""
|
|
|
|
abortStr = "abort request"
|
|
|
|
)
|
|
|
|
|
|
|
|
c = g.Client().SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
c.Use(func(c *ghttp.Client, r *http.Request) (resp *ghttp.ClientResponse, err error) {
|
2021-01-12 18:08:50 +08:00
|
|
|
str3 += "a"
|
2021-01-25 14:54:38 +08:00
|
|
|
resp, err = c.Next(r)
|
2021-01-12 18:08:50 +08:00
|
|
|
str3 += "b"
|
|
|
|
return
|
2021-01-22 01:06:44 +08:00
|
|
|
})
|
|
|
|
c.Use(func(c *ghttp.Client, r *http.Request) (*ghttp.ClientResponse, error) {
|
2021-01-12 18:08:50 +08:00
|
|
|
str3 += "c"
|
2021-01-17 18:05:29 +08:00
|
|
|
return nil, gerror.New(abortStr)
|
2021-01-22 01:06:44 +08:00
|
|
|
})
|
|
|
|
c.Use(func(c *ghttp.Client, r *http.Request) (resp *ghttp.ClientResponse, err error) {
|
2021-01-12 18:08:50 +08:00
|
|
|
str3 += "f"
|
2021-01-25 14:54:38 +08:00
|
|
|
resp, err = c.Next(r)
|
2021-01-12 18:08:50 +08:00
|
|
|
str3 += "g"
|
|
|
|
return
|
|
|
|
})
|
|
|
|
resp, err = c.Get("/")
|
2021-01-22 01:06:44 +08:00
|
|
|
t.Assert(err, abortStr)
|
2021-01-17 18:05:29 +08:00
|
|
|
t.Assert(str3, "acb")
|
2021-01-12 18:08:50 +08:00
|
|
|
t.Assert(resp, nil)
|
|
|
|
})
|
2021-01-12 19:09:45 +08:00
|
|
|
}
|
2021-01-30 23:05:02 +08:00
|
|
|
|
|
|
|
func Test_Client_Agent(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.UserAgent())
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client().SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
c.SetAgent("test")
|
|
|
|
t.Assert(c.GetContent("/"), "test")
|
|
|
|
})
|
|
|
|
}
|