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
fmt
  • Loading branch information
jcdietrich committed Nov 6, 2024
commit 3c57ce3e7a8d0c65ccd770ba27b819cd6a2a5216
14 changes: 7 additions & 7 deletions internal/server/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ func (di *Dispatcher) ServeHTTP(w http.ResponseWriter, req *http.Request) {
statistics.TrackScenarioFeature()

di.Scenario.SetState(
mock.Control.Scenario.Name,
mock.Control.Scenario.NewState)
mock.Control.Scenario.Name,
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)
}
log.Debugf("Setting Scenario values: %v", mock.Control.Scenario.Values)
di.Scenario.SetStateValues(
mock.Control.Scenario.Name,
mock.Control.Scenario.Values)
}
}

if mock.Control.WebHookURL != "" {
Expand Down
5 changes: 2 additions & 3 deletions pkg/match/scenario_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewInMemoryScenarioStore() *InMemoryScenarioStore {
status := make(map[string]string)
values := make(map[string]map[string]string)
return &InMemoryScenarioStore{
status: status,
values: values,
status: status,
values: values,
}
}

Expand Down Expand Up @@ -102,7 +102,6 @@ func (sm *InMemoryScenarioStore) GetStateValue(name, valueName string) (string,
return "", false
}


func (sm *InMemoryScenarioStore) GetPaused() bool {
return sm.paused
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/mock/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ type Callback struct {
}

type Scenario struct {
Name string `json:"name"`
Values ScenarioValues `json:"values"`
RequiredState []string `json:"requiredState"`
NewState string `json:"newState"`
Name string `json:"name"`
Values ScenarioValues `json:"values"`
RequiredState []string `json:"requiredState"`
NewState string `json:"newState"`
}

type Delay struct {
Expand Down
37 changes: 18 additions & 19 deletions pkg/vars/dummyScenarioStorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,60 @@ import (
)

type DummyScenarioStorer struct {
Name string
Values mock.ScenarioValues
Name string
Values mock.ScenarioValues
}

func NewDummyScenarioStorer(name string, values mock.ScenarioValues) DummyScenarioStorer {
result := DummyScenarioStorer{
Name: name,
Values: values,
}
result := DummyScenarioStorer{
Name: name,
Values: values,
}

return result
return result
}

func (dss DummyScenarioStorer) SetState(name, status string) {
return
return
}

func (dss DummyScenarioStorer) GetState(name string) string {
return ""
return ""
}

func (dss DummyScenarioStorer) SetStateValues(name string, values map[string]string) {
return
return

}

func (dss DummyScenarioStorer) SetStateValue(name, valueName, value string) {
return
return
}

func (dss DummyScenarioStorer) GetStateValues(name string) map[string]string {
return make(map[string]string)
return make(map[string]string)
}

func (dss DummyScenarioStorer) GetStateValue(name, valueName string) (string, bool) {
return "", false
return "", false
}

func (dss DummyScenarioStorer) Reset(name string) bool {
return true
return true
}

func (dss DummyScenarioStorer) ResetAll() {
return
return
}

func (dss DummyScenarioStorer) SetPaused(newstate bool) {
return
return
}

func (dss DummyScenarioStorer) GetPaused() bool {
return false
return false
}

func (dss DummyScenarioStorer) List() string {
return ""
return ""
}

13 changes: 6 additions & 7 deletions pkg/vars/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"regexp"
"strings"

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

var varsRegex = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
Expand Down Expand Up @@ -44,7 +44,7 @@ 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)...)
holders = append(holders, fp.walkAndGetScenario(m.Control.Scenario)...)

//fill holders with the correct values
vars = requestFiller.Fill(holders)
Expand Down Expand Up @@ -97,21 +97,20 @@ func (fp ResponseMessageEvaluator) walkAndFill(res *mock.HTTPEntity, vars map[st

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

func (fp ResponseMessageEvaluator) walkAndFillScenario(
scenario *mock.Scenario,
vars map[string][]string){
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
29 changes: 14 additions & 15 deletions pkg/vars/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func getProcessor() ResponseMessageEvaluator {
return ResponseMessageEvaluator{FillerFactory: ff}
}


func TestReplaceTags(t *testing.T) {

req := mock.Request{}
Expand Down Expand Up @@ -228,9 +227,9 @@ func TestReplaceTags(t *testing.T) {

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test001", scenarioValues)

varsProcessor := getProcessor()
varsProcessor.Eval(&req, &mock, storer )
varsProcessor.Eval(&req, &mock, storer)

if mock.Response.Body != "Request Body hi!. Query valParam. Cookie: valCookie. Random: AleixMG" {
t.Error("Replaced tags in body not match", res.Body)
Expand Down Expand Up @@ -271,7 +270,7 @@ func TestReplaceUndefinedFakeTag(t *testing.T) {
res.Body = "Request {{request.query.param2}}. Cookie: {{request.cookie.cookie2}}. Random: {{fake.otherOption}}"

mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand All @@ -298,7 +297,7 @@ func TestReplaceTagWithSpace(t *testing.T) {
res.Body = "Request {{ request.query.param1}}. Cookie: {{request.cookie.cookie1 }}. Random: {{fake.UserName }}"

mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand Down Expand Up @@ -340,7 +339,7 @@ func TestReplaceTagWithParameter(t *testing.T) {
res.Body = "Random: {{fake.CharactersN(15)}}"

m := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand All @@ -359,7 +358,7 @@ func TestReplaceTagWithParameterNoParameterPassed(t *testing.T) {
res.Body = "Random: {{fake.CharactersN}}"

mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand All @@ -378,7 +377,7 @@ func TestReplaceMissingTags(t *testing.T) {
res.Body = "Request Body {{request.body}}. Query {{request.query.param1}}. Cookie: {{request.cookie.cookie1}}."

mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand All @@ -400,7 +399,7 @@ func TestReplaceFormUrlEncodedBodyTags(t *testing.T) {
res.Body = "Form data placeholders. One '{{request.body.one}}'. Two '{{request.body.two[array]}}'."

mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand Down Expand Up @@ -431,7 +430,7 @@ func TestReplaceUrlInfo(t *testing.T) {
res.Body = "{{request.scheme}}://{{request.hostname}}:{{request.port}}{{request.path}}#{{request.fragment}}"

m := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand All @@ -446,7 +445,7 @@ func TestReplaceUrlInfo(t *testing.T) {
res.Body = "{{request.url}}"

m = mock.Definition{Request: req, Response: res}

varsProcessor.Eval(&req, &m, storer)

if m.Response.Body != "ws://example.com:8001/home?param1=valParam1&param1=valParam2&param2=valParam1#anchor" {
Expand Down Expand Up @@ -532,7 +531,7 @@ func TestReplaceJsonBodyEncodedTags(t *testing.T) {
}
`
mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand Down Expand Up @@ -614,7 +613,7 @@ func TestReplaceXmlBodyEncodedTags(t *testing.T) {
</root>
`
mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand Down Expand Up @@ -642,7 +641,7 @@ func TestReplaceTagsCallback(t *testing.T) {
cb.Body = "Request Body {{request.body}}. Query {{request.query.param1}}. Cookie: {{request.cookie.cookie1}}. Random: {{fake.UserName}}"

mock := mock.Definition{Request: req, Callback: cb}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand Down Expand Up @@ -675,7 +674,7 @@ func TestReplaceBigFile(t *testing.T) {
res.Body = fmt.Sprintf("Big file: {{file.contents(%s)}}", tmpfn)

mock := mock.Definition{Request: req, Response: res}

scenarioValues := make(map[string]string)
storer := NewDummyScenarioStorer("test002", scenarioValues)

Expand Down
34 changes: 17 additions & 17 deletions pkg/vars/filler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package vars

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

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

Expand All @@ -44,19 +44,19 @@ func (mff MockFillerFactory) CreateStreamFiller() Filler {
}

func (mff MockFillerFactory) CreateResponseFiller(res *mock.Response) Filler {
return ResponseFiller{Response: res}
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,
}
req *mock.Request,
mock *mock.Definition,
scenarioStore match.ScenearioStorer,
scenarioName string) Filler {

return ScenarioFiller{
Mock: mock,
Request: req,
Name: scenarioName,
Store: scenarioStore,
}
}
Loading