Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support xml:#14 done #16

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions decode/xml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package decode

import (
"encoding/xml"
"io"
)

type XmlDecode struct {
obj interface{}
}

func NewXmlDecode(obj interface{}) *XmlDecode {
if obj == nil {
return nil
}
return &XmlDecode{obj: obj}
}

func (x *XmlDecode) Decode(r io.Reader) error {
decode := xml.NewDecoder(r)
return decode.Decode(x.obj)
}
23 changes: 23 additions & 0 deletions encode/xml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package encode

import (
"encoding/xml"
"io"
)

type XmlEncode struct {
obj interface{}
}

func NewXmlEncode(obj interface{}) *XmlEncode {
if obj == nil {
return nil
}

return &XmlEncode{obj: obj}
}

func (x *XmlEncode) Encode(w io.Writer) error {
encode := xml.NewEncoder(w)
return encode.Encode(x.obj)
}
50 changes: 16 additions & 34 deletions gout_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package gout

import (
"context"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)

func TestMethod(t *testing.T) {

var total int32

var srv *http.Server

router := func() {
router := func() *gin.Engine {
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
Expand All @@ -32,39 +30,23 @@ func TestMethod(t *testing.T) {
router.HEAD("/someHead", cb)
router.OPTIONS("/someOptions", cb)

srv = &http.Server{
Addr: ":8080",
Handler: router,
}
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
t.Errorf("listen: %s\n", err)
}
}

go router()
return router
}()

time.Sleep(time.Millisecond * 250)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()

out := New(nil)
err := out.GET(":8080/someGet").Next().
POST(":8080/somePost").Next().
PUT(":8080/somePut").Next().
DELETE(":8080/someDelete").Next().
PATCH(":8080/somePatch").Next().
HEAD(":8080/someHead").Next().
OPTIONS(":8080/someOptions").Next().Do()
err := out.GET(ts.URL + "/someGet").Next().
POST(ts.URL + "/somePost").Next().
PUT(ts.URL + "/somePut").Next().
DELETE(ts.URL + "/someDelete").Next().
PATCH(ts.URL + "/somePatch").Next().
HEAD(ts.URL + "/someHead").Next().
OPTIONS(ts.URL + "/someOptions").Next().Do()

if err != nil {
t.Errorf("http client fail:%v\n", err)
}
assert.NoError(t, err)

if total != 7 {
t.Errorf("got %d want 7\n", total)
}
assert.Equal(t, int(total), 7)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
t.Errorf("Server Shutdown:%s\n", err)
}
}
10 changes: 10 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,21 @@ func (g *routerGroup) ToJSON(obj interface{}) *routerGroup {
return g
}

func (g *routerGroup) ToXML(obj interface{}) *routerGroup {
g.Req.bodyEncoder = encode.NewXmlEncode(obj)
return g
}

func (g *routerGroup) ShouldBindJSON(obj interface{}) *routerGroup {
g.Req.bodyDecoder = decode.NewJsonDecode(obj)
return g
}

func (g *routerGroup) ShouldBindXML(obj interface{}) *routerGroup {
g.Req.bodyDecoder = decode.NewXmlDecode(obj)
return g
}

func (g *routerGroup) Code(httpCode *int) *routerGroup {
g.Req.httpCode = httpCode
return g
Expand Down
34 changes: 32 additions & 2 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func TestGroupNew(t *testing.T) {
}

type data struct {
Id int `json:"id"`
Data string `json:"data"`
Id int `json:"id" xml:"id"`
Data string `json:"data xml:"data""`
}

type BindTest struct {
Expand All @@ -107,6 +107,36 @@ type BindTest struct {
httpCode int
}

func TestShouldBindXML(t *testing.T) {
var d, d2 data
router := func() *gin.Engine {
router := gin.Default()

router.POST("/test.xml", func(c *gin.Context) {
var d3 data
c.ShouldBindXML(&d3)
c.XML(200, d3)
})
return router
}()

ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()

g := New(nil)

d.Id = 3
d.Data = "test data"

code := 200

err := g.POST(ts.URL + "/test.xml").ToXML(&d).ShouldBindXML(&d2).Code(&code).Do()

assert.NoError(t, err)
assert.Equal(t, code, 200)
assert.Equal(t, d, d2)
}

func TestShouldBindJSON(t *testing.T) {
var d3 data
router := func() *gin.Engine {
Expand Down