mirror of https://github.com/gofiber/fiber.git
👷 Add HEAD PUT PATCH DELETE
parent
7c5fac67cf
commit
645e011813
32
client.go
32
client.go
|
@ -69,6 +69,14 @@ func (c *Client) Get(url string) *Agent {
|
|||
return c.createAgent(MethodGet, url)
|
||||
}
|
||||
|
||||
// Head returns a agent with http method HEAD.
|
||||
func Head(url string) *Agent { return defaultClient.Head(url) }
|
||||
|
||||
// Head returns a agent with http method GET.
|
||||
func (c *Client) Head(url string) *Agent {
|
||||
return c.createAgent(MethodHead, url)
|
||||
}
|
||||
|
||||
// Post sends POST request to the given url.
|
||||
func Post(url string) *Agent { return defaultClient.Post(url) }
|
||||
|
||||
|
@ -77,6 +85,30 @@ func (c *Client) Post(url string) *Agent {
|
|||
return c.createAgent(MethodPost, url)
|
||||
}
|
||||
|
||||
// Put sends PUT request to the given url.
|
||||
func Put(url string) *Agent { return defaultClient.Put(url) }
|
||||
|
||||
// Put sends PUT request to the given url.
|
||||
func (c *Client) Put(url string) *Agent {
|
||||
return c.createAgent(MethodPut, url)
|
||||
}
|
||||
|
||||
// Patch sends PATCH request to the given url.
|
||||
func Patch(url string) *Agent { return defaultClient.Patch(url) }
|
||||
|
||||
// Patch sends PATCH request to the given url.
|
||||
func (c *Client) Patch(url string) *Agent {
|
||||
return c.createAgent(MethodPatch, url)
|
||||
}
|
||||
|
||||
// Delete sends DELETE request to the given url.
|
||||
func Delete(url string) *Agent { return defaultClient.Delete(url) }
|
||||
|
||||
// Delete sends DELETE request to the given url.
|
||||
func (c *Client) Delete(url string) *Agent {
|
||||
return c.createAgent(MethodDelete, url)
|
||||
}
|
||||
|
||||
func (c *Client) createAgent(method, url string) *Agent {
|
||||
a := AcquireAgent()
|
||||
a.req.Header.SetMethod(method)
|
||||
|
|
123
client_test.go
123
client_test.go
|
@ -80,6 +80,32 @@ func Test_Client_Get(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_Client_Head(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/", func(c *Ctx) error {
|
||||
return c.SendString(c.Hostname())
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
a := Head("http://example.com")
|
||||
|
||||
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, "", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Client_Post(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -113,6 +139,103 @@ func Test_Client_Post(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_Client_Put(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Put("/", func(c *Ctx) error {
|
||||
return c.SendString(c.FormValue("foo"))
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
args := AcquireArgs()
|
||||
|
||||
args.Set("foo", "bar")
|
||||
|
||||
a := Put("http://example.com").
|
||||
Form(args)
|
||||
|
||||
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, "bar", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
ReleaseArgs(args)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Client_Patch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Patch("/", func(c *Ctx) error {
|
||||
return c.SendString(c.FormValue("foo"))
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
args := AcquireArgs()
|
||||
|
||||
args.Set("foo", "bar")
|
||||
|
||||
a := Patch("http://example.com").
|
||||
Form(args)
|
||||
|
||||
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, "bar", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
ReleaseArgs(args)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Client_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ln := fasthttputil.NewInmemoryListener()
|
||||
|
||||
app := New(Config{DisableStartupMessage: true})
|
||||
|
||||
app.Delete("/", func(c *Ctx) error {
|
||||
return c.Status(StatusNoContent).
|
||||
SendString("deleted")
|
||||
})
|
||||
|
||||
go app.Listener(ln) //nolint:errcheck
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
args := AcquireArgs()
|
||||
|
||||
a := Delete("http://example.com")
|
||||
|
||||
a.HostClient.Dial = func(addr string) (net.Conn, error) { return ln.Dial() }
|
||||
|
||||
code, body, errs := a.String()
|
||||
|
||||
utils.AssertEqual(t, StatusNoContent, code)
|
||||
utils.AssertEqual(t, "", body)
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
|
||||
ReleaseArgs(args)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Client_UserAgent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
Loading…
Reference in New Issue