-
Notifications
You must be signed in to change notification settings - Fork 6
/
service.go
117 lines (100 loc) · 2.89 KB
/
service.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
112
113
114
115
116
117
package restit
import (
"net/http"
"net/http/httptest"
"net/url"
"path"
)
// HTTPHandler implements CaseHandlerFunc
func HTTPHandler(req *http.Request) (resp Response, err error) {
c := &http.Client{}
rawResp, err := c.Do(req)
if err != nil {
return
}
resp = HTTPResponse{rawResp}
return
}
// NewHTTPService create a normal HTTP service to a real
// HTTP server
func NewHTTPService(rawURL string) *Service {
baseURL, _ := url.Parse(rawURL)
return &Service{
BaseURL: baseURL,
Handler: CaseHandlerFunc(HTTPHandler),
}
}
// HTTPTestHandler implements CaseHandlerFunc
func HTTPTestHandler(handler http.Handler) func(*http.Request) (Response, error) {
return func(req *http.Request) (resp Response, err error) {
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp = HTTPTestResponse{w}
return
}
}
// NewHTTPTestService create a dummy service based on httptest.Recorder
// as request handler
func NewHTTPTestService(rawURL string, handler http.Handler) *Service {
baseURL, _ := url.Parse(rawURL)
return &Service{
BaseURL: baseURL,
Handler: CaseHandlerFunc(HTTPTestHandler(handler)),
}
}
// Service provides method to interact with a RESTful service
// based on the given Paths value
type Service struct {
BaseURL *url.URL
Handler CaseHandler
}
// NewCase creates a new Case struct with
func (s Service) NewCase(method string, payload interface{}, paths ...string) *Case {
// formulate request URL
requestURL, err := url.Parse(s.BaseURL.String())
if err != nil {
panic(err)
}
if len(paths) > 0 {
requestURL.Path = path.Join(append([]string{requestURL.Path}, paths...)...)
}
// formulate request
req, err := NewRequest(method, requestURL.String(), payload)
if err != nil {
panic(err)
}
return &Case{
Request: req,
Handler: s.Handler,
}
}
// List sends a GET request
// to plural path and examine the result
func (s Service) List(paths ...string) *Case {
return s.NewCase("GET", nil, paths...)
}
// Create sends a POST request (with JSON encoded payload)
// to plural path and examine the result
func (s Service) Create(payload interface{}, paths ...string) *Case {
return s.NewCase("POST", payload, paths...)
}
// Update sends a PUT request (wtih JSON encoded payload)
// to singular path and examine the result
func (s Service) Update(payload interface{}, paths ...string) *Case {
return s.NewCase("PUT", payload, paths...)
}
// Patch sends a PATCH request (wtih JSON encoded payload)
// to singular path and examine the result
func (s Service) Patch(payload interface{}, paths ...string) *Case {
return s.NewCase("PATCH", payload, paths...)
}
// Retrieve sends a GET request
// to singular path and examine the result
func (s Service) Retrieve(paths ...string) *Case {
return s.NewCase("GET", nil, paths...)
}
// Delete sends a DELETE request
// to singular path and examine the result
func (s Service) Delete(paths ...string) *Case {
return s.NewCase("DELETE", nil, paths...)
}