mirror of https://github.com/gofiber/fiber.git
👷 Add Bytes functions
parent
a60d23343c
commit
62d311133b
130
client.go
130
client.go
|
@ -207,6 +207,33 @@ func (a *Agent) Set(k, v string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// SetBytesK sets the given 'key: value' header.
|
||||
//
|
||||
// Use AddBytesK for setting multiple header values under the same key.
|
||||
func (a *Agent) SetBytesK(k []byte, v string) *Agent {
|
||||
a.req.Header.SetBytesK(k, v)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// SetBytesV sets the given 'key: value' header.
|
||||
//
|
||||
// Use AddBytesV for setting multiple header values under the same key.
|
||||
func (a *Agent) SetBytesV(k string, v []byte) *Agent {
|
||||
a.req.Header.SetBytesV(k, v)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// SetBytesKV sets the given 'key: value' header.
|
||||
//
|
||||
// Use AddBytesKV for setting multiple header values under the same key.
|
||||
func (a *Agent) SetBytesKV(k []byte, v []byte) *Agent {
|
||||
a.req.Header.SetBytesKV(k, v)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Add adds the given 'key: value' header.
|
||||
//
|
||||
// Multiple headers with the same key may be added with this function.
|
||||
|
@ -217,6 +244,36 @@ func (a *Agent) Add(k, v string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// AddBytesK adds the given 'key: value' header.
|
||||
//
|
||||
// Multiple headers with the same key may be added with this function.
|
||||
// Use SetBytesK for setting a single header for the given key.
|
||||
func (a *Agent) AddBytesK(k []byte, v string) *Agent {
|
||||
a.req.Header.AddBytesK(k, v)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// AddBytesV adds the given 'key: value' header.
|
||||
//
|
||||
// Multiple headers with the same key may be added with this function.
|
||||
// Use SetBytesV for setting a single header for the given key.
|
||||
func (a *Agent) AddBytesV(k string, v []byte) *Agent {
|
||||
a.req.Header.AddBytesV(k, v)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// AddBytesKV adds the given 'key: value' header.
|
||||
//
|
||||
// Multiple headers with the same key may be added with this function.
|
||||
// Use SetBytesKV for setting a single header for the given key.
|
||||
func (a *Agent) AddBytesKV(k []byte, v []byte) *Agent {
|
||||
a.req.Header.AddBytesKV(k, v)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// ConnectionClose sets 'Connection: close' header.
|
||||
func (a *Agent) ConnectionClose() *Agent {
|
||||
a.req.Header.SetConnectionClose()
|
||||
|
@ -231,6 +288,13 @@ func (a *Agent) UserAgent(userAgent string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// UserAgentBytes sets User-Agent header value.
|
||||
func (a *Agent) UserAgentBytes(userAgent []byte) *Agent {
|
||||
a.req.Header.SetUserAgentBytes(userAgent)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Cookie sets one 'key: value' cookie.
|
||||
func (a *Agent) Cookie(key, value string) *Agent {
|
||||
a.req.Header.SetCookie(key, value)
|
||||
|
@ -238,6 +302,20 @@ func (a *Agent) Cookie(key, value string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// CookieBytesK sets one 'key: value' cookie.
|
||||
func (a *Agent) CookieBytesK(key []byte, value string) *Agent {
|
||||
a.req.Header.SetCookieBytesK(key, value)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// CookieBytesKV sets one 'key: value' cookie.
|
||||
func (a *Agent) CookieBytesKV(key, value []byte) *Agent {
|
||||
a.req.Header.SetCookieBytesKV(key, value)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Cookies sets multiple 'key: value' cookies.
|
||||
func (a *Agent) Cookies(kv ...string) *Agent {
|
||||
for i := 1; i < len(kv); i += 2 {
|
||||
|
@ -247,6 +325,15 @@ func (a *Agent) Cookies(kv ...string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// CookiesBytesKV sets multiple 'key: value' cookies.
|
||||
func (a *Agent) CookiesBytesKV(kv ...[]byte) *Agent {
|
||||
for i := 1; i < len(kv); i += 2 {
|
||||
a.req.Header.SetCookieBytesKV(kv[i-1], kv[i])
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Referer sets Referer header value.
|
||||
func (a *Agent) Referer(referer string) *Agent {
|
||||
a.req.Header.SetReferer(referer)
|
||||
|
@ -254,6 +341,13 @@ func (a *Agent) Referer(referer string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// RefererBytes sets Referer header value.
|
||||
func (a *Agent) RefererBytes(referer []byte) *Agent {
|
||||
a.req.Header.SetRefererBytes(referer)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// ContentType sets Content-Type header value.
|
||||
func (a *Agent) ContentType(contentType string) *Agent {
|
||||
a.req.Header.SetContentType(contentType)
|
||||
|
@ -261,6 +355,13 @@ func (a *Agent) ContentType(contentType string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// ContentTypeBytes sets Content-Type header value.
|
||||
func (a *Agent) ContentTypeBytes(contentType []byte) *Agent {
|
||||
a.req.Header.SetContentTypeBytes(contentType)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
/************************** End Header Setting **************************/
|
||||
|
||||
/************************** URI Setting **************************/
|
||||
|
@ -272,6 +373,13 @@ func (a *Agent) Host(host string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// HostBytes sets host for the uri.
|
||||
func (a *Agent) HostBytes(host []byte) *Agent {
|
||||
a.req.URI().SetHostBytes(host)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// QueryString sets URI query string.
|
||||
func (a *Agent) QueryString(queryString string) *Agent {
|
||||
a.req.URI().SetQueryString(queryString)
|
||||
|
@ -279,6 +387,13 @@ func (a *Agent) QueryString(queryString string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// QueryStringBytes sets URI query string.
|
||||
func (a *Agent) QueryStringBytes(queryString []byte) *Agent {
|
||||
a.req.URI().SetQueryStringBytes(queryString)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// BasicAuth sets URI username and password.
|
||||
func (a *Agent) BasicAuth(username, password string) *Agent {
|
||||
a.req.URI().SetUsername(username)
|
||||
|
@ -287,6 +402,14 @@ func (a *Agent) BasicAuth(username, password string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// BasicAuthBytes sets URI username and password.
|
||||
func (a *Agent) BasicAuthBytes(username, password []byte) *Agent {
|
||||
a.req.URI().SetUsernameBytes(username)
|
||||
a.req.URI().SetPasswordBytes(password)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
/************************** End URI Setting **************************/
|
||||
|
||||
/************************** Request Setting **************************/
|
||||
|
@ -298,6 +421,13 @@ func (a *Agent) BodyString(bodyString string) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// Body sets request body.
|
||||
func (a *Agent) Body(body []byte) *Agent {
|
||||
a.req.SetBody(body)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// BodyStream sets request body stream and, optionally body size.
|
||||
//
|
||||
// If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
|
||||
|
|
|
@ -283,7 +283,7 @@ func Test_Client_UserAgent(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Headers(t *testing.T) {
|
||||
func Test_Client_Agent_Set_Or_Add_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" {
|
||||
|
@ -296,11 +296,17 @@ func Test_Client_Agent_Headers(t *testing.T) {
|
|||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Set("k1", "v1").
|
||||
Add("k1", "v11").
|
||||
Set("k2", "v2")
|
||||
SetBytesK([]byte("k1"), "v1").
|
||||
SetBytesV("k1", []byte("v1")).
|
||||
AddBytesK([]byte("k1"), "v11").
|
||||
AddBytesV("k1", []byte("v22")).
|
||||
AddBytesKV([]byte("k1"), []byte("v33")).
|
||||
SetBytesKV([]byte("k2"), []byte("v2")).
|
||||
Add("k2", "v22")
|
||||
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "K1v1K1v11K2v2")
|
||||
testAgent(t, handler, wrapAgent, "K1v1K1v11K1v22K1v33K2v2K2v22")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Connection_Close(t *testing.T) {
|
||||
|
@ -324,7 +330,8 @@ func Test_Client_Agent_UserAgent(t *testing.T) {
|
|||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.UserAgent("ua")
|
||||
a.UserAgent("ua").
|
||||
UserAgentBytes([]byte("ua"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "ua")
|
||||
|
@ -338,8 +345,10 @@ func Test_Client_Agent_Cookie(t *testing.T) {
|
|||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Cookie("k1", "v1").
|
||||
Cookie("k2", "v2").
|
||||
Cookies("k3", "v3", "k4", "v4")
|
||||
CookieBytesK([]byte("k2"), "v2").
|
||||
CookieBytesKV([]byte("k2"), []byte("v2")).
|
||||
Cookies("k3", "v3", "k4", "v4").
|
||||
CookiesBytesKV([]byte("k3"), []byte("v3"), []byte("k4"), []byte("v4"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "v1v2v3v4")
|
||||
|
@ -351,7 +360,8 @@ func Test_Client_Agent_Referer(t *testing.T) {
|
|||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Referer("http://referer.com")
|
||||
a.Referer("http://referer.com").
|
||||
RefererBytes([]byte("http://referer.com"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "http://referer.com")
|
||||
|
@ -363,13 +373,14 @@ func Test_Client_Agent_ContentType(t *testing.T) {
|
|||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.ContentType("custom-type")
|
||||
a.ContentType("custom-type").
|
||||
ContentTypeBytes([]byte("custom-type"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "custom-type")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Specific_Host(t *testing.T) {
|
||||
func Test_Client_Agent_Host(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
@ -383,7 +394,8 @@ func Test_Client_Agent_Specific_Host(t *testing.T) {
|
|||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
a := Get("http://1.1.1.1:8080").
|
||||
Host("example.com")
|
||||
Host("example.com").
|
||||
HostBytes([]byte("example.com"))
|
||||
|
||||
utils.AssertEqual(t, "1.1.1.1:8080", a.HostClient.Addr)
|
||||
|
||||
|
@ -402,7 +414,8 @@ func Test_Client_Agent_QueryString(t *testing.T) {
|
|||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.QueryString("foo=bar&bar=baz")
|
||||
a.QueryString("foo=bar&bar=baz").
|
||||
QueryStringBytes([]byte("foo=bar&bar=baz"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "foo=bar&bar=baz")
|
||||
|
@ -420,7 +433,8 @@ func Test_Client_Agent_BasicAuth(t *testing.T) {
|
|||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.BasicAuth("foo", "bar")
|
||||
a.BasicAuth("foo", "bar").
|
||||
BasicAuthBytes([]byte("foo"), []byte("bar"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "foo:bar")
|
||||
|
@ -438,6 +452,18 @@ func Test_Client_Agent_BodyString(t *testing.T) {
|
|||
testAgent(t, handler, wrapAgent, "foo=bar&bar=baz")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Body(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Body())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Body([]byte("foo=bar&bar=baz"))
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "foo=bar&bar=baz")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_BodyStream(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
return c.Send(c.Request().Body())
|
||||
|
|
Loading…
Reference in New Issue