Skip to content

HTTP(S) Wait strategy

The HTTP wait strategy will check the result of an HTTP(S) request against the container and allows to set the following conditions:

  • the port to be used.
  • the path to be used.
  • the HTTP method to be used.
  • the HTTP request body to be sent.
  • the HTTP status code matcher as a function.
  • the HTTP response matcher as a function.
  • the TLS config to be used for HTTPS.
  • the startup timeout to be used in seconds, default is 60 seconds.
  • the poll interval to be used in milliseconds, default is 100 milliseconds.
  • the basic auth credentials to be used.

Variations on the HTTP wait strategy are supported, including:

Match an HTTP method

    ctx := context.Background()
    req := testcontainers.ContainerRequest{
        Image:        "nginx:latest",
        ExposedPorts: []string{"80/tcp"},
        WaitingFor:   wait.ForHTTP("/").WithPort("80/tcp"),
    }

    gogs, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
        ContainerRequest: req,
        Started:          true,
    })
    if err != nil {
        panic(err)
    }

 

    tlsconfig := &tls.Config{RootCAs: certpool, ServerName: "testcontainer.go.test"}
    var i int
    dockerReq := testcontainers.ContainerRequest{
        FromDockerfile: testcontainers.FromDockerfile{
            Context: filepath.Join(workdir, "testdata"),
        },
        ExposedPorts: []string{"6443/tcp"},
        WaitingFor: wait.NewHTTPStrategy("/ping").WithTLS(true, tlsconfig).
            WithStartupTimeout(time.Second * 10).WithPort("6443/tcp").
            WithResponseMatcher(func(body io.Reader) bool {
                data, _ := io.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"))),
    }

Match an HTTP method with Basic Auth

ctx := context.Background()
req := testcontainers.ContainerRequest{
    Image:        "gogs/gogs:0.11.91",
    ExposedPorts: []string{"3000/tcp"},
    WaitingFor:   wait.ForHTTP("/").WithPort("3000/tcp").WithBasicAuth("username", "password"),
}

gogs, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
    ContainerRequest: req,
    Started:          true,
})
if err != nil {
    panic(err)
}

Match an HTTPS status code and a response matcher

tlsconfig := &tls.Config{RootCAs: certpool, ServerName: "testcontainer.go.test"}
var i int
dockerReq := testcontainers.ContainerRequest{
    FromDockerfile: testcontainers.FromDockerfile{
        Context: filepath.Join(workdir, "testdata"),
    },
    ExposedPorts: []string{"6443/tcp"},
    WaitingFor: wait.NewHTTPStrategy("/ping").WithTLS(true, tlsconfig).
        WithStartupTimeout(time.Second * 10).WithPort("6443/tcp").
        WithResponseMatcher(func(body io.Reader) bool {
            data, _ := io.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"))),
}