Skip to content

Commit 8ea546e

Browse files
committed
DecodeJSON/XML now tested
1 parent dea16dd commit 8ea546e

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.17
55
require (
66
github.com/julienschmidt/httprouter v1.3.0
77
github.com/muir/nject v0.4.0
8-
github.com/muir/nvelope v0.3.0
8+
github.com/muir/nvelope v0.3.1
99
github.com/pkg/errors v0.9.1
1010
github.com/stretchr/testify v1.7.0
1111
)

go.sum

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:F
3434
github.com/muir/nape v0.2.0/go.mod h1:VjvP6gR5PzQ3yyOPjyGlFOxDFucMVhBkaJGT6c0CvsU=
3535
github.com/muir/nape v0.2.1 h1:Yce3vLOW4umOMwFzvqYHfIKa3Ltx0Y6qiGcE8yFPkJA=
3636
github.com/muir/nape v0.2.1/go.mod h1:FE26YLNvUZ7DQJVW34+ypho9cGTn0NccvXjMaDo1nTw=
37+
github.com/muir/nchi v0.0.1/go.mod h1:v6gUg0F1wD7qpZcp6I2/5Y4IUA9qzlKgCqehdEuOPJg=
3738
github.com/muir/nject v0.2.0/go.mod h1:vFJkDQciI0UURYI3cSZLyBhYxL3gn+4FvJBY9cZSsaA=
3839
github.com/muir/nject v0.4.0 h1:g9IBW+6PHtz65GsKinJRweHXBQLaRUZgtbjIGnEACA0=
3940
github.com/muir/nject v0.4.0/go.mod h1:le4jeI0HP94VGIAGbdB4gLMzWENM/uHh8Iq3/Qv8wak=
4041
github.com/muir/nvelope v0.2.0/go.mod h1:raB9voDUnhMELiU2fZG7yHYdiRGKSh/FGtsjYWtdbpc=
41-
github.com/muir/nvelope v0.3.0 h1:p2iufA6MHIi0ciHUnTaOZLwbWAYVnx+IAjX1Otm0vW0=
4242
github.com/muir/nvelope v0.3.0/go.mod h1:XWPSm68reA4rxkuC/keqHbeFT9QJBorF2ttjF7flzyM=
43+
github.com/muir/nvelope v0.3.1 h1:wccP8t3UP1tsQBjOU/wuZVr2p68B4/ZE7W7qGc3CzFY=
44+
github.com/muir/nvelope v0.3.1/go.mod h1:bND7Jn4CqmEEKXyTh+GKtbN4Bi4GKnWRSR4iB5l0+AU=
4345
github.com/muir/reflectutils v0.4.0 h1:1VXbXaav7tR5BTYTHcGTdOAyxu3idCzM5FCtCpgm2u4=
4446
github.com/muir/reflectutils v0.4.0/go.mod h1:hyMWDtoeNsc1FTq9qlXsbwDzbwp3A3M8zbRNKDzvSmc=
4547
github.com/pelletier/go-toml v1.0.1-0.20170904195809-1d6b12b7cb29/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=

nvelope.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111

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

2324
// DecodeXML is is a pre-defined special nject.Provider created with
2425
// nvelope.GenerateDecoder for decoding XML requests.Use it with the
25-
// other features of https://github.com/muir/nvelope
26+
// other features of https://github.com/muir/nvelope . DecodeXML must be
27+
// paired with nvelope.ReadBody to actually decode XML.
2628
var DecodeXML = nvelope.GenerateDecoder(
2729
nvelope.WithDecoder("application/xml", xml.Unmarshal),
2830
nvelope.WithDefaultContentType("application/xml"),

nvelope_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package nchi_test
2+
3+
import (
4+
"bytes"
5+
"encoding/xml"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"net/http/httptest"
10+
"strings"
11+
"testing"
12+
13+
"github.com/muir/nchi"
14+
"github.com/muir/nvelope"
15+
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
type bodyData struct {
20+
I int `json:"i"`
21+
}
22+
23+
type parameters struct {
24+
Body bodyData `nvelope:"model"`
25+
Zoom int `nvelope:"path,name=zoomie"`
26+
Who string `nvelope:"query,name=who"`
27+
}
28+
29+
func TestDecoders(t *testing.T) {
30+
mux := nchi.NewRouter(nchi.WithRedirectFixedPath(true))
31+
mux.Use(nvelope.MinimalErrorHandler, nvelope.ReadBody)
32+
mux.Post("/td1/:zoomie/foo", nchi.DecodeJSON, func(p parameters, w http.ResponseWriter) {
33+
_, _ = w.Write([]byte(fmt.Sprintf("bi %d z %d w %s", p.Body.I, p.Zoom, p.Who)))
34+
})
35+
mux.Post("/td2/:zoomie/bar", nchi.DecodeXML, func(p parameters, w http.ResponseWriter) {
36+
_, _ = w.Write([]byte(fmt.Sprintf("bi %d z %d w %s", p.Body.I, p.Zoom, p.Who)))
37+
})
38+
39+
w := httptest.NewRecorder()
40+
r := httptest.NewRequest("POST", "/td1/9/foo?who=john", strings.NewReader(`{"I":3}`))
41+
mux.ServeHTTP(w, r)
42+
body, err := io.ReadAll(w.Result().Body)
43+
assert.NoError(t, err, "/td1")
44+
got := string(body)
45+
t.Log("/td1/9/foo?who=john ->", got)
46+
assert.Equal(t, "bi 3 z 9 w john", got, "/td1")
47+
48+
enc, err := xml.Marshal(bodyData{I: 19})
49+
assert.NoError(t, err, "marshal xml")
50+
w = httptest.NewRecorder()
51+
r = httptest.NewRequest("POST", "/td2/11/bar?who=mary", bytes.NewReader(enc))
52+
mux.ServeHTTP(w, r)
53+
body, err = io.ReadAll(w.Result().Body)
54+
assert.NoError(t, err, "/td2")
55+
got = string(body)
56+
t.Log("/td2/11/bar?who=mary ->", got)
57+
assert.Equal(t, "bi 19 z 11 w mary", got, "/td2")
58+
}

0 commit comments

Comments
 (0)