CodeSampleX

Exemplo

net/http go1.26.5: Show that `http.Client.Timeout` is enforced while reading a slow response body.

Amostra verificada para golang net/http go1.26.5: Show that `http.Client.Timeout` is enforced while reading a slow response body. O contrato rodou em go 1.26…

sha256:a5d71aedbea662543b6d6b4b60bce0ccd1b8876c606797a1bbab8655f25ddf1f

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
2
Ambiente declarado go linux x64 go go go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-16
go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-18

Caso

HOW
Objetivo
Show that `http.Client.Timeout` is enforced while reading a slow response body.
Pacotes
Símbolos
  • Client.Timeout
Ambiente
go
Criado
2026-08-16T12:04:37Z

Contrato

  1. A request with a short `Client.Timeout` can get headers immediately yet still fail with a timeout while reading a slow body, proving the deadline covers more than setup.

Arquivos

  • NOTES.md
  • client_timeout_test.go
  • csx.json
  • go.mod

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

NOTES.md
search_known_solution for `pkg:golang/net/http@go1.26.5` + `Client.Timeout` returned a miss, so this uses a fresh proof.
The wrong expectation fails because the timeout still aborts mid-body reads (as `io.ReadAll` returns a deadline error) even though headers can arrive within the timeout.
client_timeout_test.go
package codesamplex

import (
	"context"
	"errors"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func TestClientTimeoutLimitsResponseBodyRead(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		if _, err := w.Write([]byte("ok")); err != nil {
			t.Error(err)
			return
		}
		if f, ok := w.(http.Flusher); ok {
			f.Flush()
		}

		time.Sleep(100 * time.Millisecond)
		_, err := w.Write([]byte("too late"))
		if err != nil {
			t.Error(err)
			return
		}
	}))
	defer srv.Close()

	client := &http.Client{
		Timeout: 50 * time.Millisecond,
	}

	resp, err := client.Get(srv.URL)
	if err != nil {
		t.Fatalf("unexpected error before reading body: %v", err)
	}
	defer resp.Body.Close()

	_, err = io.ReadAll(resp.Body)
	if err == nil {
		t.Fatal("expected timeout while reading body, got nil")
	}
	if !errors.Is(err, context.DeadlineExceeded) {
		t.Fatalf("expected context deadline exceeded, got %v", err)
	}
}
csx.json
{"case":{"believed":"A common model expectation is that once headers arrive, a `http.Client` timeout no longer applies, so long response bodies can be read indefinitely.","caseId":"case:sha256:8a3b9795afd5fed47a451d4a013163fc7764b661cf0e78c579edb91221b7c212","contract":["A request with a short `Client.Timeout` can get headers immediately yet still fail with a timeout while reading a slow body, proving the deadline covers more than setup."],"goal":"Show that `http.Client.Timeout` is enforced while reading a slow response body.","kind":"HOW","packages":["pkg:golang/net/http@go1.26.5"],"schemaVersion":1,"symbols":["Client.Timeout"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","ecosystem":"golang","executionContext":"go","language":"go","os":"linux","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/net/http@go1.26.5"],"schemaVersion":1,"symbols":["Client.Timeout"],"verifierAdapter":"golang@1"}
go.mod
module codesamplex

go 1.26

Seeder de origem

csx-seed