Skip to content

Commit

Permalink
Memoize response headers and support passing them in requests and ass…
Browse files Browse the repository at this point in the history
…erting in responses (#35)
  • Loading branch information
akajla09 authored Sep 18, 2023
1 parent 7c6e175 commit 61670f7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
with:
distribution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 4 additions & 1 deletion listresponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"id": 2,
"name": "name2"
}
]
],
"headers": {
"Warrant-Token": "asdf"
}
}
}
]
Expand Down
44 changes: 38 additions & 6 deletions suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ type TestSpec struct {

// Request information for a single test case
type Request struct {
Method string `json:"method"`
BaseUrl string `json:"baseUrl"`
Url string `json:"url"`
Body interface{} `json:"body"`
Method string `json:"method"`
BaseUrl string `json:"baseUrl"`
Url string `json:"url"`
Body interface{} `json:"body"`
Headers map[string]string `json:"headers"`
}

// Expected test case response
type ExpectedResponse struct {
StatusCode int `json:"statusCode"`
Body interface{} `json:"body"`
StatusCode int `json:"statusCode"`
Body interface{} `json:"body"`
Headers map[string]string `json:"headers"`
}

// Results for an executed TestSuite
Expand Down Expand Up @@ -264,6 +266,14 @@ func (suite TestSuite) executeTest(test TestSpec, extractedFields map[string]str
for k, v := range suite.config.CustomHeaders {
req.Header.Add(k, v)
}
for k, v := range test.Request.Headers {
headerVal, err := templateReplace(v, extractedFields)
if err != nil {
testErrors = append(testErrors, err.Error())
return Failed(test.Name, testErrors, time.Since(start))
}
req.Header.Add(k, headerVal)
}
resp, err := suite.config.HttpClient.Do(req)
if err != nil {
testErrors = append(testErrors, fmt.Sprintf("Error making request: %v", err))
Expand All @@ -276,6 +286,28 @@ func (suite TestSuite) executeTest(test TestSpec, extractedFields map[string]str
testErrors = append(testErrors, fmt.Sprintf("Expected http %d but got http %d", test.ExpectedResponse.StatusCode, statusCode))
}

// Memoize response headers
for headerName, headerValues := range resp.Header {
headerValConcat := strings.Join(headerValues, ",")
extractedFields[test.Name+".header."+headerName] = strings.TrimSpace(headerValConcat)
}
// Compare all expected response headers
for expHeaderName, expHeaderValTemplate := range test.ExpectedResponse.Headers {
if actualVals, ok := resp.Header[http.CanonicalHeaderKey(expHeaderName)]; ok {
expHeaderVal, err := templateReplace(expHeaderValTemplate, extractedFields)
if err != nil {
testErrors = append(testErrors, fmt.Sprintf("Invalid expected response header template %s", expHeaderValTemplate))
continue
}
actualVal := strings.Join(actualVals, ",")
if actualVal != expHeaderVal {
testErrors = append(testErrors, fmt.Sprintf("Expected response header '%s: %s' but got '%s: %s'", expHeaderName, expHeaderVal, expHeaderName, actualVal))
}
} else {
testErrors = append(testErrors, fmt.Sprintf("Expected response header '%s: %s' not present", expHeaderName, expHeaderValTemplate))
}
}

// Read response payload
body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
type MockHttpClient struct {
StatusCode int
Body string
Header map[string][]string
}

func (c *MockHttpClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: c.StatusCode,
Body: io.NopCloser(strings.NewReader(c.Body)),
Header: c.Header,
}, nil
}

Expand Down Expand Up @@ -58,6 +60,9 @@ func TestListResponse(t *testing.T) {
mockClient := MockHttpClient{}
mockClient.StatusCode = 200
mockClient.Body = "[{\"id\": 1, \"name\": \"name1\"},{\"id\": 2,\"name\": \"name2\"}]"
respHeaders := make(map[string][]string)
respHeaders["Warrant-Token"] = []string{"asdf"}
mockClient.Header = respHeaders
results, _ := ExecuteSuite(RunConfig{
BaseUrl: "",
CustomHeaders: nil,
Expand Down

0 comments on commit 61670f7

Please sign in to comment.