Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: HTTP wait strategy does not take query params into account #2466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions wait/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,12 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge
client := http.Client{Transport: tripper, Timeout: time.Second}
address := net.JoinHostPort(ipAddress, strconv.Itoa(mappedPort.Int()))

endpoint := url.URL{
Scheme: proto,
Host: address,
Path: ws.Path,
endpoint, err := url.Parse(ws.Path)
if err != nil {
return err
}
endpoint.Scheme = proto
endpoint.Host = address

if ws.UserInfo != nil {
endpoint.User = ws.UserInfo
Expand Down
81 changes: 81 additions & 0 deletions wait/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,87 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) {
}
}

func TestHTTPStrategyWaitUntilReadyWithQueryString(t *testing.T) {
workdir, err := os.Getwd()
if err != nil {
t.Error(err)
return
}

capath := filepath.Join(workdir, "testdata", "root.pem")
cafile, err := os.ReadFile(capath)
if err != nil {
t.Errorf("can't load ca file: %v", err)
return
}

certpool := x509.NewCertPool()
if !certpool.AppendCertsFromPEM(cafile) {
t.Errorf("the ca file isn't valid")
return
}

tlsconfig := &tls.Config{RootCAs: certpool, ServerName: "testcontainer.go.test"}
dockerReq := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: filepath.Join(workdir, "testdata"),
},

ExposedPorts: []string{"6443/tcp"},
WaitingFor: wait.NewHTTPStrategy("/query-params-ping?v=pong").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"))
}),
}

container, err := testcontainers.GenericContainer(context.Background(),
testcontainers.GenericContainerRequest{ContainerRequest: dockerReq, Started: true})
if err != nil {
t.Error(err)
return
}
defer container.Terminate(context.Background()) // nolint: errcheck

host, err := container.Host(context.Background())
if err != nil {
t.Error(err)
return
}
port, err := container.MappedPort(context.Background(), "6443/tcp")
if err != nil {
t.Error(err)
return
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsconfig,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
resp, err := client.Get(fmt.Sprintf("https://%s:%s", host, port.Port()))
if err != nil {
t.Error(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("status code isn't ok: %s", resp.Status)
return
}
}

func TestHTTPStrategyWaitUntilReadyNoBasicAuth(t *testing.T) {
workdir, err := os.Getwd()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions wait/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func main() {
w.WriteHeader(http.StatusUnauthorized)
})

mux.HandleFunc("/query-params-ping", func(w http.ResponseWriter, req *http.Request) {
v := req.URL.Query().Get("v")
if v == "" {
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("pong"))
})

mux.HandleFunc("/headers", func(w http.ResponseWriter, req *http.Request) {
h := req.Header.Get("X-request-header")
if h != "" {
Expand Down
Loading