-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
58 lines (44 loc) · 1.33 KB
/
server.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
package jh
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
)
// server exists to test this package.
type server struct {
handler http.Handler
}
func (srv *server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
srv.handler.ServeHTTP(w, req)
}
func newServer() *server {
r := httprouter.New()
s := &server{r}
r.GET("/ok/", Adapter(s.ok))
r.GET("/conflict/", Adapter(s.conflict))
r.GET("/created/", Adapter(s.created))
r.GET("/ise/", Adapter(s.ise))
return s
}
func (srv *server) ok(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
return resp(), nil
}
func (srv *server) conflict(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
if err := conflict(); err != nil {
return nil, Wrap(err, "context")
}
// should not get here
return resp(), nil
}
func (srv *server) created(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
return NewSuccess(resp(), http.StatusCreated), nil
}
func (srv *server) ise(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
return nil, errors.New("should get default code")
}
func conflict() error {
return NewError("reason", http.StatusConflict)
}
func resp() map[string]int {
return map[string]int{"a": 3, "b": 2}
}