-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
188 lines (157 loc) · 5.17 KB
/
controller.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
"sync"
"github.com/rs/zerolog/log"
)
var expectations Expectations
var mu sync.Mutex
// ControllerGetExpectations returns list with expectations in concurrent mode
func ControllerGetExpectations(expsInjection Expectations) Expectations {
if expsInjection != nil {
return expsInjection
}
mu.Lock()
defer mu.Unlock()
if expectations == nil {
expectations = make(Expectations)
}
return expectations
}
// ControllerAddExpectation adds new expectation to list. If expectation with same key exists, updates it
func ControllerAddExpectation(key string, exp Expectation, expsInjection Expectations) Expectations {
var exps = ControllerGetExpectations(expsInjection)
mu.Lock()
defer mu.Unlock()
exps[key] = exp
return exps
}
// ControllerRemoveExpectation removes expectation with particular key
func ControllerRemoveExpectation(key string, expsInjection Expectations) Expectations {
var exps = ControllerGetExpectations(expsInjection)
mu.Lock()
defer mu.Unlock()
if _, ok := exps[key]; ok {
delete(exps, key)
}
return exps
}
// ControllerTranslateHTTPHeadersToExpHeaders translates http headers into custom headers map
func ControllerTranslateHTTPHeadersToExpHeaders(httpHeader http.Header) *Headers {
headers := Headers{}
for name, headerLine := range httpHeader {
headers[name] = strings.Join(headerLine, ",")
}
return &headers
}
// ControllerTranslateRequestToExpectation Translates http request to expectation request
func ControllerTranslateRequestToExpectation(r *http.Request) *ExpectationRequest {
var expRequest = ExpectationRequest{}
expRequest.Method = r.Method
expRequest.Path = r.URL.RequestURI()
if len(r.URL.Fragment) > 0 {
expRequest.Path += "#" + r.URL.Fragment
}
// Buffer the body
if r.Body != nil {
bodyBuffer, error := ioutil.ReadAll(r.Body)
if error == nil {
expRequest.Body = string(bodyBuffer)
}
}
if len(r.Header) > 0 {
expRequest.Headers = ControllerTranslateHTTPHeadersToExpHeaders(r.Header)
}
return &expRequest
}
// ControllerStringPassesFilter validates whether the input string has filter string as substring or as a regex
func ControllerStringPassesFilter(str string, filter string) bool {
r, error := regexp.Compile("(?s)" + filter)
if error != nil {
return strings.Contains(str, filter)
}
return r.Match([]byte(str))
}
// ControllerRequestPassesFilter validates whether the incoming request passes particular filter
func ControllerRequestPassesFilter(req *ExpectationRequest, storedExpectation *ExpectationRequest) bool {
fLog := log.With().Str("function", "ControllerRequestPassesFilter").Logger()
if storedExpectation == nil {
fLog.Debug().Msg("Stored expectation.request is nil")
return true
}
if len(storedExpectation.Method) > 0 && storedExpectation.Method != req.Method {
fLog.Info().Msgf("method %s should be %s", req.Method, storedExpectation.Method)
return false
}
if len(storedExpectation.Path) > 0 && !ControllerStringPassesFilter(req.Path, storedExpectation.Path) {
fLog.Info().Msgf("path %s doesn't pass filter %s", req.Path, storedExpectation.Path)
return false
}
if len(storedExpectation.Body) > 0 && !ControllerStringPassesFilter(req.Body, storedExpectation.Body) {
fLog.Info().Msgf("body %s doesn't pass filter %s", req.Body, storedExpectation.Body)
return false
}
if storedExpectation.Headers != nil {
if req.Headers == nil {
fLog.Info().Msgf("Request is expected to contain headers")
return false
}
for storedHeaderName, storedHeaderValue := range *storedExpectation.Headers {
value, ok := (*req.Headers)[storedHeaderName]
if !ok {
fLog.Info().Msgf("No header %s in the request headers %v", storedHeaderName, req.Headers)
return false
}
if !ControllerStringPassesFilter(value, storedHeaderValue) {
fLog.Info().Msgf("header %s:%s has been rejected. Expected header value %s", storedHeaderName, value, storedHeaderValue)
return false
}
}
}
return true
}
// ControllerSortExpectationsByPriority returns map with int keys sorted by priority DESC.
// 0-indexed element has the highest priority
func ControllerSortExpectationsByPriority(exps Expectations) ExpectationsInt {
listForSorting := ExpectationsInt{}
i := 0
for _, exp := range exps {
listForSorting[i] = exp
i++
}
sort.Sort(listForSorting)
return listForSorting
}
// ControllerCreateHTTPRequest creates an http request based on incoming request and forward rules
func ControllerCreateHTTPRequest(req *ExpectationRequest, fwd *ExpectationForward) *http.Request {
fLog := log.With().Str("function", "ControllerCreateHTTPRequest").Logger()
fwdURL, err := url.Parse(fmt.Sprintf("%s://%s%s", fwd.Scheme, fwd.Host, req.Path))
if err != nil {
fLog.Panic().Err(err)
return nil
}
fLog.Info().Msgf("Send request to %s", fwdURL)
httpReq, err := http.NewRequest(req.Method, fwdURL.String(), bytes.NewBuffer([]byte(req.Body)))
if err != nil {
fLog.Panic().Err(err)
return nil
}
if req.Headers != nil {
for name, value := range *req.Headers {
httpReq.Header.Set(name, value)
}
}
if fwd.Headers != nil {
for name, value := range *fwd.Headers {
httpReq.Header.Set(name, value)
}
}
return httpReq
}