package calls import ( "net/http" "net/http/httptest" "testing" "time" ) // TestTheTestTTLHeaderIsDevelopmentOnly pins the one thing that makes // X-Calls-Test-TTL safe to have at all: outside development it is not // read, so a request cannot shorten its own stream and no amount of // header-setting turns a five-minute stream into a reconnect storm. func TestTheTestTTLHeaderIsDevelopmentOnly(t *testing.T) { req := func(v string) *http.Request { r := httptest.NewRequest(http.MethodGet, "/m/abc/stream", nil) if v != "" { r.Header.Set(testTTLHeader, v) } return r } newApp := func(env string) *app { a := &app{cfg: resolved{Config: Config{Env: env}}} a.ttl.Store(int64(5 * time.Minute)) return a } dev := newApp(EnvDevelopment) if got := dev.streamLifeFor(req("2s")); got != 2*time.Second { t.Fatalf("development: stream life %v, want 2s", got) } if got := dev.streamLifeFor(req("")); got != 5*time.Minute { t.Fatalf("development, no header: stream life %v, want the configured 5m", got) } if got := dev.streamLifeFor(req("not a duration")); got != 5*time.Minute { t.Fatalf("development, unparseable header: stream life %v, want the configured 5m", got) } // The floor is the same one every other timing goes through: a // header cannot hand time.NewTimer a zero or a negative. if got := dev.streamLifeFor(req("0s")); got != minStreamTiming { t.Fatalf("development, zero header: stream life %v, want the %v floor", got, minStreamTiming) } prod := newApp("production") if got := prod.streamLifeFor(req("2s")); got != 5*time.Minute { t.Fatalf("production: stream life %v, want the configured 5m — the header must not be read", got) } }