Skip to content

Commit

Permalink
topdown: http.send cache test use new test server
Browse files Browse the repository at this point in the history
The older version of the cache testing for http.send didn't use
separate t.Run()'s for each case so it made it hard to see which case
was broken if the number of requests didn't match up with expected.
It also re-used the same test server which seemed to cause some
timing issues.

Signed-off-by: Patrick East <east.patrick@gmail.com>
  • Loading branch information
patrick-east authored and tsandall committed Apr 24, 2020
1 parent 1365a38 commit c79dc60
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions topdown/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,18 +660,7 @@ func TestHTTPRedirectEnable(t *testing.T) {
}

func TestHTTPSendCaching(t *testing.T) {
// test server
nextResponse := "{}"
var requests []*http.Request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests = append(requests, r)
w.WriteHeader(http.StatusOK)
w.Write([]byte(nextResponse))
}))
defer ts.Close()

// expected result

var body []interface{}
bodyMap := map[string]string{"id": "1", "firstname": "John"}
body = append(body, bodyMap)
Expand Down Expand Up @@ -770,16 +759,25 @@ func TestHTTPSendCaching(t *testing.T) {
data := loadSmallTestData()

for _, tc := range tests {
nextResponse = tc.response
requests = nil
runTopDownTestCase(t, data, tc.note, []string{strings.ReplaceAll(tc.ruleTemplate, "%URL%", ts.URL)}, tc.response)

// Note: The runTopDownTestCase ends up evaluating twice (once with and once without partial
// eval first), so expect 2x the total request count the test case specified.
actualCount := len(requests) / 2
if actualCount != tc.expectedReqCount {
t.Fatalf("Expected to only get %d requests, got %d", tc.expectedReqCount, actualCount)
}
t.Run(tc.note, func(t *testing.T) {

var requests []*http.Request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests = append(requests, r)
w.WriteHeader(http.StatusOK)
w.Write([]byte(tc.response))
}))
defer ts.Close()

runTopDownTestCase(t, data, tc.note, []string{strings.ReplaceAll(tc.ruleTemplate, "%URL%", ts.URL)}, tc.response)

// Note: The runTopDownTestCase ends up evaluating twice (once with and once without partial
// eval first), so expect 2x the total request count the test case specified.
actualCount := len(requests) / 2
if actualCount != tc.expectedReqCount {
t.Fatalf("Expected to get %d requests, got %d", tc.expectedReqCount, actualCount)
}
})
}
}

Expand Down

0 comments on commit c79dc60

Please sign in to comment.