Skip to content

Commit

Permalink
DecodeJSON/XML now tested
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Mar 18, 2022
1 parent dea16dd commit 8ea546e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/julienschmidt/httprouter v1.3.0
github.com/muir/nject v0.4.0
github.com/muir/nvelope v0.3.0
github.com/muir/nvelope v0.3.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:F
github.com/muir/nape v0.2.0/go.mod h1:VjvP6gR5PzQ3yyOPjyGlFOxDFucMVhBkaJGT6c0CvsU=
github.com/muir/nape v0.2.1 h1:Yce3vLOW4umOMwFzvqYHfIKa3Ltx0Y6qiGcE8yFPkJA=
github.com/muir/nape v0.2.1/go.mod h1:FE26YLNvUZ7DQJVW34+ypho9cGTn0NccvXjMaDo1nTw=
github.com/muir/nchi v0.0.1/go.mod h1:v6gUg0F1wD7qpZcp6I2/5Y4IUA9qzlKgCqehdEuOPJg=
github.com/muir/nject v0.2.0/go.mod h1:vFJkDQciI0UURYI3cSZLyBhYxL3gn+4FvJBY9cZSsaA=
github.com/muir/nject v0.4.0 h1:g9IBW+6PHtz65GsKinJRweHXBQLaRUZgtbjIGnEACA0=
github.com/muir/nject v0.4.0/go.mod h1:le4jeI0HP94VGIAGbdB4gLMzWENM/uHh8Iq3/Qv8wak=
github.com/muir/nvelope v0.2.0/go.mod h1:raB9voDUnhMELiU2fZG7yHYdiRGKSh/FGtsjYWtdbpc=
github.com/muir/nvelope v0.3.0 h1:p2iufA6MHIi0ciHUnTaOZLwbWAYVnx+IAjX1Otm0vW0=
github.com/muir/nvelope v0.3.0/go.mod h1:XWPSm68reA4rxkuC/keqHbeFT9QJBorF2ttjF7flzyM=
github.com/muir/nvelope v0.3.1 h1:wccP8t3UP1tsQBjOU/wuZVr2p68B4/ZE7W7qGc3CzFY=
github.com/muir/nvelope v0.3.1/go.mod h1:bND7Jn4CqmEEKXyTh+GKtbN4Bi4GKnWRSR4iB5l0+AU=
github.com/muir/reflectutils v0.4.0 h1:1VXbXaav7tR5BTYTHcGTdOAyxu3idCzM5FCtCpgm2u4=
github.com/muir/reflectutils v0.4.0/go.mod h1:hyMWDtoeNsc1FTq9qlXsbwDzbwp3A3M8zbRNKDzvSmc=
github.com/pelletier/go-toml v1.0.1-0.20170904195809-1d6b12b7cb29/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
6 changes: 4 additions & 2 deletions nvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// DecodeJSON is is a pre-defined special nject.Provider created with
// nvelope.GenerateDecoder for decoding JSON requests. Use it with the
// other features of https://github.com/muir/nvelope
// other features of https://github.com/muir/nvelope . DecodeJSON must
// be paired with nvelope.ReadBody to actually decode JSON.
var DecodeJSON = nvelope.GenerateDecoder(
nvelope.WithDecoder("application/json", json.Unmarshal),
nvelope.WithDefaultContentType("application/json"),
Expand All @@ -22,7 +23,8 @@ var DecodeJSON = nvelope.GenerateDecoder(

// DecodeXML is is a pre-defined special nject.Provider created with
// nvelope.GenerateDecoder for decoding XML requests.Use it with the
// other features of https://github.com/muir/nvelope
// other features of https://github.com/muir/nvelope . DecodeXML must be
// paired with nvelope.ReadBody to actually decode XML.
var DecodeXML = nvelope.GenerateDecoder(
nvelope.WithDecoder("application/xml", xml.Unmarshal),
nvelope.WithDefaultContentType("application/xml"),
Expand Down
58 changes: 58 additions & 0 deletions nvelope_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package nchi_test

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/muir/nchi"
"github.com/muir/nvelope"

"github.com/stretchr/testify/assert"
)

type bodyData struct {
I int `json:"i"`
}

type parameters struct {
Body bodyData `nvelope:"model"`
Zoom int `nvelope:"path,name=zoomie"`
Who string `nvelope:"query,name=who"`
}

func TestDecoders(t *testing.T) {
mux := nchi.NewRouter(nchi.WithRedirectFixedPath(true))
mux.Use(nvelope.MinimalErrorHandler, nvelope.ReadBody)
mux.Post("/td1/:zoomie/foo", nchi.DecodeJSON, func(p parameters, w http.ResponseWriter) {
_, _ = w.Write([]byte(fmt.Sprintf("bi %d z %d w %s", p.Body.I, p.Zoom, p.Who)))
})
mux.Post("/td2/:zoomie/bar", nchi.DecodeXML, func(p parameters, w http.ResponseWriter) {
_, _ = w.Write([]byte(fmt.Sprintf("bi %d z %d w %s", p.Body.I, p.Zoom, p.Who)))
})

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/td1/9/foo?who=john", strings.NewReader(`{"I":3}`))
mux.ServeHTTP(w, r)
body, err := io.ReadAll(w.Result().Body)
assert.NoError(t, err, "/td1")
got := string(body)
t.Log("/td1/9/foo?who=john ->", got)
assert.Equal(t, "bi 3 z 9 w john", got, "/td1")

enc, err := xml.Marshal(bodyData{I: 19})
assert.NoError(t, err, "marshal xml")
w = httptest.NewRecorder()
r = httptest.NewRequest("POST", "/td2/11/bar?who=mary", bytes.NewReader(enc))
mux.ServeHTTP(w, r)
body, err = io.ReadAll(w.Result().Body)
assert.NoError(t, err, "/td2")
got = string(body)
t.Log("/td2/11/bar?who=mary ->", got)
assert.Equal(t, "bi 19 z 11 w mary", got, "/td2")
}

0 comments on commit 8ea546e

Please sign in to comment.