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. If no port is passed, it will use the first exposed port in the image.
  • 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("/").WithStartupTimeout(10 * time.Second),
}

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

Match an HTTP method with Port

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

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

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("/").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).
        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"))),
}