| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // TestTheTestTTLHeaderIsDevelopmentOnly pins the one thing that makes |
| 11 | // X-Calls-Test-TTL safe to have at all: outside development it is not |
| 12 | // read, so a request cannot shorten its own stream and no amount of |
| 13 | // header-setting turns a five-minute stream into a reconnect storm. |
| 14 | func TestTheTestTTLHeaderIsDevelopmentOnly(t *testing.T) { |
| 15 | req := func(v string) *http.Request { |
| 16 | r := httptest.NewRequest(http.MethodGet, "/m/abc/stream", nil) |
| 17 | if v != "" { |
| 18 | r.Header.Set(testTTLHeader, v) |
| 19 | } |
| 20 | return r |
| 21 | } |
| 22 | newApp := func(env string) *app { |
| 23 | a := &app{cfg: resolved{Config: Config{Env: env}}} |
| 24 | a.ttl.Store(int64(5 * time.Minute)) |
| 25 | return a |
| 26 | } |
| 27 | |
| 28 | dev := newApp(EnvDevelopment) |
| 29 | if got := dev.streamLifeFor(req("2s")); got != 2*time.Second { |
| 30 | t.Fatalf("development: stream life %v, want 2s", got) |
| 31 | } |
| 32 | if got := dev.streamLifeFor(req("")); got != 5*time.Minute { |
| 33 | t.Fatalf("development, no header: stream life %v, want the configured 5m", got) |
| 34 | } |
| 35 | if got := dev.streamLifeFor(req("not a duration")); got != 5*time.Minute { |
| 36 | t.Fatalf("development, unparseable header: stream life %v, want the configured 5m", got) |
| 37 | } |
| 38 | // The floor is the same one every other timing goes through: a |
| 39 | // header cannot hand time.NewTimer a zero or a negative. |
| 40 | if got := dev.streamLifeFor(req("0s")); got != minStreamTiming { |
| 41 | t.Fatalf("development, zero header: stream life %v, want the %v floor", got, minStreamTiming) |
| 42 | } |
| 43 | |
| 44 | prod := newApp("production") |
| 45 | if got := prod.streamLifeFor(req("2s")); got != 5*time.Minute { |
| 46 | t.Fatalf("production: stream life %v, want the configured 5m — the header must not be read", got) |
| 47 | } |
| 48 | } |
| 49 | |