Bug: HttpWaitStrategy ignores response predicates for accepted HTTP error statuses

Ouverte Adaptée aux débutants
#1,143 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@PranavMulakala y travaille déjà.

Depuis le 23/10/2026.

  • #1144 par @PranavMulakala — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
85/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
python
Domaine
testing

Piste de recherche

Commence dans testcontainers/core/wait_strategies.py au niveau de HttpWaitStrategy._handle_http_error(), qui renvoie True sans vérifier _response_predicate. Lis _check_response() pour voir l'évaluation normale du prédicat. Exécute le script de reproduction de l'issue pour observer le ready erroné, puis modifie la branche d'erreur pour évaluer le prédicat. Terminé signifie que le script lève TimeoutError.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Describe the bug

HttpWaitStrategy.for_response_predicate() is not evaluated when an HTTP endpoint returns an explicitly accepted error status, such as 404 or 503. The strategy reports ready even when the response body does not match the predicate.

This affects both for_status_code(...) and for_status_code_matching(...). HTTP 200 responses do evaluate the body predicate. A caller that deliberately accepts an error status still needs the additional body condition to pass; accepting the status should not bypass that condition.

Found while testing HTTP readiness behavior on main at 6146aa1170f415fcc825ed9bddc4eacbeb937a12 (package version 4.15.0). I searched issues and PRs for HttpWaitStrategy, for_response_predicate, response predicate, and HTTPError and did not find this behavior reported. The HTTPError resource-cleanup work in #1115 is related code but a different problem.

Reproduction

This endpoint always returns 503 with body starting. The status is deliberately accepted, but the body predicate requires ready.

"""Reproduce a false-positive HTTP readiness result (requires Docker)."""
from testcontainers.core.container import DockerContainer
from testcontainers.core.wait_strategies import HttpWaitStrategy

server = """
from http.server import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(503)
        self.end_headers()
        self.wfile.write(b"starting")

HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()
"""
strategy = (
    HttpWaitStrategy(8080)
    .for_status_code(503)
    .for_response_predicate(lambda body: body == "ready")
    .with_startup_timeout(2)
    .with_poll_interval(0.1)
)
try:
    with (
        DockerContainer("python:3.12-alpine")
        .with_exposed_ports(8080)
        .with_command(["python", "-u", "-c", server])
        .waiting_for(strategy)
    ):
        raise AssertionError("BUG: marked ready even though the body is always 'starting'")
except TimeoutError:
    print("Correct: the body never matched, so readiness timed out.")

Expected behavior

The wait should continue polling and raise TimeoutError because the body never matches.

Actual behavior

The context manager enters immediately after the first HTTP response; the assertion fails. In a second reproduction, an endpoint returning starting on request 1 and ready on request 2 receives only one request before the wait finishes.

Root cause and proposed fix

urlopen() raises HTTPError for error statuses. _handle_http_error() accepts the status and returns True without evaluating _response_predicate. The normal response path in _check_response() does evaluate it.

Evaluate the body predicate for accepted HTTP errors while the existing with e: block still owns the response. This preserves status-only waits and response cleanup.

Runtime environment

  • macOS / Apple silicon; Colima Docker runtime
  • Regression tests reproduced on Python 3.12.14
  • Patch validated on Python 3.10.22, 3.12.14, 3.13.17, and 3.14.9
  • Real HTTP server in python:3.12-alpine
Langage dominant
Python
Étoiles
2.3k
Forks
395
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de testcontainers/testcontainers-python

Toutes les issues de testcontainers/testcontainers-python

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.