onsi / gomega

Ginkgo's Preferred Matcher Library
http://onsi.github.io/gomega/
MIT License
2.15k stars 282 forks source link

HaveHTTPBody Does Not Work with Eventually #682

Closed Kyle-Cooley closed 1 year ago

Kyle-Cooley commented 1 year ago

The following code does not work as expected when the first attempt does not succeed:

Eventually(func(ctx SpecContext) (*http.Response, error) {
  req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost:8080/test", strings.NewReader(query))
  req.Header.Add("Content-Type", "application/json")
  if err != nil {
    return nil, err
  }
  return http.DefaultClient.Do(req)
}).
  WithContext(ctx).
  WithPolling(5 * time.Second).
  WithTimeout(30 * time.Second).
  Should(HaveHTTPBody(MatchJSON(expectedResult)))

This appears to be caused by the use of a cached variable that is never updated after being set: https://github.com/onsi/gomega/blob/master/matchers/have_http_body_matcher.go#L76

Kyle-Cooley commented 1 year ago

Possible solution: Store the pointer for the response and use that to determine if the cache should be used or not.

onsi commented 1 year ago

hey @Kyle-Cooley - good catch; agreed on checking the pointer as you're proposing. Would you be up for submitting a PR?

Kyle-Cooley commented 1 year ago

Hello @onsi , yes, I will make a PR for it.