-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
111 lines (100 loc) · 2.69 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package nvelope_test
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
"github.com/muir/nape"
"github.com/muir/nvelope"
"github.com/gorilla/mux"
)
// nolint:deadcode,unused
func Main() {
r := mux.NewRouter()
srv := &http.Server{
Addr: "0.0.0.0:8080",
Handler: r,
}
Service(r)
log.Fatal(srv.ListenAndServe())
}
type PostBodyModel struct {
Use string `json:"use"`
Exported string `json:"exported"`
Names string `json:"names"`
}
type ExampleRequestBundle struct {
Request PostBodyModel `nvelope:"model"`
With *string `nvelope:"path,name=with"`
Parameters int64 `nvelope:"path,name=parameters"`
Friends []int `nvelope:"query,name=friends"`
ContentType string `nvelope:"header,name=Content-Type"`
}
type ExampleResponse struct {
Stuff string `json:"stuff,omitempty"`
Here string `json:"here,omitempty"`
}
func HandleExampleEndpoint(req ExampleRequestBundle) (nvelope.Response, error) {
if req.ContentType != "application/json" {
return nil, errors.New("content type must be application/json")
}
switch req.Parameters {
case 666:
panic("something is not right")
case 100:
return nil, nil
default:
return ExampleResponse{
Stuff: *req.With,
}, nil
}
}
func Service(router *mux.Router) {
service := nape.RegisterServiceWithMux("example", router)
service.RegisterEndpoint("/a/path/{with}/{parameters}",
// order matters and this is a correct order
nvelope.NoLogger,
nvelope.InjectWriter,
nvelope.EncodeJSON,
nvelope.CatchPanic,
nvelope.Nil204,
nvelope.ReadBody,
nape.DecodeJSON,
HandleExampleEndpoint,
).Methods("POST")
}
// Example shows an injection chain handling a single endpoint using nject,
// nape, and nvelope.
func Example() {
r := mux.NewRouter()
Service(r)
ts := httptest.NewServer(r)
client := ts.Client()
doPost := func(url string, body string) {
// nolint:noctx
res, err := client.Post(ts.URL+url, "application/json",
strings.NewReader(body))
if err != nil {
fmt.Println("response error:", err)
return
}
b, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println("read error:", err)
return
}
res.Body.Close()
fmt.Println(res.StatusCode, "->"+string(b))
}
doPost("/a/path/joe/37", `{"Use":"yeah","Exported":"uh hu"}`)
doPost("/a/path/joe/100", `{"Use":"yeah","Exported":"uh hu"}`)
doPost("/a/path/joe/38", `invalid json`)
doPost("/a/path/joe/666", `{"Use":"yeah","Exported":"uh hu"}`)
// Output: 200 ->{"stuff":"joe"}
// 204 ->
// 400 ->nvelope_test.ExampleRequestBundle model: Could not decode application/json into nvelope_test.PostBodyModel: invalid character 'i' looking for beginning of value
// 500 ->panic: something is not right
}