api/internal/endpoint/handler/http/mock/protocol_options.go
Peter Kurfer d70ba748f5 Introduce Lifecycle for every endpoint and manage listeners in the renamed Orchestrator
- merge packages to get a more concise layout because plugins are no more and therefore there's not a lot to be exported
- fix test logger
- rework config parsing to be easier and more transparent
- remove unnecessary APIs because dynamic endpoint handling is rather a won't implement
2021-02-10 20:26:45 +00:00

97 lines
2.1 KiB
Go

//go:generate go-enum -f $GOFILE --lower --marshal --names
package mock
import (
"net/http"
"path/filepath"
"regexp"
"gitlab.com/inetmock/inetmock/internal/endpoint"
)
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
},
}
)
/* 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
type targetRule struct {
pattern *regexp.Regexp
response string
requestMatchTarget RequestMatchTarget
targetKey string
}
func (tr targetRule) Pattern() *regexp.Regexp {
return tr.pattern
}
func (tr targetRule) Response() string {
return tr.response
}
type httpOptions struct {
Rules []targetRule
}
func loadFromConfig(lifecycle endpoint.Lifecycle) (options httpOptions, err error) {
type tmpCfg struct {
Pattern string
Response string
Matcher string
Target string
}
tmpRules := struct {
Rules []tmpCfg
}{}
if err = lifecycle.UnmarshalOptions(&tmpRules); err != nil {
return
}
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 {
continue
}
options.Rules = append(options.Rules, targetRule{
pattern: rulePattern,
response: absoluteResponsePath,
requestMatchTarget: matchTargetValue,
targetKey: i.Target,
})
}
return
}