mirror of https://github.com/gofiber/fiber.git
🍀 Cleanup
parent
e93306ca61
commit
6cc4a5ec04
144
client.go
144
client.go
|
@ -151,6 +151,8 @@ func addMissingPort(addr string, isTLS bool) string {
|
|||
return net.JoinHostPort(addr, strconv.Itoa(port))
|
||||
}
|
||||
|
||||
/************************** Header Setting **************************/
|
||||
|
||||
// Set sets the given 'key: value' header.
|
||||
//
|
||||
// Use Add for setting multiple header values under the same key.
|
||||
|
@ -170,13 +172,6 @@ func (a *Agent) Add(k, v string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// Host sets host for the uri.
|
||||
func (a *Agent) Host(host string) *Agent {
|
||||
a.req.URI().SetHost(host)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// ConnectionClose sets 'Connection: close' header.
|
||||
func (a *Agent) ConnectionClose() *Agent {
|
||||
a.req.Header.SetConnectionClose()
|
||||
|
@ -191,16 +186,6 @@ func (a *Agent) UserAgent(userAgent string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// Debug mode enables logging request and response detail
|
||||
func (a *Agent) Debug(w ...io.Writer) *Agent {
|
||||
a.debugWriter = os.Stdout
|
||||
if len(w) > 0 {
|
||||
a.debugWriter = w[0]
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Cookie sets one 'key: value' cookie.
|
||||
func (a *Agent) Cookie(key, value string) *Agent {
|
||||
a.req.Header.SetCookie(key, value)
|
||||
|
@ -217,9 +202,69 @@ func (a *Agent) Cookies(kv ...string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// Timeout sets request timeout duration.
|
||||
func (a *Agent) Timeout(timeout time.Duration) *Agent {
|
||||
a.timeout = timeout
|
||||
// Referer sets Referer header value.
|
||||
func (a *Agent) Referer(referer string) *Agent {
|
||||
a.req.Header.SetReferer(referer)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// ContentType sets Content-Type header value.
|
||||
func (a *Agent) ContentType(contentType string) *Agent {
|
||||
a.req.Header.SetContentType(contentType)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
/************************** End Header Setting **************************/
|
||||
|
||||
/************************** URI Setting **************************/
|
||||
|
||||
// Host sets host for the uri.
|
||||
func (a *Agent) Host(host string) *Agent {
|
||||
a.req.URI().SetHost(host)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// QueryString sets URI query string.
|
||||
func (a *Agent) QueryString(queryString string) *Agent {
|
||||
a.req.URI().SetQueryString(queryString)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
/************************** End URI Setting **************************/
|
||||
|
||||
/************************** Request Setting **************************/
|
||||
|
||||
// BodyString sets request body.
|
||||
func (a *Agent) BodyString(bodyString string) *Agent {
|
||||
a.req.SetBodyString(bodyString)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// BodyStream sets request body stream and, optionally body size.
|
||||
//
|
||||
// If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
|
||||
// before returning io.EOF.
|
||||
//
|
||||
// If bodySize < 0, then bodyStream is read until io.EOF.
|
||||
//
|
||||
// bodyStream.Close() is called after finishing reading all body data
|
||||
// if it implements io.Closer.
|
||||
//
|
||||
// Note that GET and HEAD requests cannot have body.
|
||||
func (a *Agent) BodyStream(bodyStream io.Reader, bodySize int) *Agent {
|
||||
a.req.SetBodyStream(bodyStream, bodySize)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Request sets custom request for createAgent.
|
||||
func (a *Agent) Request(req *Request) *Agent {
|
||||
a.customReq = req
|
||||
|
||||
return a
|
||||
}
|
||||
|
@ -252,33 +297,23 @@ func (a *Agent) Form(args *Args) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// QueryString sets URI query string.
|
||||
func (a *Agent) QueryString(queryString string) *Agent {
|
||||
a.req.URI().SetQueryString(queryString)
|
||||
/************************** End Request Setting **************************/
|
||||
|
||||
/************************** Agent Setting **************************/
|
||||
|
||||
// Debug mode enables logging request and response detail
|
||||
func (a *Agent) Debug(w ...io.Writer) *Agent {
|
||||
a.debugWriter = os.Stdout
|
||||
if len(w) > 0 {
|
||||
a.debugWriter = w[0]
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// BodyString sets request body.
|
||||
func (a *Agent) BodyString(bodyString string) *Agent {
|
||||
a.req.SetBodyString(bodyString)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// BodyStream sets request body stream and, optionally body size.
|
||||
//
|
||||
// If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
|
||||
// before returning io.EOF.
|
||||
//
|
||||
// If bodySize < 0, then bodyStream is read until io.EOF.
|
||||
//
|
||||
// bodyStream.Close() is called after finishing reading all body data
|
||||
// if it implements io.Closer.
|
||||
//
|
||||
// Note that GET and HEAD requests cannot have body.
|
||||
func (a *Agent) BodyStream(bodyStream io.Reader, bodySize int) *Agent {
|
||||
a.req.SetBodyStream(bodyStream, bodySize)
|
||||
// Timeout sets request timeout duration.
|
||||
func (a *Agent) Timeout(timeout time.Duration) *Agent {
|
||||
a.timeout = timeout
|
||||
|
||||
return a
|
||||
}
|
||||
|
@ -309,27 +344,6 @@ func (a *Agent) TLSConfig(config *tls.Config) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// Request sets custom request for createAgent.
|
||||
func (a *Agent) Request(req *Request) *Agent {
|
||||
a.customReq = req
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Referer sets Referer header value.
|
||||
func (a *Agent) Referer(referer string) *Agent {
|
||||
a.req.Header.SetReferer(referer)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// ContentType sets Content-Type header value.
|
||||
func (a *Agent) ContentType(contentType string) *Agent {
|
||||
a.req.Header.SetContentType(contentType)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// MaxRedirectsCount sets max redirect count for GET and HEAD.
|
||||
func (a *Agent) MaxRedirectsCount(count int) *Agent {
|
||||
a.maxRedirectsCount = count
|
||||
|
@ -337,6 +351,8 @@ func (a *Agent) MaxRedirectsCount(count int) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
/************************** End Agent Setting **************************/
|
||||
|
||||
// Bytes returns the status code, bytes body and errors of url.
|
||||
func (a *Agent) Bytes(customResp ...*Response) (code int, body []byte, errs []error) {
|
||||
defer a.release()
|
||||
|
|
506
client_test.go
506
client_test.go
|
@ -154,6 +154,92 @@ func Test_Client_UserAgent(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Headers(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
c.Request().Header.VisitAll(func(key, value []byte) {
|
||||
if k := string(key); k == "K1" || k == "K2" {
|
||||
_, _ = c.Write(key)
|
||||
_, _ = c.Write(value)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Set("k1", "v1").
|
||||
Add("k1", "v11").
|
||||
Set("k2", "v2")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "K1v1K1v11K2v2")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Connection_Close(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
if c.Request().Header.ConnectionClose() {
|
||||
return c.SendString("close")
|
||||
}
|
||||
return c.SendString("not close")
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.ConnectionClose()
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "close")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_UserAgent(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Header.UserAgent())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.UserAgent("ua")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "ua")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Cookie(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.SendString(
|
||||
c.Cookies("k1") + c.Cookies("k2") + c.Cookies("k3") + c.Cookies("k4"))
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Cookie("k1", "v1").
|
||||
Cookie("k2", "v2").
|
||||
Cookies("k3", "v3", "k4", "v4")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "v1v2v3v4")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Referer(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Header.Referer())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Referer("http://referer.com")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "http://referer.com")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_ContentType(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Header.ContentType())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.ContentType("custom-type")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "custom-type")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Specific_Host(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -181,65 +267,6 @@ func Test_Client_Agent_Specific_Host(t *testing.T) {
|
|||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Headers(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
c.Request().Header.VisitAll(func(key, value []byte) {
|
||||
if k := string(key); k == "K1" || k == "K2" {
|
||||
_, _ = c.Write(key)
|
||||
_, _ = c.Write(value)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Set("k1", "v1").
|
||||
Add("k1", "v11").
|
||||
Set("k2", "v2")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "K1v1K1v11K2v2")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_UserAgent(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Header.UserAgent())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.UserAgent("ua")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "ua")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Connection_Close(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
if c.Request().Header.ConnectionClose() {
|
||||
return c.SendString("close")
|
||||
}
|
||||
return c.SendString("not close")
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.ConnectionClose()
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "close")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Referer(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Header.Referer())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Referer("http://referer.com")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "http://referer.com")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_QueryString(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().URI().QueryString())
|
||||
|
@ -264,33 +291,6 @@ func Test_Client_Agent_BodyString(t *testing.T) {
|
|||
testAgent(t, handler, wrapAgent, "foo=bar&bar=baz")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Cookie(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.SendString(
|
||||
c.Cookies("k1") + c.Cookies("k2") + c.Cookies("k3") + c.Cookies("k4"))
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Cookie("k1", "v1").
|
||||
Cookie("k2", "v2").
|
||||
Cookies("k3", "v3", "k4", "v4")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "v1v2v3v4")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_ContentType(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Header.ContentType())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.ContentType("custom-type")
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "custom-type")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_BodyStream(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Body())
|
||||
|
@ -303,28 +303,42 @@ func Test_Client_Agent_BodyStream(t *testing.T) {
|
|||
testAgent(t, handler, wrapAgent, "body stream")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Form(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
utils.AssertEqual(t, MIMEApplicationForm, string(c.Request().Header.ContentType()))
|
||||
func Test_Client_Agent_Custom_Request_And_Response(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
return c.Send(c.Request().Body())
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString("custom")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
a := AcquireAgent()
|
||||
req := AcquireRequest()
|
||||
resp := AcquireResponse()
|
||||
|
||||
req.Header.SetMethod(MethodGet)
|
||||
req.SetRequestURI("http://example.com")
|
||||
a.Request(req)
|
||||
|
||||
utils.AssertEqual(t, nil, a.Parse())
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String(resp)
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "custom", body)
|
||||
utils.AssertEqual(t, "custom", string(resp.Body()))
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
ReleaseRequest(req)
|
||||
ReleaseResponse(resp)
|
||||
}
|
||||
|
||||
args := AcquireArgs()
|
||||
|
||||
args.Set("a", "b")
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Form(args)
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "a=b")
|
||||
|
||||
ReleaseArgs(args)
|
||||
}
|
||||
|
||||
type jsonData struct {
|
||||
F string `json:"f"`
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Json(t *testing.T) {
|
||||
|
@ -352,6 +366,30 @@ func Test_Client_Agent_Json_Error(t *testing.T) {
|
|||
utils.AssertEqual(t, "json: unsupported type: complex128", errs[0].Error())
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Form(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
utils.AssertEqual(t, MIMEApplicationForm, string(c.Request().Header.ContentType()))
|
||||
|
||||
return c.Send(c.Request().Body())
|
||||
}
|
||||
|
||||
args := AcquireArgs()
|
||||
|
||||
args.Set("a", "b")
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Form(args)
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "a=b")
|
||||
|
||||
ReleaseArgs(args)
|
||||
}
|
||||
|
||||
type jsonData struct {
|
||||
F string `json:"f"`
|
||||
}
|
||||
|
||||
func Test_Client_Debug(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.SendString("debug")
|
||||
|
@ -375,37 +413,6 @@ func Test_Client_Debug(t *testing.T) {
|
|||
utils.AssertEqual(t, true, strings.Contains(str, "Content-Type: text/plain; charset=utf-8\r\nContent-Length: 5\r\n\r\ndebug"))
|
||||
}
|
||||
|
||||
func testAgent(t *testing.T, handler Handler, wrapAgent func(agent *Agent), excepted string, count ...int) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", handler)
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
c := 1
|
||||
if len(count) > 0 {
|
||||
c = count[0]
|
||||
}
|
||||
|
||||
for i := 0; i < c; i++ {
|
||||
a := Get("http://example.com")
|
||||
|
||||
wrapAgent(a)
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, excepted, body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Timeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -432,6 +439,76 @@ func Test_Client_Agent_Timeout(t *testing.T) {
|
|||
utils.AssertEqual(t, "timeout", errs[0].Error())
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Reuse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString("reuse")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
a := Get("http://example.com").
|
||||
Reuse()
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "reuse", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
code, body, errs = a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "reuse", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
|
||||
func Test_Client_Agent_TLS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Create tls certificate
|
||||
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
config := &tls.Config{
|
||||
Certificates: []tls.Certificate{cer},
|
||||
}
|
||||
|
||||
ln, err := net.Listen(NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln = tls.NewListener(ln, config)
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString("tls")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
code, body, errs := Get("https://" + ln.Addr().String()).
|
||||
InsecureSkipVerify().
|
||||
TLSConfig(config).
|
||||
InsecureSkipVerify().
|
||||
String()
|
||||
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "tls", body)
|
||||
}
|
||||
|
||||
type data struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func Test_Client_Agent_MaxRedirectsCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -478,122 +555,6 @@ func Test_Client_Agent_MaxRedirectsCount(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Custom(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString("custom")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
a := AcquireAgent()
|
||||
req := AcquireRequest()
|
||||
resp := AcquireResponse()
|
||||
|
||||
req.Header.SetMethod(MethodGet)
|
||||
req.SetRequestURI("http://example.com")
|
||||
a.Request(req)
|
||||
|
||||
utils.AssertEqual(t, nil, a.Parse())
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String(resp)
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "custom", body)
|
||||
utils.AssertEqual(t, "custom", string(resp.Body()))
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
ReleaseRequest(req)
|
||||
ReleaseResponse(resp)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Reuse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString("reuse")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
a := Get("http://example.com").
|
||||
Reuse()
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "reuse", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
code, body, errs = a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "reuse", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Parse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
a := Get("https://example.com:10443")
|
||||
|
||||
utils.AssertEqual(t, nil, a.Parse())
|
||||
}
|
||||
|
||||
func Test_Client_Agent_TLS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Create tls certificate
|
||||
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
config := &tls.Config{
|
||||
Certificates: []tls.Certificate{cer},
|
||||
}
|
||||
|
||||
ln, err := net.Listen(NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln = tls.NewListener(ln, config)
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString("tls")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
code, body, errs := Get("https://" + ln.Addr().String()).
|
||||
InsecureSkipVerify().
|
||||
TLSConfig(config).
|
||||
InsecureSkipVerify().
|
||||
String()
|
||||
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, "tls", body)
|
||||
}
|
||||
|
||||
type data struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Struct(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -642,7 +603,46 @@ func Test_Client_Agent_Struct(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Parse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
a := Get("https://example.com:10443")
|
||||
|
||||
utils.AssertEqual(t, nil, a.Parse())
|
||||
}
|
||||
|
||||
func Test_AddMissingPort_TLS(t *testing.T) {
|
||||
addr := addMissingPort("example.com", true)
|
||||
utils.AssertEqual(t, "example.com:443", addr)
|
||||
}
|
||||
|
||||
func testAgent(t *testing.T, handler Handler, wrapAgent func(agent *Agent), excepted string, count ...int) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", handler)
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
c := 1
|
||||
if len(count) > 0 {
|
||||
c = count[0]
|
||||
}
|
||||
|
||||
for i := 0; i < c; i++ {
|
||||
a := Get("http://example.com")
|
||||
|
||||
wrapAgent(a)
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusOK, code)
|
||||
utils.AssertEqual(t, excepted, body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue