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
Prev Previous commit
Next Next commit
response filler tests
  • Loading branch information
jcdietrich committed Nov 6, 2024
commit b3439a596b4c3e9ae2053cb79ec4c198c8f9380d
76 changes: 76 additions & 0 deletions pkg/vars/response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package vars

import (
"strings"
"testing"

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

var testVars = []struct {
key string
value string
expectToFind bool
}{
{"response.body.test1", "one", true},
{"response.body.test2", "two", true},
{"response.body.not_found", "nothing", false},
{"response.header.Content-Type", "application/json", true},
{"response.header.not_found", "nothing", false},
{"response.cookie.test_cookie", "test_cookie_value", true},
{"response.cookie.not_found", "nothing", false},
}

func getTestResponse() *mock.Response {
var status = 200

var headers = make(mock.Values)
headers["Content-Type"] = []string{"application/json"}

var cookies = make(mock.Cookies)
cookies["test_cookie"] = "test_cookie_value"

var httpHeaders = mock.HttpHeaders{Headers: headers, Cookies: cookies}

var body = "{\"test1\":\"one\",\"test2\":\"two\"}"
var httpEntity = mock.HTTPEntity{HttpHeaders: httpHeaders, Body: body}

var response = mock.Response{StatusCode: status, HTTPEntity: httpEntity}
return &response
}

func getLoadedResponseFiller() ResponseFiller {
return ResponseFiller{Response: getTestResponse()}
}

func TestResponseFiller(t *testing.T) {
var filler = getLoadedResponseFiller()

for _, tt := range testVars {
log.Errorf("testing: %v", tt)
holders := []string{
tt.key,
}

vars := filler.Fill(holders)
log.Errorf("vars: %v", vars)

if len(vars) == 0 {
if tt.expectToFind {
t.Errorf("Unable to retrieve vars")
}
continue
}

v, ok := vars[tt.key]

if !ok {
t.Errorf("Unable to retrieve value inside vars")
continue
}

if strings.EqualFold(v[0], tt.value) != tt.expectToFind {
t.Errorf("Couldn't get the expected value. Expected: %s, Value found: %s", tt.value, v[0])
}
}
}