diff --git a/network_test.go b/network_test.go index ab136b86b0..d2aaae971b 100644 --- a/network_test.go +++ b/network_test.go @@ -84,7 +84,7 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) { hp := wait.ForListeningPort("5672/tcp") hp.WithStartupTimeout(3 * time.Minute) amqpRequest := ContainerRequest{ - Image: "rabbitmq:management-alpine", + Image: "rabbitmq:3.8.19-management-alpine", ExposedPorts: []string{"15672/tcp", "5672/tcp"}, Env: env, AutoRemove: true, diff --git a/wait/http.go b/wait/http.go index f882dca1a6..9315991c51 100644 --- a/wait/http.go +++ b/wait/http.go @@ -1,11 +1,13 @@ package wait import ( + "bytes" "context" "crypto/tls" "errors" "fmt" "io" + "io/ioutil" "net" "net/http" "strconv" @@ -184,12 +186,21 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge address := net.JoinHostPort(ipAddress, strconv.Itoa(port.Int())) endpoint := fmt.Sprintf("%s://%s%s", proto, address, ws.Path) + // cache the body into a byte-slice so that it can be iterated over multiple times + var body []byte + if ws.Body != nil { + body, err = ioutil.ReadAll(ws.Body) + if err != nil { + return + } + } + for { select { case <-ctx.Done(): return ctx.Err() case <-time.After(ws.PollInterval): - req, err := http.NewRequestWithContext(ctx, ws.Method, endpoint, ws.Body) + req, err := http.NewRequestWithContext(ctx, ws.Method, endpoint, bytes.NewReader(body)) if err != nil { return err } diff --git a/wait/http_test.go b/wait/http_test.go index a3a34431e1..9e0e3aa8b4 100644 --- a/wait/http_test.go +++ b/wait/http_test.go @@ -62,6 +62,7 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) { } tlsconfig := &tls.Config{RootCAs: certpool, ServerName: "testcontainer.go.test"} + var i int dockerReq := testcontainers.ContainerRequest{ FromDockerfile: testcontainers.FromDockerfile{ Context: workdir + "/testdata", @@ -73,6 +74,10 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) { data, _ := ioutil.ReadAll(body) return bytes.Equal(data, []byte("pong")) }). + WithStatusCodeMatcher(func(status int) bool { + i++ // always fail the first try in order to force the polling loop to be re-run + return i > 1 && status == 200 + }). WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), }