-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockit.go
106 lines (88 loc) · 2.33 KB
/
mockit.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
package mockit
import (
"bytes"
"fmt"
"html/template"
"net/http"
"regexp"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/dimfeld/httptreemux"
)
// regex to match {{ param }}
var paramRegex = regexp.MustCompile(`{{.+}}`)
// Response represents a mock response.
type Response struct {
Code int
Headers map[string]string
Body string
}
// Endpoint represents a mock endpoint.
type Endpoint struct {
URL string
Method string
Response Response
}
// Config represents a mock configuration.
type Config struct {
Endpoints []Endpoint
}
// NewRouter returns a new router based on the given configuration.
func NewRouter(config Config) http.Handler {
router := httptreemux.New()
for _, e := range config.Endpoints {
endpoint := e
router.Handle(endpoint.Method, endpoint.URL, func(w http.ResponseWriter, r *http.Request, params map[string]string) {
body, err := compile(endpoint.Response.Body, params)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for k, v := range endpoint.Response.Headers {
w.Header().Set(k, v)
}
w.WriteHeader(endpoint.Response.Code)
if _, err := w.Write(body); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}
return router
}
func compile(text string, params map[string]string) ([]byte, error) {
if !paramRegex.MatchString(text) {
return []byte(text), nil
}
funcMap := template.FuncMap{
"urlParam": paramsValue(params),
"uuid": gofakeit.UUID,
"now": time.Now,
"username": gofakeit.Username,
"name": gofakeit.Name,
"email": gofakeit.Email,
"phone": gofakeit.Phone,
"int": gofakeit.Int64,
"digit": gofakeit.Digit,
"digitN": gofakeit.DigitN,
"letter": gofakeit.Letter,
"letterN": gofakeit.LetterN,
"word": gofakeit.Word,
"phrase": gofakeit.Phrase,
"loremIpsum": gofakeit.LoremIpsumSentence,
}
t, err := template.New("").Funcs(funcMap).Parse(text)
if err != nil {
return nil, fmt.Errorf("parse template: %w", err)
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, nil); err != nil {
return nil, fmt.Errorf("execute template: %w", err)
}
return tpl.Bytes(), nil
}
func paramsValue(params map[string]string) func(string) string {
return func(key string) string {
return params[key]
}
}