forked from tokopedia/gripmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub.go
165 lines (136 loc) · 3.16 KB
/
stub.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package stub
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/go-chi/chi"
)
type Options struct {
Port string
BindAddr string
StubPath string
}
const DEFAULT_PORT = "4771"
func RunStubServer(opt Options) {
if opt.Port == "" {
opt.Port = DEFAULT_PORT
}
addr := opt.BindAddr + ":" + opt.Port
r := chi.NewRouter()
r.Post("/add", addStub)
r.Get("/", listStub)
r.Post("/find", handleFindStub)
r.Get("/clear", handleClearStub)
if opt.StubPath != "" {
readStubFromFile(opt.StubPath)
}
fmt.Println("Serving stub admin on http://" + addr)
go func() {
err := http.ListenAndServe(addr, r)
log.Fatal(err)
}()
}
func responseError(err error, w http.ResponseWriter) {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
type Stub struct {
Service string `json:"service"`
Method string `json:"method"`
Input Input `json:"input"`
Output Output `json:"output"`
}
type Input struct {
Equals map[string]interface{} `json:"equals"`
Contains map[string]interface{} `json:"contains"`
Matches map[string]interface{} `json:"matches"`
}
type Output struct {
Data map[string]interface{} `json:"data"`
Error string `json:"error"`
}
func addStub(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
responseError(err, w)
return
}
stub := new(Stub)
err = json.Unmarshal(body, stub)
if err != nil {
responseError(err, w)
return
}
err = validateStub(stub)
if err != nil {
responseError(err, w)
return
}
err = storeStub(stub)
if err != nil {
responseError(err, w)
return
}
w.Write([]byte("Success add stub"))
}
func listStub(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(allStub())
}
func validateStub(stub *Stub) error {
if stub.Service == "" {
return fmt.Errorf("Service name can't be empty")
}
if stub.Method == "" {
return fmt.Errorf("Method name can't be emtpy")
}
// due to golang implementation
// method name must capital
stub.Method = strings.Title(stub.Method)
switch {
case stub.Input.Contains != nil:
break
case stub.Input.Equals != nil:
break
case stub.Input.Matches != nil:
break
default:
return fmt.Errorf("Input cannot be empty")
}
// TODO: validate all input case
if stub.Output.Error == "" && stub.Output.Data == nil {
return fmt.Errorf("Output can't be empty")
}
return nil
}
type findStubPayload struct {
Service string `json:"service"`
Method string `json:"method"`
Data map[string]interface{} `json:"data"`
}
func handleFindStub(w http.ResponseWriter, r *http.Request) {
stub := new(findStubPayload)
err := json.NewDecoder(r.Body).Decode(stub)
if err != nil {
responseError(err, w)
return
}
// due to golang implementation
// method name must capital
stub.Method = strings.Title(stub.Method)
output, err := findStub(stub)
if err != nil {
log.Println(err)
responseError(err, w)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
func handleClearStub(w http.ResponseWriter, r *http.Request) {
clearStorage()
w.Write([]byte("OK"))
}