-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
195 lines (173 loc) · 4.6 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package gmock // nolint:golint
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/rs/zerolog/log"
)
const (
defaultStubsDir = "stubs" // the default location for stubs
defaultContentType = "application/json" // the default content type for stubs
defaultPort = 8080 // the default port for the server
)
// Config is used to configure the server.
type Config struct {
Port int
StubsDir string
Stubs []*Stub
}
// Server is the HTTP mock server.
type Server struct {
stubs stubHashMap
dir string
port int
srv *http.Server
}
// NewServer creates a new server.
func NewServer() *Server {
s := &Server{
stubs: make(stubHashMap),
dir: defaultStubsDir,
port: defaultPort,
}
return s
}
// NewServerWithConfig creates a new server with the given config.
func NewServerWithConfig(config Config) *Server {
s := NewServer()
if config.Port > 0 {
s.port = config.Port
}
s.addStubs(config.Stubs...)
s.loadStubs(config.StubsDir)
return s
}
// Start starts the server.
func (s *Server) Start() {
if s.dir == defaultStubsDir {
s.loadStubs(defaultStubsDir)
}
mux := http.NewServeMux()
mux.HandleFunc("/httpmock/add", s.addStubHandler)
mux.HandleFunc("/httpmock/list", s.listStubsHandler)
mux.HandleFunc("/", s.genericStubHandler)
s.srv = &http.Server{
Addr: fmt.Sprintf(":%d", s.port),
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}
go func() {
log.Info().Msgf("starting HTTP mock server on port %d", s.port)
if err := s.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error().Msgf("failed to start HTTP mock server: %v", err)
}
}()
time.Sleep(time.Second)
}
// Stop stops the server.
func (s *Server) Stop() error {
if err := s.srv.Shutdown(context.Background()); err != nil {
log.Error().Msgf("failed to stop HTTP mock server: %v", err)
return err
}
log.Info().Msgf("HTTP mock server stopped")
return nil
}
// AddStub adds a stub to the server.
func (s *Server) AddStub(stub *Stub) {
s.addStub(stub)
}
// AddStubs adds multiple stubs to the server.
func (s *Server) AddStubs(stubs ...*Stub) {
s.addStubs(stubs...)
}
// WithStubs adds multiple stubs to the server.
func (s *Server) WithStubs(stubs ...*Stub) *Server {
s.addStubs(stubs...)
return s
}
// WithStubsFrom loads stubs from the given location.
func (s *Server) WithStubsFrom(stubsLocation string) *Server {
s.loadStubs(stubsLocation)
return s
}
// ClearStubs clears all stubs from the server.
func (s *Server) ClearStubs() {
s.stubs = make(stubHashMap)
}
// WithPort sets the port for the server.
func (s *Server) WithPort(port int) *Server {
s.port = port
return s
}
// loadStubs loads stubs from the given location.
func (s *Server) loadStubs(location string) {
s.dir = location
files, err := os.ReadDir(location)
if err != nil {
log.Warn().Msgf("failed to read stubs directory: %s %v", location, err)
return
}
for _, file := range files {
if file.IsDir() {
s.loadStubs(filepath.Join(location, file.Name()))
continue
}
// only load .json and .yaml/.yml files
if !strings.HasSuffix(file.Name(), ".json") && !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
continue
}
fileBytes, err := os.ReadFile(fmt.Sprintf("%s/%s", location, file.Name()))
if err != nil {
log.Error().Msgf("failed to read stub file: %v", err)
return
}
stub, err := getStubFromBytes(fileBytes)
if err != nil {
log.Error().Msgf("failed to parse stub file %s: %v", file.Name(), err)
return
}
s.addStub(stub)
}
}
// addStub adds a stub to the server.
func (s *Server) addStub(stub *Stub) []error {
stub.Request.Sanitize()
if errs := stub.validationErrors(); len(errs) > 0 {
log.Error().Msgf("invalid stub: %v", errs)
return errs
}
// compact JSON body before hashing
if stub.Request.Body != nil {
bodyBytes, err := json.Marshal(stub.Request.Body)
if err != nil {
log.Error().Msgf("failed to marshal stub body: %v", err)
return []error{err}
}
buff := new(bytes.Buffer)
if err = json.Compact(buff, bodyBytes); err != nil {
log.Error().Msgf("failed to compact stub body: %v", err)
return []error{err}
}
stub.Request.Body = buff.String()
}
h := getHash(stub.Request)
if existing, ok := s.stubs[h]; ok {
log.Warn().Msgf("overriding existing stub: %s ", existing.Request.String())
}
s.stubs[h] = stub
log.Info().Msgf("added stub: %s", stub.Request.String())
return []error{}
}
// addStubs adds multiple stubs to the server.
func (s *Server) addStubs(stubs ...*Stub) {
for _, v := range stubs {
s.addStub(v)
}
}