mirror of https://github.com/gofiber/fiber.git
👷 Add XML
parent
20104ba10f
commit
5af0d42e7b
18
client.go
18
client.go
|
@ -3,6 +3,7 @@ package fiber
|
|||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
@ -277,8 +278,8 @@ func (a *Agent) Request(req *Request) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// Json sends a json request.
|
||||
func (a *Agent) Json(v interface{}) *Agent {
|
||||
// JSON sends a JSON request.
|
||||
func (a *Agent) JSON(v interface{}) *Agent {
|
||||
a.req.Header.SetContentType(MIMEApplicationJSON)
|
||||
|
||||
if body, err := json.Marshal(v); err != nil {
|
||||
|
@ -290,6 +291,19 @@ func (a *Agent) Json(v interface{}) *Agent {
|
|||
return a
|
||||
}
|
||||
|
||||
// XML sends a XML request.
|
||||
func (a *Agent) XML(v interface{}) *Agent {
|
||||
a.req.Header.SetContentType(MIMEApplicationXML)
|
||||
|
||||
if body, err := xml.Marshal(v); err != nil {
|
||||
a.errs = append(a.errs, err)
|
||||
} else {
|
||||
a.req.SetBody(body)
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Form sends request with body if args is non-nil.
|
||||
//
|
||||
// Note that this will force http method to post.
|
||||
|
|
|
@ -368,15 +368,15 @@ func Test_Client_Agent_Json(t *testing.T) {
|
|||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.Json(jsonData{F: "f"})
|
||||
a.JSON(data{Success: true})
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, `{"f":"f"}`)
|
||||
testAgent(t, handler, wrapAgent, `{"success":true}`)
|
||||
}
|
||||
|
||||
func Test_Client_Agent_Json_Error(t *testing.T) {
|
||||
a := Get("http://example.com").
|
||||
Json(complex(1, 1))
|
||||
JSON(complex(1, 1))
|
||||
|
||||
_, body, errs := a.String()
|
||||
|
||||
|
@ -385,6 +385,31 @@ func Test_Client_Agent_Json_Error(t *testing.T) {
|
|||
utils.AssertEqual(t, "json: unsupported type: complex128", errs[0].Error())
|
||||
}
|
||||
|
||||
func Test_Client_Agent_XML(t *testing.T) {
|
||||
handler := func(c *Ctx) error {
|
||||
utils.AssertEqual(t, MIMEApplicationXML, string(c.Request().Header.ContentType()))
|
||||
|
||||
return c.Send(c.Request().Body())
|
||||
}
|
||||
|
||||
wrapAgent := func(a *Agent) {
|
||||
a.XML(data{Success: true})
|
||||
}
|
||||
|
||||
testAgent(t, handler, wrapAgent, "<data><success>true</success></data>")
|
||||
}
|
||||
|
||||
func Test_Client_Agent_XML_Error(t *testing.T) {
|
||||
a := Get("http://example.com").
|
||||
XML(complex(1, 1))
|
||||
|
||||
_, body, errs := a.String()
|
||||
|
||||
utils.AssertEqual(t, "", body)
|
||||
utils.AssertEqual(t, 1, len(errs))
|
||||
utils.AssertEqual(t, "xml: 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()))
|
||||
|
@ -405,10 +430,6 @@ func Test_Client_Agent_Form(t *testing.T) {
|
|||
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")
|
||||
|
@ -524,10 +545,6 @@ func Test_Client_Agent_TLS(t *testing.T) {
|
|||
utils.AssertEqual(t, "tls", body)
|
||||
}
|
||||
|
||||
type data struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func Test_Client_Agent_MaxRedirectsCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -665,3 +682,7 @@ func testAgent(t *testing.T, handler Handler, wrapAgent func(agent *Agent), exce
|
|||
utils.AssertEqual(t, 0, len(errs))
|
||||
}
|
||||
}
|
||||
|
||||
type data struct {
|
||||
Success bool `json:"success" xml:"success"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue