Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
chore: update modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jan 1, 2020
1 parent 632c435 commit 2a85e2b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 49 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://img.shields.io/travis/vicanso/elton-responder.svg?label=linux+build)](https://travis-ci.org/vicanso/elton-responder)

Responder middleware for elton, it can convert `Context.Body` to json data. By this middleware, it's more simple for successful response. More response type can be supported through custom marshal function and content type.
Responder middleware for elton, it can convert `Context.Body` to json data. Using this middleware, it's more simple for successful response. More response type can be supported through custom marshal function and content type.


```go
Expand All @@ -15,12 +15,12 @@ import (
)

func main() {
d := elton.New()
e := elton.New()

d.Use(responder.NewDefault())
e.Use(responder.NewDefault())

// {"name":"tree.xie","id":123}
d.GET("/", func(c *elton.Context) (err error) {
e.GET("/", func(c *elton.Context) (err error) {
c.Body = &struct {
Name string `json:"name"`
ID int `json:"id"`
Expand All @@ -31,7 +31,10 @@ func main() {
return
})

d.ListenAndServe(":7001")
er := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}
```

Expand Down
8 changes: 4 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type AutoGenerated []struct {
}

func main() {
d := elton.New()
e := elton.New()
resp, err := http.Get("https://api.github.com/repos/vicanso/elton/commits")
if err != nil {
panic(err)
Expand All @@ -104,15 +104,15 @@ func main() {
}
_ = json.Unmarshal(buf, genInfo)

d.Use(responder.New(responder.Config{
e.Use(responder.New(responder.Config{
Marshal: marshal,
}))
d.GET("/", func(c *elton.Context) (err error) {
e.GET("/", func(c *elton.Context) (err error) {
c.Body = genInfo
return
})

err = d.ListenAndServe(":3000")
err = e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.12

require (
github.com/stretchr/testify v1.4.0
github.com/vicanso/elton v0.2.2
github.com/vicanso/elton v0.2.3
github.com/vicanso/hes v0.2.1
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4d
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/vicanso/elton v0.2.2 h1:MZ5nfJFKBWDWnFPO8wRyPat8kZz3KoNBY0scemo7RFQ=
github.com/vicanso/elton v0.2.2/go.mod h1:QFZ+Un4LLBANtl0mExkqLD4uqw3JLA2ZCWUHaCsHOUg=
github.com/vicanso/elton v0.2.3 h1:XQskGFtw/hhtNXRU7dLX0OFcpG64pK4PMXh9CVjHVbA=
github.com/vicanso/elton v0.2.3/go.mod h1:QFZ+Un4LLBANtl0mExkqLD4uqw3JLA2ZCWUHaCsHOUg=
github.com/vicanso/hes v0.2.1 h1:jRFEADmiQ30koVY/sKwlkhyXM5B3QbVVizLqrjNJgPw=
github.com/vicanso/hes v0.2.1/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo=
github.com/vicanso/intranet-ip v0.0.1 h1:cYS+mExFsKqewWSuHtFwAqw/CO66GsheB/P1BPmSTx0=
Expand Down
72 changes: 36 additions & 36 deletions responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,53 +86,53 @@ func TestResponder(t *testing.T) {
})

t.Run("invalid response", func(t *testing.T) {
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
checkResponse(t, resp, 500, "category=elton-responder, message=invalid response")
})

t.Run("return string", func(t *testing.T) {
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.Body = "abc"
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
checkResponse(t, resp, 200, "abc")
checkContentType(t, resp, "text/plain; charset=UTF-8")
})

t.Run("return bytes", func(t *testing.T) {
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.Body = []byte("abc")
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
checkResponse(t, resp, 200, "abc")
checkContentType(t, resp, elton.MIMEBinary)
})
t.Run("return bytes(set content type)", func(t *testing.T) {
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.Body = []byte("abc")
return nil
})
resp := httptest.NewRecorder()

contentType := "abc"
resp.Header().Set(elton.HeaderContentType, contentType)
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
checkResponse(t, resp, 200, "abc")
checkContentType(t, resp, contentType)
})
Expand All @@ -141,71 +141,71 @@ func TestResponder(t *testing.T) {
type T struct {
Name string `json:"name,omitempty"`
}
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.Created(&T{
Name: "tree.xie",
})
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
checkResponse(t, resp, 201, `{"name":"tree.xie"}`)
checkJSON(t, resp)
})

t.Run("json marshal fail", func(t *testing.T) {
assert := assert.New(t)
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.Body = func() {}
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
assert.Equal(500, resp.Code)
assert.Equal("message=json: unsupported type: func()", resp.Body.String())
})

t.Run("reader body", func(t *testing.T) {
assert := assert.New(t)
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.Body = bytes.NewReader([]byte("abcd"))
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, 200)
assert.Equal(resp.Body.String(), "abcd")
})

t.Run("no content", func(t *testing.T) {
assert := assert.New(t)
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.StatusCode = http.StatusNoContent
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusNoContent)
})

t.Run("accepted(202)", func(t *testing.T) {
assert := assert.New(t)
d := elton.New()
d.Use(m)
d.GET("/", func(c *elton.Context) error {
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
c.StatusCode = http.StatusAccepted
return nil
})
resp := httptest.NewRecorder()
d.ServeHTTP(resp, req)
e.ServeHTTP(resp, req)
assert.Empty(resp.Body)
assert.Equal(resp.Code, http.StatusAccepted)
})
Expand Down

0 comments on commit 2a85e2b

Please sign in to comment.