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
fix scenario values, and add some logs
  • Loading branch information
jcdietrich committed Nov 6, 2024
commit 248da0770508228aa8e1a9a60f36f3372c039ad7
1 change: 1 addition & 0 deletions internal/server/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (di *Dispatcher) ServeHTTP(w http.ResponseWriter, req *http.Request) {
mock.Control.Scenario.NewState)

if len(mock.Control.Scenario.Values) != 0 {
log.Debugf("Setting Scenario values: %v", mock.Control.Scenario.Values)
di.Scenario.SetStateValues(
mock.Control.Scenario.Name,
mock.Control.Scenario.Values)
Expand Down
1 change: 1 addition & 0 deletions pkg/match/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (mm Request) matchScenarioState(scenario *mock.Scenario) bool {
currentState := mm.scenario.GetState(scenario.Name)
for _, r := range scenario.RequiredState {
if strings.ToLower(r) == currentState {
log.Debugf("Scenario %v has matched %v state.", scenario.Name, currentState)
return true
}
}
Expand Down
33 changes: 25 additions & 8 deletions pkg/vars/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition, s
fakeFiller := fp.FillerFactory.CreateFakeFiller()
streamFiller := fp.FillerFactory.CreateStreamFiller()
responseFiller := fp.FillerFactory.CreateResponseFiller(&m.Response)
scenarioFiller := fp.FillerFactory.CreateScenarioFiller(req, m, store, m.Control.Scenario.Name)

//first replace the external streams
holders := fp.walkAndGet(m.Response.HTTPEntity)
Expand All @@ -43,25 +44,24 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition, s
//get the holders in the response and the callback structs
holders = fp.walkAndGet(m.Response.HTTPEntity)
holders = append(holders, fp.walkAndGet(m.Callback.HTTPEntity)...)
holders = append(holders, fp.walkAndGetScenario(m.Control.Scenario)...)

//fill holders with the correct values
vars = requestFiller.Fill(holders)
fp.mergeVars(vars, fakeFiller.Fill(holders))
fp.mergeVars(vars, scenarioFiller.Fill(holders))

// if we have a scenario, fill any scenario.* holders
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
//replace the holders in the response
fp.walkAndFill(&m.Response.HTTPEntity, vars)

// fill any response.* holders
fp.mergeVars(vars, responseFiller.Fill(holders))

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

//replace the holders in the Scenario
fp.walkAndFillScenario(&m.Control.Scenario, vars)
}

func (fp ResponseMessageEvaluator) walkAndGet(res mock.HTTPEntity) []string {
Expand Down Expand Up @@ -95,6 +95,23 @@ func (fp ResponseMessageEvaluator) walkAndFill(res *mock.HTTPEntity, vars map[st
res.Body = fp.replaceVars(res.Body, vars)
}

func (fp ResponseMessageEvaluator) walkAndGetScenario(scenario mock.Scenario) []string {
vars := []string{}
for _, value := range scenario.Values {
fp.extractVars(value, &vars)
}
return vars
}

func (fp ResponseMessageEvaluator) walkAndFillScenario(
scenario *mock.Scenario,
vars map[string][]string){
for valueName, value := range scenario.Values {
scenario.Values[valueName] = fp.replaceVars(value, vars)
}
}


func (fp ResponseMessageEvaluator) replaceVars(input string, vars map[string][]string) string {
return varsRegex.ReplaceAllStringFunc(input, func(value string) string {
varName := strings.Trim(value, "{} ")
Expand Down