Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenario Values & Reading from Response #178

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
WIP scenario store now passed to evaluator
  • Loading branch information
jcdietrich committed Nov 6, 2024
commit d99673db1fade2c8ec440ed0f995519d0fe716ae
2 changes: 1 addition & 1 deletion internal/server/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition
statistics.TrackProxyFeature()
response = getProxyResponse(request, mock)
} else {
di.Evaluator.Eval(request, mock)
di.Evaluator.Eval(request, mock, di.Scenario)
response = &mock.Response
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/match/scenario_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ type ScenearioStorer interface {
SetStateValues(name string, values map[string]string)
SetStateValue(name, valueName, value string)
GetStateValues(name string) map[string]string
GetStateValue(name, valueName string) string
GetStateValue(name, valueName string) (string, bool)
Reset(name string) bool
ResetAll()
SetPaused(newstate bool)
GetPaused() bool
List() string
}

type ScenearioStore struct {
}

func NewInMemoryScenarioStore() *InMemoryScenarioStore {
status := make(map[string]string)
values := make(map[string]map[string]string)
Expand All @@ -32,6 +35,7 @@ type InMemoryScenarioStore struct {
status map[string]string
values map[string]map[string]string
paused bool
*ScenearioStore
}

func (sm *InMemoryScenarioStore) List() string {
Expand Down Expand Up @@ -90,12 +94,12 @@ func (sm *InMemoryScenarioStore) GetStateValues(name string) map[string]string {
return make(map[string]string)
}

func (sm *InMemoryScenarioStore) GetStateValue(name, valueName string) string {
func (sm *InMemoryScenarioStore) GetStateValue(name, valueName string) (string, bool) {
if v, f := sm.values[strings.ToLower(name)][strings.ToLower(valueName)]; f {
return v
return v, true
}

return ""
return "", false
}


Expand Down
12 changes: 9 additions & 3 deletions pkg/vars/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"strings"

"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/match"
)

var varsRegex = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)

type Evaluator interface {
Eval(req *mock.Request, m *mock.Definition)
Eval(req *mock.Request, m *mock.Definition, scenearioStore match.ScenearioStorer)
}

type ResponseMessageEvaluator struct {
Expand All @@ -21,7 +22,7 @@ func NewResponseMessageEvaluator(fp FillerFactory) *ResponseMessageEvaluator {
return &ResponseMessageEvaluator{FillerFactory: fp}
}

func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition) {
func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition, store match.ScenearioStorer) {
requestFiller := fp.FillerFactory.CreateRequestFiller(req, m)
fakeFiller := fp.FillerFactory.CreateFakeFiller()
streamFiller := fp.FillerFactory.CreateStreamFiller()
Expand All @@ -44,7 +45,12 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition) {
vars = requestFiller.Fill(holders)
fp.mergeVars(vars, fakeFiller.Fill(holders))

//replace the holders in the response
if m.Control.Scenario.Name != "" {
scenarioFiller := fp.FillerFactory.CreateScenarioFiller(req, m, store, m.Control.Scenario.Name)
fp.mergeVars(vars, scenarioFiller.Fill(holders))
}

//replace the holders in the response and the callback
fp.walkAndFill(&m.Response.HTTPEntity, vars)

// fill any response.* holders
Expand Down
21 changes: 21 additions & 0 deletions pkg/vars/filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vars

import (
"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/match"
"github.com/jmartin82/mmock/v3/pkg/vars/fake"
)

Expand All @@ -14,6 +15,12 @@ type FillerFactory interface {
CreateFakeFiller() Filler
CreateStreamFiller() Filler
CreateResponseFiller(res *mock.Response) Filler
CreateScenarioFiller(
req *mock.Request,
mock *mock.Definition,
scenarioStore match.ScenearioStorer,
scenarioName string,
) Filler
}

type MockFillerFactory struct {
Expand All @@ -39,3 +46,17 @@ func (mff MockFillerFactory) CreateStreamFiller() Filler {
func (mff MockFillerFactory) CreateResponseFiller(res *mock.Response) Filler {
return ResponseFiller{Response: res}
}

func (mff MockFillerFactory) CreateScenarioFiller(
req *mock.Request,
mock *mock.Definition,
scenarioStore match.ScenearioStorer,
scenarioName string) Filler {

return ScenarioFiller{
Mock: mock,
Request: req,
Name: scenarioName,
Store: scenarioStore,
}
}
32 changes: 32 additions & 0 deletions pkg/vars/scenario.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package vars

import (
"strings"
"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/match"

)

type ScenarioFiller struct {
Mock *mock.Definition
Request *mock.Request
Name string
Store match.ScenearioStorer
}

func (sf ScenarioFiller) Fill(holders []string) map[string][]string {
vars := make(map[string][]string)
for _, tag := range holders {
found := false
s := ""
if strings.HasPrefix(tag, "scenario.") {
s, found = sf.Store.GetStateValue(sf.Name, tag[8:])
}

if found {
vars[tag] = append(vars[tag], s)
}

}
return vars
}