Skip to content

Commit

Permalink
Treat callback like response (#177)
Browse files Browse the repository at this point in the history
* Treat callback like response

* Fixed codecov
  • Loading branch information
jmartin82 authored Apr 30, 2024
1 parent bbff6d5 commit a84db26
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
6 changes: 3 additions & 3 deletions internal/server/handle_callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions pkg/mock/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
29 changes: 17 additions & 12 deletions pkg/vars/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
25 changes: 22 additions & 3 deletions pkg/vars/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,36 @@ 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)
cookie["cookie1"] = "valCookie"
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)

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
Expand Down

0 comments on commit a84db26

Please sign in to comment.