2020-12-26 13:11:49 +00:00
|
|
|
//go:generate go-enum -f $GOFILE --lower --marshal --names
|
2021-01-13 17:07:04 +00:00
|
|
|
package mock
|
2020-04-01 02:08:21 +00:00
|
|
|
|
2020-04-06 22:17:07 +00:00
|
|
|
import (
|
2020-12-26 13:11:49 +00:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2020-04-06 22:17:07 +00:00
|
|
|
"regexp"
|
2020-12-26 13:11:49 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2020-04-06 22:17:07 +00:00
|
|
|
)
|
2020-04-01 02:08:21 +00:00
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
var (
|
|
|
|
ruleValueSelectors = map[RequestMatchTarget]ruleValueSelector{
|
|
|
|
RequestMatchTargetHeader: func(req *http.Request, targetKey string) string {
|
|
|
|
return req.Header.Get(targetKey)
|
|
|
|
},
|
|
|
|
RequestMatchTargetPath: func(req *http.Request, _ string) string {
|
|
|
|
return req.URL.Path
|
|
|
|
},
|
|
|
|
}
|
2020-04-01 02:08:21 +00:00
|
|
|
)
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
/* ENUM(
|
|
|
|
Path,
|
|
|
|
Header
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
type RequestMatchTarget int
|
|
|
|
|
|
|
|
func (x RequestMatchTarget) Matches(req *http.Request, targetKey string, regex *regexp.Regexp) bool {
|
|
|
|
val := ruleValueSelectors[x](req, targetKey)
|
|
|
|
return regex.MatchString(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ruleValueSelector func(req *http.Request, targetKey string) string
|
|
|
|
|
2020-04-01 02:08:21 +00:00
|
|
|
type targetRule struct {
|
2020-12-26 13:11:49 +00:00
|
|
|
pattern *regexp.Regexp
|
|
|
|
response string
|
|
|
|
requestMatchTarget RequestMatchTarget
|
|
|
|
targetKey string
|
2020-04-06 22:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tr targetRule) Pattern() *regexp.Regexp {
|
|
|
|
return tr.pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr targetRule) Response() string {
|
|
|
|
return tr.response
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpOptions struct {
|
|
|
|
Rules []targetRule
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func loadFromConfig(config *viper.Viper) (options httpOptions, err error) {
|
|
|
|
type tmpCfg struct {
|
|
|
|
Pattern string
|
|
|
|
Response string
|
|
|
|
Matcher string
|
|
|
|
Target string
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpRules := struct {
|
|
|
|
Rules []tmpCfg
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err = config.Unmarshal(&tmpRules); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-01 02:08:21 +00:00
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
for _, i := range tmpRules.Rules {
|
|
|
|
var rulePattern *regexp.Regexp
|
|
|
|
var matchTargetValue RequestMatchTarget
|
|
|
|
var absoluteResponsePath string
|
|
|
|
var parseErr error
|
|
|
|
if rulePattern, parseErr = regexp.Compile(i.Pattern); parseErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if matchTargetValue, parseErr = ParseRequestMatchTarget(i.Matcher); parseErr != nil {
|
|
|
|
matchTargetValue = RequestMatchTargetPath
|
|
|
|
}
|
|
|
|
|
|
|
|
if absoluteResponsePath, parseErr = filepath.Abs(i.Response); parseErr != nil {
|
2021-01-07 21:00:12 +00:00
|
|
|
continue
|
2020-04-06 22:17:07 +00:00
|
|
|
}
|
2020-12-26 13:11:49 +00:00
|
|
|
|
|
|
|
options.Rules = append(options.Rules, targetRule{
|
|
|
|
pattern: rulePattern,
|
|
|
|
response: absoluteResponsePath,
|
|
|
|
requestMatchTarget: matchTargetValue,
|
|
|
|
targetKey: i.Target,
|
|
|
|
})
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 01:51:41 +00:00
|
|
|
return
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|