diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 423e423..0ee4bb9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,9 @@ jobs: - name: Test with coverage run: make coverage - - - name: Upload coverage to Codecov - run: bash <(curl -s https://codecov.io/bash) + - name: Upload coverage reports to Codecov + run: | + # Replace `linux` below with the appropriate OS + curl -Os https://uploader.codecov.io/latest/linux/codecov + chmod +x codecov + ./codecov -t ${CODECOV_TOKEN} \ No newline at end of file diff --git a/internal/server/handle_callback_test.go b/internal/server/handle_callback_test.go index 5a08562..da22b12 100644 --- a/internal/server/handle_callback_test.go +++ b/internal/server/handle_callback_test.go @@ -45,9 +45,9 @@ func TestPost(t *testing.T) { defer ts.Close() cb := mock.Callback{ - Url: ts.URL, - Method: "POST", - Body: "Some post body", + Url: ts.URL, + Method: "POST", + HTTPEntity: mock.HTTPEntity{Body: "Some post body"}, } resp, err := HandleCallback(cb) diff --git a/pkg/mock/definition.go b/pkg/mock/definition.go index c5c909c..65c6e9c 100644 --- a/pkg/mock/definition.go +++ b/pkg/mock/definition.go @@ -16,6 +16,11 @@ type HttpHeaders struct { Cookies Cookies `json:"cookies"` } +type HTTPEntity struct { + HttpHeaders + Body string `json:"body"` +} + type Request struct { Scheme string `json:"scheme"` Host string `json:"host"` @@ -24,23 +29,20 @@ type Request struct { Path string `json:"path"` QueryStringParameters Values `json:"queryStringParameters"` Fragment string `json:"fragment"` - HttpHeaders - Body string `json:"body"` + HTTPEntity } type Response struct { StatusCode int `json:"statusCode"` - HttpHeaders - Body string `json:"body"` + HTTPEntity } type Callback struct { Delay Delay `json:"delay"` Method string `json:"method"` Url string `json:"url"` - HttpHeaders - Body string `json:"body"` - Timeout Delay `json:"timeout"` + HTTPEntity + Timeout Delay `json:"timeout"` } type Scenario struct { diff --git a/pkg/vars/evaluator.go b/pkg/vars/evaluator.go index 4a7a887..5802047 100644 --- a/pkg/vars/evaluator.go +++ b/pkg/vars/evaluator.go @@ -26,20 +26,27 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition) { fakeFiller := fp.FillerFactory.CreateFakeFiller() streamFiller := fp.FillerFactory.CreateStreamFiller() - //replace stream holders for their content - fp.walkAndFill(m, streamFiller.Fill(fp.walkAndGet(m.Response))) + //first replace the external streams + holders := fp.walkAndGet(m.Response.HTTPEntity) + vars := streamFiller.Fill(holders) + fp.walkAndFill(&m.Response.HTTPEntity, vars) - //extract holders - holders := fp.walkAndGet(m.Response) - fp.extractVars(m.Callback.Body, &holders) + //repeat the same opration in order to replace any holder coming from the external streams - //fill holders - vars := requestFiller.Fill(holders) + //get the holders in the response and the callback structs + holders = fp.walkAndGet(m.Response.HTTPEntity) + holders = append(holders, fp.walkAndGet(m.Callback.HTTPEntity)...) + + //fill holders with the correct values + vars = requestFiller.Fill(holders) fp.mergeVars(vars, fakeFiller.Fill(holders)) - fp.walkAndFill(m, vars) + + //replace the holders in the response and the callback + fp.walkAndFill(&m.Response.HTTPEntity, vars) + fp.walkAndFill(&m.Callback.HTTPEntity, vars) } -func (fp ResponseMessageEvaluator) walkAndGet(res mock.Response) []string { +func (fp ResponseMessageEvaluator) walkAndGet(res mock.HTTPEntity) []string { vars := []string{} for _, header := range res.Headers { @@ -56,8 +63,7 @@ func (fp ResponseMessageEvaluator) walkAndGet(res mock.Response) []string { return vars } -func (fp ResponseMessageEvaluator) walkAndFill(m *mock.Definition, vars map[string][]string) { - res := &m.Response +func (fp ResponseMessageEvaluator) walkAndFill(res *mock.HTTPEntity, vars map[string][]string) { for header, values := range res.Headers { for i, value := range values { res.Headers[header][i] = fp.replaceVars(value, vars) @@ -69,7 +75,6 @@ func (fp ResponseMessageEvaluator) walkAndFill(m *mock.Definition, vars map[stri } res.Body = fp.replaceVars(res.Body, vars) - m.Callback.Body = fp.replaceVars(m.Callback.Body, vars) } func (fp ResponseMessageEvaluator) replaceVars(input string, vars map[string][]string) string { diff --git a/pkg/vars/evaluator_test.go b/pkg/vars/evaluator_test.go index 24e1cef..dbaefdb 100644 --- a/pkg/vars/evaluator_test.go +++ b/pkg/vars/evaluator_test.go @@ -194,8 +194,10 @@ func TestReplaceTags(t *testing.T) { req := mock.Request{} req.Body = "hi!" + val := make(mock.Values) val["param1"] = []string{"valParam"} + req.QueryStringParameters = val cookie := make(mock.Cookies) @@ -203,20 +205,25 @@ func TestReplaceTags(t *testing.T) { req.Cookies = cookie res := mock.Response{} + cb := mock.Callback{} res.Body = "Request Body {{request.body}}. Query {{request.query.param1}}. Cookie: {{request.cookie.cookie1}}. Random: {{fake.UserName}}" + cb.Body = "Callback Body {{request.body}}. Query {{request.query.param1}}. Cookie: {{request.cookie.cookie1}}. Random: {{fake.UserName}}" cookie = make(mock.Cookies) cookie["cookie1"] = "valCookie" cookie["cookie2"] = "{{fake.UserName}}" + res.Cookies = cookie + cb.Cookies = cookie val = make(mock.Values) val["header1"] = []string{"valHeader"} val["header2"] = []string{"valHeader", "{{request.query.param1}}"} res.Headers = val + cb.Headers = val - mock := mock.Definition{Request: req, Response: res} + mock := mock.Definition{Request: req, Response: res, Callback: cb} varsProcessor := getProcessor() varsProcessor.Eval(&req, &mock) @@ -231,6 +238,18 @@ func TestReplaceTags(t *testing.T) { if mock.Response.Headers["header2"][1] != "valParam" { t.Error("Replaced tags in headers match", mock.Response.Headers["header2"][1]) } + + if mock.Callback.Cookies["cookie2"] != "AleixMG" { + t.Error("Replaced tags in Callback cookie match", mock.Callback.Cookies["cookie2"]) + } + + if mock.Callback.Headers["header2"][1] != "valParam" { + t.Error("Replaced tags in Callback headers match", mock.Callback.Headers["header2"][1]) + } + + if mock.Callback.Body != "Callback Body hi!. Query valParam. Cookie: valCookie. Random: AleixMG" { + t.Error("Replaced tags in body not match", cb.Body) + } } func TestReplaceUndefinedFakeTag(t *testing.T) { @@ -564,7 +583,7 @@ func TestReplaceXmlBodyEncodedTags(t *testing.T) { func TestReplaceTagsCallback(t *testing.T) { req := mock.Request{} - req.Body = "hi!" + req.Body = "hi Isona!" val := make(mock.Values) val["param1"] = []string{"valParam"} req.QueryStringParameters = val @@ -580,7 +599,7 @@ func TestReplaceTagsCallback(t *testing.T) { varsProcessor := getProcessor() varsProcessor.Eval(&req, &mock) - if mock.Callback.Body != "Request Body hi!. Query valParam. Cookie: valCookie. Random: AleixMG" { + if mock.Callback.Body != "Request Body hi Isona!. Query valParam. Cookie: valCookie. Random: AleixMG" { t.Error("Replaced tags in callback body not match", cb.Body) } }