From a94c835d5d02a31f17b927b22e5acee2c3cfba41 Mon Sep 17 00:00:00 2001 From: lansfy <5764541+lansfy@users.noreply.github.com> Date: Fri, 3 Jan 2025 22:09:19 -0700 Subject: [PATCH] fix: removed not needed dependency from github.com/Masterminds/sprig --- .github/workflows/ci-build.yml | 3 +- go.mod | 12 +----- go.sum | 36 ---------------- mocks/loader.go | 54 +++++++++++++++++++----- mocks/mocks.go | 7 +-- mocks/service_mock.go | 11 ----- mocks/strategy_based_on_request_reply.go | 4 +- mocks/strategy_constant_reply.go | 4 +- mocks/strategy_constant_reply_test.go | 4 +- mocks/strategy_drop_request.go | 2 +- mocks/strategy_method_vary_reply.go | 4 +- mocks/strategy_sequential_reply.go | 4 +- mocks/strategy_template_reply.go | 11 +++-- mocks/strategy_uri_vary_reply.go | 4 +- runner/runner.go | 3 +- runner/runner_testing.go | 10 ++++- 16 files changed, 79 insertions(+), 94 deletions(-) diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index e340d27..4f377da 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -17,7 +17,8 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - go: ['1.18.x', '1.23.x'] + #go: ['1.18.x', '1.23.x'] + go: ['1.23.x'] steps: - name: Set up Go diff --git a/go.mod b/go.mod index 5eb3b0f..e82beaf 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,11 @@ module github.com/lansfy/gonkex go 1.18 require ( - github.com/Masterminds/sprig/v3 v3.2.2 github.com/fatih/color v1.7.0 github.com/google/uuid v1.3.1 github.com/joho/godotenv v1.3.0 github.com/kylelemons/godebug v1.1.0 + github.com/mattn/go-colorable v0.1.12 github.com/stretchr/testify v1.7.1 github.com/tidwall/gjson v1.17.0 golang.org/x/sync v0.4.0 @@ -16,20 +16,10 @@ require ( ) require ( - github.com/Masterminds/goutils v1.1.1 // indirect - github.com/Masterminds/semver/v3 v3.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/huandu/xstrings v1.3.2 // indirect - github.com/imdario/mergo v0.3.13 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.16 // indirect - github.com/mitchellh/copystructure v1.2.0 // indirect - github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/shopspring/decimal v1.2.0 // indirect - github.com/spf13/cast v1.3.1 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect - golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect ) diff --git a/go.sum b/go.sum index 3f6a5fd..64e0d0c 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,10 @@ -github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= -github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= -github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= -github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= -github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8= -github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= -github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= -github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= -github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= -github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= -github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= @@ -27,21 +14,9 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= -github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= -github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= -github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= -github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= -github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= -github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= -github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= @@ -50,27 +25,16 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= -golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mocks/loader.go b/mocks/loader.go index ce760cf..3fd27ea 100644 --- a/mocks/loader.go +++ b/mocks/loader.go @@ -3,9 +3,40 @@ package mocks import ( "errors" "fmt" + "text/template" ) -func loadDefinition(path string, rawDef interface{}) (*Definition, error) { +type Loader interface { + LoadDefinition(rawDef interface{}) (*Definition, error) +} + +type YamlLoaderOpts struct { + TemplateReplyFuncs template.FuncMap +} + +func NewYamlLoader(opts *YamlLoaderOpts) Loader { + var funcs template.FuncMap + if opts != nil { + funcs = opts.TemplateReplyFuncs + } + return &loaderImpl{ + TemplateReplyFuncs: funcs, + } +} + +type loaderImpl struct { + TemplateReplyFuncs template.FuncMap +} + +func (l *loaderImpl) LoadDefinition(rawDef interface{}) (*Definition, error) { + def, err := l.loadDefinition("$", rawDef) + if err != nil { + return nil, err + } + return def, nil +} + +func (l *loaderImpl) loadDefinition(path string, rawDef interface{}) (*Definition, error) { def, ok := rawDef.(map[interface{}]interface{}) if !ok { return nil, fmt.Errorf("at path %s: Definition must be key-values", path) @@ -44,7 +75,7 @@ func loadDefinition(path string, rawDef interface{}) (*Definition, error) { return fmt.Errorf("strategy '%s': %w", strategyName, err) } - replyStrategy, err := loadStrategy(path, strategyName, def, &ak) + replyStrategy, err := l.loadStrategy(path, strategyName, def, &ak) if err != nil { return nil, wrap(err) } @@ -59,34 +90,35 @@ func loadDefinition(path string, rawDef interface{}) (*Definition, error) { return NewDefinition(path, requestConstraints, replyStrategy, callsConstraint), nil } -func loadStrategy(path, strategyName string, definition map[interface{}]interface{}, ak *[]string) (ReplyStrategy, error) { +func (l *loaderImpl) loadStrategy(path, strategyName string, definition map[interface{}]interface{}, + ak *[]string) (ReplyStrategy, error) { path = path + "." + strategyName switch strategyName { case "nop": return NewNopReply(), nil case "constant": *ak = append(*ak, "body", "statusCode", "headers") - return loadConstantStrategy(path, definition) + return l.loadConstantStrategy(path, definition) case "sequence": *ak = append(*ak, "sequence") - return loadSequenceReplyStrategy(path, definition) + return l.loadSequenceReplyStrategy(path, definition) case "template": *ak = append(*ak, "body", "statusCode", "headers") - return loadTemplateReplyStrategy(path, definition) + return l.loadTemplateReplyStrategy(path, definition) case "basedOnRequest": *ak = append(*ak, "basePath", "uris") - return loadBasedOnRequestReplyStrategy(path, definition) + return l.loadBasedOnRequestReplyStrategy(path, definition) case "file": *ak = append(*ak, "filename", "statusCode", "headers") - return loadFileStrategy(path, definition) + return l.loadFileStrategy(path, definition) case "uriVary": *ak = append(*ak, "basePath", "uris") - return loadUriVaryReplyStrategy(path, definition) + return l.loadUriVaryReplyStrategy(path, definition) case "methodVary": *ak = append(*ak, "methods") - return loadMethodVaryStrategy(path, definition) + return l.loadMethodVaryStrategy(path, definition) case "dropRequest": - return loadDropRequestStrategy(path, definition) + return l.loadDropRequestStrategy(path, definition) default: return nil, errors.New("unknown strategy") } diff --git a/mocks/mocks.go b/mocks/mocks.go index fc3a540..7937793 100644 --- a/mocks/mocks.go +++ b/mocks/mocks.go @@ -98,17 +98,18 @@ func (m *Mocks) GetNames() []string { return names } -func (m *Mocks) LoadDefinitions(definitions map[string]interface{}) error { +func (m *Mocks) LoadDefinitions(loader Loader, definitions map[string]interface{}) error { for serviceName, definition := range definitions { service := m.Service(serviceName) if service == nil { return fmt.Errorf("unknown mock name: %s", serviceName) } - err := service.LoadDefinition(definition) + def, err := loader.LoadDefinition(definition) if err != nil { - return err + return fmt.Errorf("load definition for '%s': %w", serviceName, err) } + service.SetDefinition(def) } return nil } diff --git a/mocks/service_mock.go b/mocks/service_mock.go index b511f80..73dd913 100644 --- a/mocks/service_mock.go +++ b/mocks/service_mock.go @@ -2,7 +2,6 @@ package mocks import ( "context" - "fmt" "net" "net/http" "sync" @@ -69,16 +68,6 @@ func (m *ServiceMock) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -func (m *ServiceMock) LoadDefinition(newDefinition interface{}) error { - def, err := loadDefinition("$", newDefinition) - if err != nil { - return fmt.Errorf("load definition for '%s': %w", m.ServiceName, err) - } - // load the Definition into the mock - m.SetDefinition(def) - return nil -} - func (m *ServiceMock) SetDefinition(newDefinition *Definition) { m.Lock() defer m.Unlock() diff --git a/mocks/strategy_based_on_request_reply.go b/mocks/strategy_based_on_request_reply.go index 6122e0e..c7bdeec 100644 --- a/mocks/strategy_based_on_request_reply.go +++ b/mocks/strategy_based_on_request_reply.go @@ -7,7 +7,7 @@ import ( "sync" ) -func loadBasedOnRequestReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadBasedOnRequestReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { var uris []*Definition if u, ok := def["uris"]; ok { urisList, ok := u.([]interface{}) @@ -20,7 +20,7 @@ func loadBasedOnRequestReplyStrategy(path string, def map[interface{}]interface{ if !ok { return nil, errors.New("`uris` list item must be a map") } - def, err := loadDefinition(path+"."+strconv.Itoa(i), v) + def, err := l.loadDefinition(path+"."+strconv.Itoa(i), v) if err != nil { return nil, err } diff --git a/mocks/strategy_constant_reply.go b/mocks/strategy_constant_reply.go index 65a9369..527541b 100644 --- a/mocks/strategy_constant_reply.go +++ b/mocks/strategy_constant_reply.go @@ -5,7 +5,7 @@ import ( "os" ) -func loadFileStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadFileStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { filename, err := getRequiredStringKey(def, "filename", false) if err != nil { return nil, err @@ -25,7 +25,7 @@ func loadFileStrategy(path string, def map[interface{}]interface{}) (ReplyStrate return NewConstantReplyWithCode(content, statusCode, headers), nil } -func loadConstantStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadConstantStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { body, err := getRequiredStringKey(def, "body", true) if err != nil { return nil, err diff --git a/mocks/strategy_constant_reply_test.go b/mocks/strategy_constant_reply_test.go index 7bb30bd..40a3e8b 100644 --- a/mocks/strategy_constant_reply_test.go +++ b/mocks/strategy_constant_reply_test.go @@ -54,6 +54,8 @@ body: "Multi-header test"`, }, } + loader := &loaderImpl{} + for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { // Create the reply strategy @@ -61,7 +63,7 @@ body: "Multi-header test"`, err := yaml.Unmarshal([]byte(tt.content), &def) require.NoError(t, err) - reply, err := loadConstantStrategy("$", def) + reply, err := loader.loadConstantStrategy("$", def) require.NoError(t, err) // Mock request and response diff --git a/mocks/strategy_drop_request.go b/mocks/strategy_drop_request.go index d5cc22b..c6ba0ba 100644 --- a/mocks/strategy_drop_request.go +++ b/mocks/strategy_drop_request.go @@ -5,7 +5,7 @@ import ( "net/http" ) -func loadDropRequestStrategy(path string, _ map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadDropRequestStrategy(path string, _ map[interface{}]interface{}) (ReplyStrategy, error) { return NewDropRequestReply(), nil } diff --git a/mocks/strategy_method_vary_reply.go b/mocks/strategy_method_vary_reply.go index 797b20f..e957029 100644 --- a/mocks/strategy_method_vary_reply.go +++ b/mocks/strategy_method_vary_reply.go @@ -6,7 +6,7 @@ import ( "strings" ) -func loadMethodVaryStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadMethodVaryStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { var methods map[string]*Definition if u, ok := def["methods"]; ok { methodsMap, ok := u.(map[interface{}]interface{}) @@ -15,7 +15,7 @@ func loadMethodVaryStrategy(path string, def map[interface{}]interface{}) (Reply } methods = make(map[string]*Definition, len(methodsMap)) for method, v := range methodsMap { - def, err := loadDefinition(path+"."+method.(string), v) + def, err := l.loadDefinition(path+"."+method.(string), v) if err != nil { return nil, err } diff --git a/mocks/strategy_sequential_reply.go b/mocks/strategy_sequential_reply.go index 4fb4dcc..5b9ee22 100644 --- a/mocks/strategy_sequential_reply.go +++ b/mocks/strategy_sequential_reply.go @@ -7,7 +7,7 @@ import ( "sync" ) -func loadSequenceReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadSequenceReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { if _, ok := def["sequence"]; !ok { return nil, errors.New("`sequence` key required") } @@ -17,7 +17,7 @@ func loadSequenceReplyStrategy(path string, def map[interface{}]interface{}) (Re } strategies := make([]*Definition, len(seqSlice)) for i, v := range seqSlice { - def, err := loadDefinition(path+"."+strconv.Itoa(i), v) + def, err := l.loadDefinition(path+"."+strconv.Itoa(i), v) if err != nil { return nil, err } diff --git a/mocks/strategy_template_reply.go b/mocks/strategy_template_reply.go index 92ff3b5..0f97bf0 100644 --- a/mocks/strategy_template_reply.go +++ b/mocks/strategy_template_reply.go @@ -7,11 +7,9 @@ import ( "net/http" "sync" "text/template" - - "github.com/Masterminds/sprig/v3" ) -func loadTemplateReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadTemplateReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { body, err := getRequiredStringKey(def, "body", true) if err != nil { return nil, err @@ -24,11 +22,12 @@ func loadTemplateReplyStrategy(path string, def map[interface{}]interface{}) (Re if err != nil { return nil, err } - return NewTemplateReply(body, statusCode, headers) + return NewTemplateReply(body, statusCode, headers, l.TemplateReplyFuncs) } -func NewTemplateReply(content string, statusCode int, headers map[string]string) (ReplyStrategy, error) { - tmpl, err := template.New("").Funcs(sprig.GenericFuncMap()).Parse(content) +func NewTemplateReply(content string, statusCode int, headers map[string]string, + funcs template.FuncMap) (ReplyStrategy, error) { + tmpl, err := template.New("").Funcs(funcs).Parse(content) if err != nil { return nil, fmt.Errorf("template syntax error: %w", err) } diff --git a/mocks/strategy_uri_vary_reply.go b/mocks/strategy_uri_vary_reply.go index 7290276..db34974 100644 --- a/mocks/strategy_uri_vary_reply.go +++ b/mocks/strategy_uri_vary_reply.go @@ -6,7 +6,7 @@ import ( "strings" ) -func loadUriVaryReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { +func (l *loaderImpl) loadUriVaryReplyStrategy(path string, def map[interface{}]interface{}) (ReplyStrategy, error) { basePath, err := getOptionalStringKey(def, "basePath", true) if err != nil { return nil, err @@ -19,7 +19,7 @@ func loadUriVaryReplyStrategy(path string, def map[interface{}]interface{}) (Rep } uris = make(map[string]*Definition, len(urisMap)) for uri, v := range urisMap { - def, err := loadDefinition(path+"."+uri.(string), v) + def, err := l.loadDefinition(path+"."+uri.(string), v) if err != nil { return nil, err } diff --git a/runner/runner.go b/runner/runner.go index e4661fd..2a399fc 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -24,6 +24,7 @@ type Config struct { FixturesDir string DB storage.StorageInterface Mocks *mocks.Mocks + MocksLoader mocks.Loader Variables *variables.Variables HTTPProxyURL *url.URL } @@ -137,7 +138,7 @@ func (r *Runner) executeTest(v models.TestInterface) (*models.Result, error) { // load mocks if v.ServiceMocks() != nil { - if err := r.config.Mocks.LoadDefinitions(v.ServiceMocks()); err != nil { + if err := r.config.Mocks.LoadDefinitions(r.config.MocksLoader, v.ServiceMocks()); err != nil { return nil, err } } diff --git a/runner/runner_testing.go b/runner/runner_testing.go index bdd446f..6c07dae 100644 --- a/runner/runner_testing.go +++ b/runner/runner_testing.go @@ -7,6 +7,7 @@ import ( "os" "strings" "testing" + "text/template" "github.com/lansfy/gonkex/checker" "github.com/lansfy/gonkex/checker/response_body" @@ -35,6 +36,8 @@ type RunWithTestingOpts struct { MainOutputFunc output.OutputInterface Outputs []output.OutputInterface Checkers []checker.CheckerInterface + + TemplateReplyFuncs template.FuncMap } func registerMocksEnvironment(m *mocks.Mocks) { @@ -73,8 +76,11 @@ func RunWithTesting(t *testing.T, serverURL string, opts *RunWithTestingOpts) { handler := testingHandler{t} runner := New( &Config{ - Host: serverURL, - Mocks: opts.Mocks, + Host: serverURL, + Mocks: opts.Mocks, + MocksLoader: mocks.NewYamlLoader(&mocks.YamlLoaderOpts{ + TemplateReplyFuncs: opts.TemplateReplyFuncs, + }), FixturesDir: opts.FixturesDir, DB: opts.DB, Variables: variables.New(),