package server_test import ( "bytes" "context" "errors" "fmt" "log/slog" "net/http" "net/http/httptest" "sort" "strconv" "strings" "testing" "time" "amadan.net/oficina/suite" "amadan.net/oficina/suite/conformance" "amadan.net/oficina/suite/server" ) // fixtureProducer is a correct Producer built from the custody fixture, the // same world the conformance harness checks. type fixtureProducer struct { f suite.CustodyFixture extraRows int // Feed returns this many more rows than the cases contentErr error feedErr error ruleErr error short bool // Rule answers one fewer than asked cursor string // overrides the feed's cursor when set content []byte // overrides S3 bytes when set lastAsker suite.Subject zeroTime bool // the first row carries no touched_at zeroRev bool // the first row carries revision 0 resetStart bool // Feed resets even the beginning } func (p *fixtureProducer) find(id string) (suite.CustodyCase, bool) { for _, c := range p.f.Cases { if c.ItemID == id { return c, true } } return suite.CustodyCase{}, false } func (p *fixtureProducer) Feed(_ context.Context, cursor string, limit int) (suite.Feed, error) { if p.feedErr != nil { return suite.Feed{}, p.feedErr } if cursor == "end" { return suite.Feed{Cursor: "end"}, nil } if cursor != "" || p.resetStart { // The only cursors this log issues are "end" and the override, and // a test's own; anything else names no position in it (§6.6). if p.cursor == "" || cursor != p.cursor { return suite.Feed{}, fmt.Errorf("fichas-like: cursor %q: %w", cursor, server.ErrCursorReset) } } var rows []suite.FeedRow for _, c := range p.f.Cases { rows = append(rows, suite.FeedRow{ItemID: c.ItemID, Revision: c.Revision, Gone: c.Rung == suite.RungDeleted, TouchedAt: time.Date(2026, 11, 1, 9, 0, 0, 0, time.UTC)}) } if p.zeroTime { rows[0].TouchedAt = time.Time{} } if p.zeroRev { rows[0].Revision = 0 } for i := 0; i < p.extraRows; i++ { rows = append(rows, suite.FeedRow{ItemID: "extra-" + strconv.Itoa(i), Revision: 1, TouchedAt: time.Date(2026, 11, 1, 9, 0, 0, 0, time.UTC)}) } next := "end" if p.cursor != "" { next = p.cursor } if p.cursor == "-" { next = "" } return suite.Feed{Cursor: next, Rows: rows}, nil } func (p *fixtureProducer) Rule(_ context.Context, asker suite.Subject, pairs []suite.ItemRevision) ([]suite.Authorization, error) { p.lastAsker = asker if p.ruleErr != nil { return nil, p.ruleErr } out := make([]suite.Authorization, 0, len(pairs)) if asker.Kind != p.f.Asker.Kind { for range pairs { out = append(out, suite.UNKNOWN) } return out, nil } for _, pr := range pairs { c, ok := p.find(pr.ItemID) may := ok && asker == p.f.Asker && c.Rung != suite.RungDeleted && (c.Rung == suite.RungTeam || c.Owner == "asker" || c.AskerGrant) // STALE from the log, never by comparing numbers (§6.6): this log // holds the current revision and the one before it. switch { case !may: out = append(out, suite.NOT_VISIBLE) case pr.Revision == c.Revision: out = append(out, suite.VISIBLE) case pr.Revision == c.PriorRevision: out = append(out, suite.STALE) default: out = append(out, suite.UNKNOWN) } } if p.short { out = out[:len(out)-1] } return out, nil } func (p *fixtureProducer) Content(_ context.Context, id string, rev int64) ([]byte, error) { if p.contentErr != nil { return nil, p.contentErr } c, ok := p.find(id) if !ok || c.Rung == suite.RungDeleted || rev != c.Revision { return nil, suite.ErrNotAvailable } if p.content != nil { return p.content, nil } return []byte(c.S3Body), nil } func producer(t *testing.T) *fixtureProducer { t.Helper() f, err := suite.LoadCustodyFixture() if err != nil { t.Fatal(err) } return &fixtureProducer{f: f} } const token = "a-test-token-for-memoria" func consumers(t *testing.T, tokens ...string) []server.Consumer { t.Helper() var ts []server.Token for i, tok := range tokens { ts = append(ts, server.Token{Epoch: i + 1, SHA256: server.TokenHash(tok)}) } return []server.Consumer{{ID: "memoria", Tokens: ts}} } func do(h http.Handler, method, url, tok string, body []byte) *httptest.ResponseRecorder { req := httptest.NewRequest(method, url, bytes.NewReader(body)) if tok != "" { req.Header.Set("Authorization", "Bearer "+tok) } rec := httptest.NewRecorder() h.ServeHTTP(rec, req) return rec } // The routes this package serves pass the doctrine's own harness, driven // through a producer that only knows the fixture, mounted as an app mounts // it: Handler, behind a token (conformance.RunProducer, revision 11). The // tests below cover what the harness does not send. // // MUTATION-TESTED 2026-11-01, reverted: CheckProducer driving the deprecated // Routes in place of Handler → "a request with no token: got 404". // // MUTATION-TESTED 2026-11-02 (Fable's review of 0a59e7d, L6), reverted: // Authenticate's no-token 401 stripped of Cache-Control and Vary → "GET // /suite/s3 401: Cache-Control "" and Vary """ here; with CheckProducer's // anonymous probe sent around the header audit instead of through it (as // before), the same mutant passed this test. func TestRoutesPassConformance(t *testing.T) { conformance.RunProducer(t, producer(t), suite.SubjectPersonRef) } // The deprecated Routes still serves what it served, for the apps whose // tests call it (the module is additive). func TestDeprecatedRoutesStillConform(t *testing.T) { conformance.Run(t, server.Routes(producer(t), nil)) } // No consumers, no routes (doctrine §3). func TestNoConsumersServesNothing(t *testing.T) { if h := server.Handler(producer(t), nil, nil); h != nil { t.Fatal("Handler with no consumers returned routes") } } // Every failure to authenticate is the same empty 401, and both tokens of a // rotation are accepted. // // MUTATION-TESTED 2026-11-01, each reverted: the hash comparison skipped // (any bearer admitted) → "an unknown token"; only the first token of a // consumer compared → "the second token"; a reason written in the 401 body // → "alike". Revision 11: the scheme check removed (any seven bytes taken // as "Bearer ") → "not a bearer", which "Basic " never caught. func TestAuthentication(t *testing.T) { h := server.Handler(producer(t), consumers(t, token, "the-next-token"), nil) ok := do(h, "GET", suite.RouteS1, token, nil) if ok.Code != 200 { t.Fatalf("the configured token: %d", ok.Code) } if rec := do(h, "GET", suite.RouteS1, "the-next-token", nil); rec.Code != 200 { t.Errorf("the second token of a rotation: %d", rec.Code) } var first string for name, req := range map[string]func() *http.Request{ "no header": func() *http.Request { return httptest.NewRequest("GET", suite.RouteS1, nil) }, "an unknown token": func() *http.Request { r := httptest.NewRequest("GET", suite.RouteS1, nil) r.Header.Set("Authorization", "Bearer not-the-token") return r }, // Seven bytes, as "Bearer " is: with "Basic " (six) the token was // cut one byte short and refused whether or not the scheme was // checked (Fable, review of c38e3ad, L5). "not a bearer": func() *http.Request { r := httptest.NewRequest("GET", suite.RouteS1, nil) r.Header.Set("Authorization", "Digest "+token) return r }, "an empty bearer": func() *http.Request { r := httptest.NewRequest("GET", suite.RouteS1, nil) r.Header.Set("Authorization", "Bearer ") return r }, } { rec := httptest.NewRecorder() h.ServeHTTP(rec, req()) if rec.Code != 401 { t.Errorf("%s: %d, want 401", name, rec.Code) } got := signature(rec) if first == "" { first = got } else if got != first { t.Errorf("%s: refusals not alike: %q vs %q", name, got, first) } } } // MUTATION-TESTED 2026-11-01, reverted: the SHA-256 length check reduced to // "not empty" → "31 bytes of hex" and "33 bytes of hex" (Fable, review of // c38e3ad, L5: nothing tested it). func TestParseConsumers(t *testing.T) { good := `[{"consumer_id":"memoria","tokens":[{"epoch":1,"sha256":"` + server.TokenHash("x") + `"}]}]` if cs, err := server.ParseConsumers(good); err != nil || len(cs) != 1 || cs[0].ID != "memoria" { t.Fatalf("good: %+v, %v", cs, err) } if cs, err := server.ParseConsumers(" "); err != nil || cs != nil { t.Errorf("empty is no consumers: %+v, %v", cs, err) } h := server.TokenHash("x") for name, bad := range map[string]string{ "an unknown field": `[{"consumer_id":"m","origin":"x","tokens":[{"epoch":1,"sha256":"` + h + `"}]}]`, "three tokens": `[{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"` + h + `"},{"epoch":2,"sha256":"` + h + `"},{"epoch":3,"sha256":"` + h + `"}]}]`, "no tokens": `[{"consumer_id":"m","tokens":[]}]`, "not hex": `[{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"plaintext-token"}]}]`, // Hex, and a byte short of a SHA-256: no token can ever match it, so // it is a typo that would serve nobody (Fable, review of c38e3ad, L5). "31 bytes of hex": `[{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"` + h[:62] + `"}]}]`, "33 bytes of hex": `[{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"` + h + `00"}]}]`, "a repeated id": `[{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"` + h + `"}]},{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"` + h + `"}]}]`, "trailing content": good + `[]`, "not a list": `{"consumer_id":"m"}`, } { if _, err := server.ParseConsumers(bad); err == nil { t.Errorf("%s: accepted", name) } } if cs, err := server.ConsumersFromEnv(func(k string) string { if k == "DOCS_SUITE_CONSUMERS" { return good } return "" }, "docs"); err != nil || len(cs) != 1 { t.Errorf("ConsumersFromEnv: %+v, %v", cs, err) } } // S1 holds the producer to what the consumer's decoder demands. func TestS1(t *testing.T) { p := producer(t) h := server.Routes(p, nil) rec := do(h, "GET", suite.RouteS1+"?cursor=", "", nil) feed, err := suite.DecodeS1(rec.Code, rec.Body.Bytes()) if err != nil || len(feed.Rows) != len(p.f.Cases) || feed.Cursor != "end" { t.Fatalf("first page: %+v, %v", feed, err) } // At the tip: rows [] (never null) and the same cursor back. tip := do(h, "GET", suite.RouteS1+"?cursor=end", "", nil) if !strings.Contains(tip.Body.String(), `"rows":[]`) { t.Errorf("rows: the tip's rows are not an empty list: %s", tip.Body.String()) } if feed, err := suite.DecodeS1(tip.Code, tip.Body.Bytes()); err != nil || feed.Cursor != "end" || len(feed.Rows) != 0 { t.Errorf("tip: %+v, %v", feed, err) } p.cursor = "-" if rec := do(h, "GET", suite.RouteS1, "", nil); rec.Code != 500 { t.Errorf("an empty cursor from the producer: %d, want 500", rec.Code) } p.cursor, p.feedErr = "", errors.New("db down") if rec := do(h, "GET", suite.RouteS1, "", nil); rec.Code != 503 { t.Errorf("a failed feed: %d, want 503 (never an empty page)", rec.Code) } } // S2 decodes strictly, caps the batch, echoes every pair, and fails loudly. // // MUTATION-TESTED 2026-11-01, each reverted: DisallowUnknownFields removed → // "an unknown field"; the cap check removed → "257 pairs"; the answer-count // check removed → "a short answer"; a producer error answered 200 with no // results → "a failed rule". func TestS2(t *testing.T) { p := producer(t) h := server.Routes(p, nil) ask := func(body string) *httptest.ResponseRecorder { return do(h, "POST", suite.RouteS2, "", []byte(body)) } asker := `{"kind":"person_ref","value":"pers_ana"}` ok := ask(`{"asker":` + asker + `,"pairs":[{"item_id":"doc-team","revision":7},{"item_id":"doc-link","revision":5}]}`) got := suite.DecodeS2([]suite.ItemRevision{{ItemID: "doc-team", Revision: 7}, {ItemID: "doc-link", Revision: 5}}, ok.Code, ok.Body.Bytes()) if len(got) != 2 || got[0] != suite.VISIBLE || got[1] != suite.NOT_VISIBLE { t.Fatalf("answers = %v (%d %s)", got, ok.Code, ok.Body.String()) } if p.lastAsker != (suite.Subject{Kind: suite.SubjectPersonRef, Value: "pers_ana"}) { t.Errorf("the asker reached the producer as %+v", p.lastAsker) } var many []string for i := 0; i < 257; i++ { many = append(many, `{"item_id":"doc-team","revision":7}`) } for name, body := range map[string]string{ "an unknown field": `{"asker":` + asker + `,"pairs":[],"extra":1}`, "257 pairs": `{"asker":` + asker + `,"pairs":[` + strings.Join(many, ",") + `]}`, "a bad kind": `{"asker":{"kind":"email","value":"x"},"pairs":[]}`, "an empty asker": `{"asker":{"kind":"address","value":""},"pairs":[]}`, "no pairs": `{"asker":` + asker + `}`, "an empty item id": `{"asker":` + asker + `,"pairs":[{"item_id":"","revision":1}]}`, "not JSON": `nope`, "two documents": `{"asker":` + asker + `,"pairs":[]}{}`, } { if rec := ask(body); rec.Code != 400 { t.Errorf("%s: %d, want 400", name, rec.Code) } } p.short = true if rec := ask(`{"asker":` + asker + `,"pairs":[{"item_id":"doc-team","revision":7}]}`); rec.Code != 500 { t.Errorf("a short answer: %d, want 500", rec.Code) } p.short, p.ruleErr = false, errors.New("roster unreachable") if rec := ask(`{"asker":` + asker + `,"pairs":[{"item_id":"doc-team","revision":7}]}`); rec.Code != 503 { t.Errorf("a failed rule: %d, want 503", rec.Code) } } // Every S3 refusal is the same response: a deleted item, an id that never // existed, a superseded, missing or malformed revision, and an item too // large to serve. // // MUTATION-TESTED 2026-11-01, each reverted: a malformed revision answered // 400 → "malformed"; the size check removed → "too large"; ErrNotAvailable // answered 503 → "deleted". func TestS3RefusesAlike(t *testing.T) { p := producer(t) h := server.Routes(p, nil) base := do(h, "GET", suite.RouteS3+"/"+suite.NonexistentItemID+"?revision=1", "", nil) if base.Code != 404 { t.Fatalf("baseline: %d", base.Code) } sig := signature for name, url := range map[string]string{ "deleted": suite.RouteS3 + "/doc-deleted?revision=6", "superseded": suite.RouteS3 + "/doc-team?revision=6", "missing": suite.RouteS3 + "/doc-team", "malformed": suite.RouteS3 + "/doc-team?revision=seven", "zero": suite.RouteS3 + "/doc-team?revision=0", } { if rec := do(h, "GET", url, "", nil); sig(rec) != sig(base) { t.Errorf("%s: %q, want the baseline %q", name, sig(rec), sig(base)) } } p.content = bytes.Repeat([]byte("x"), server.MaxContentBytes+1) if rec := do(h, "GET", suite.RouteS3+"/doc-team?revision=7", "", nil); sig(rec) != sig(base) { t.Errorf("too large: %d, want the baseline", rec.Code) } p.content = bytes.Repeat([]byte("x"), server.MaxContentBytes) if rec := do(h, "GET", suite.RouteS3+"/doc-team?revision=7", "", nil); rec.Code != 200 || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/plain") || !bytes.Equal(rec.Body.Bytes(), p.content) { t.Errorf("at the limit: %d %s, %d bytes", rec.Code, rec.Header().Get("Content-Type"), rec.Body.Len()) } // A producer that fails is not an item that is unavailable: 503, which // the consumer retries, never the 404 it would count as gone. p.content, p.contentErr = nil, errors.New("replay failed") if rec := do(h, "GET", suite.RouteS3+"/doc-team?revision=7", "", nil); rec.Code != 503 { t.Errorf("a failed producer: %d, want 503", rec.Code) } } // Astra's survivors (2026-11-01). // // MUTATION-TESTED 2026-11-01, each reverted: only the first consumer // compared → "a second consumer"; the consumer left out of the context → // "context"; the S2 body cap removed → "an oversized body"; the page limit // check removed → "a page over the limit"; a producer error logged with its // text → "the log"; Decoder.More() back as the end check → "trailing". func TestTheSurvivors(t *testing.T) { two := append(consumers(t, token), server.Consumer{ID: "another", Tokens: []server.Token{{Epoch: 1, SHA256: server.TokenHash("another-token")}}}) h := server.Handler(producer(t), two, nil) if rec := do(h, "GET", suite.RouteS1, "another-token", nil); rec.Code != 200 { t.Errorf("a second consumer: %d", rec.Code) } who := server.Authenticate(two, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c, _ := server.ConsumerFrom(r.Context()) w.Write([]byte(c.ID)) })) if rec := do(who, "GET", "/", "another-token", nil); rec.Body.String() != "another" { t.Errorf("context: the consumer is %q", rec.Body.String()) } p := producer(t) var logs bytes.Buffer routes := server.Routes(p, slog.New(slog.NewTextHandler(&logs, nil))) big := `{"asker":{"kind":"person_ref","value":"pers_ana"},"pairs":[{"item_id":"` + strings.Repeat("a", 300<<10) + `","revision":1}]}` if rec := do(routes, "POST", suite.RouteS2, "", []byte(big)); rec.Code != 400 { t.Errorf("an oversized body: %d, want 400", rec.Code) } p.extraRows = server.FeedPageLimit if rec := do(routes, "GET", suite.RouteS1, "", nil); rec.Code != 500 { t.Errorf("a page over the limit: %d, want 500", rec.Code) } p.extraRows = 0 const secret = "Q3 board pack: acquisition of Acme" p.feedErr, p.ruleErr, p.contentErr = errors.New(secret), errors.New(secret), errors.New(secret) do(routes, "GET", suite.RouteS1, "", nil) do(routes, "POST", suite.RouteS2, "", []byte(`{"asker":{"kind":"person_ref","value":"pers_ana"},"pairs":[{"item_id":"doc-team","revision":7}]}`)) do(routes, "GET", suite.RouteS3+"/doc-team?revision=7", "", nil) if strings.Contains(logs.String(), "acquisition") { t.Errorf("the log: a producer's error text was logged: %s", logs.String()) } if !strings.Contains(logs.String(), "S1") || !strings.Contains(logs.String(), "S2") || !strings.Contains(logs.String(), "S3") { t.Errorf("the log: failures were not logged at all: %s", logs.String()) } p.feedErr, p.ruleErr, p.contentErr = nil, nil, nil for _, junk := range []string{"]", "}", "[]", `{"a":1}`} { body := `{"asker":{"kind":"person_ref","value":"pers_ana"},"pairs":[{"item_id":"doc-team","revision":7}]}` + junk if rec := do(routes, "POST", suite.RouteS2, "", []byte(body)); rec.Code != 400 { t.Errorf("trailing %q after an S2 request: %d, want 400", junk, rec.Code) } } good := `[{"consumer_id":"m","tokens":[{"epoch":1,"sha256":"` + server.TokenHash("x") + `"}]}]` for _, bad := range []string{good + "]", good + "}", "null", "[]", `{"consumer_id":"m"}`} { if _, err := server.ParseConsumers(bad); err == nil { t.Errorf("trailing or not a list: %q accepted", bad) } } } // S1's guards at their edges (Astra, 2026-11-01). // // MUTATION-TESTED 2026-11-01, each reverted: the page limit off by one // (>= for >) → "exactly the limit"; the empty item id check removed → "an // empty item id". func TestS1Edges(t *testing.T) { p := producer(t) h := server.Routes(p, nil) p.extraRows = server.FeedPageLimit - len(p.f.Cases) if rec := do(h, "GET", suite.RouteS1, "", nil); rec.Code != 200 { t.Errorf("exactly the limit: %d, want 200", rec.Code) } p.extraRows = 0 bad := &emptyIDProducer{fixtureProducer: p} if rec := do(server.Routes(bad, nil), "GET", suite.RouteS1, "", nil); rec.Code != 500 { t.Errorf("an empty item id: %d, want 500", rec.Code) } } type emptyIDProducer struct{ *fixtureProducer } func (p *emptyIDProducer) Feed(ctx context.Context, cursor string, limit int) (suite.Feed, error) { f, err := p.fixtureProducer.Feed(ctx, cursor, limit) if len(f.Rows) > 0 { f.Rows[0].ItemID = "" } return f, err } // signature is a response as a client could compare it: status, every // header, body. func signature(r *httptest.ResponseRecorder) string { var hs []string for k, v := range r.Header() { hs = append(hs, k+"="+strings.Join(v, ",")) } sort.Strings(hs) return strconv.Itoa(r.Code) + "|" + strings.Join(hs, "|") + "|" + r.Body.String() } // A cursor the producer's log does not recognise is S1's one fixed 409, and // the consumer's decoder reads it as a reset (doctrine §6.6, revision 11). // The beginning is never reset: a producer that resets "" would send a // consumer back to "" for ever, so that is a 500. // // MUTATION-TESTED 2026-11-01, each reverted: the ErrCursorReset branch // removed (a reset answered as a failed feed) → "a foreign cursor"; the "" // guard removed → "the beginning reset"; the body written through // json.Encoder as an object with a second field → "a foreign cursor". func TestS1CursorReset(t *testing.T) { p := producer(t) h := server.Handler(p, consumers(t, token), nil) rec := do(h, "GET", suite.RouteS1+"?cursor=17.4421", token, nil) if _, err := suite.DecodeS1(rec.Code, rec.Body.Bytes()); !errors.Is(err, suite.ErrCursorReset) { t.Errorf("a foreign cursor: %d %q, %v; want the reset", rec.Code, rec.Body.String(), err) } if rec.Code != 409 || rec.Body.String() != suite.CursorResetBody || rec.Header().Get("Content-Type") != "application/json" { t.Errorf("a foreign cursor: %d %q %q, want exactly 409 %s as JSON", rec.Code, rec.Body.String(), rec.Header().Get("Content-Type"), suite.CursorResetBody) } // The cursors the log did issue still read. if rec := do(h, "GET", suite.RouteS1+"?cursor=end", token, nil); rec.Code != 200 { t.Errorf("the log's own cursor: %d", rec.Code) } p.resetStart = true if rec := do(h, "GET", suite.RouteS1+"?cursor=", token, nil); rec.Code != 500 { t.Errorf("the beginning reset: %d, want 500", rec.Code) } } // Every response the package writes is one nobody may keep, and refusals of // a kind stay byte-identical with the headers on (Fable, review of c38e3ad, // M3). Through Handler, so the 401 is covered, and including the mux's own // 404 and 405, which no route of ours writes. // // MUTATION-TESTED 2026-11-01, each reverted: noStore dropped from routes → // "Routes" (through Handler, Authenticate's own noStore still covers it); // noStore dropped from Authenticate → "no token"; Vary not set → every // response. func TestEveryResponseIsNoStore(t *testing.T) { p := producer(t) h := server.Handler(p, consumers(t, token), nil) ask := `{"asker":{"kind":"person_ref","value":"pers_ana"},"pairs":[{"item_id":"doc-team","revision":7}]}` cases := map[string]func() *httptest.ResponseRecorder{ "S1 200": func() *httptest.ResponseRecorder { return do(h, "GET", suite.RouteS1, token, nil) }, "S1 409": func() *httptest.ResponseRecorder { return do(h, "GET", suite.RouteS1+"?cursor=x", token, nil) }, "S2 200": func() *httptest.ResponseRecorder { return do(h, "POST", suite.RouteS2, token, []byte(ask)) }, "S2 400": func() *httptest.ResponseRecorder { return do(h, "POST", suite.RouteS2, token, []byte("nope")) }, "S3 200": func() *httptest.ResponseRecorder { return do(h, "GET", suite.RouteS3+"/doc-team?revision=7", token, nil) }, "S3 404": func() *httptest.ResponseRecorder { return do(h, "GET", suite.RouteS3+"/doc-deleted?revision=6", token, nil) }, "S3 POST": func() *httptest.ResponseRecorder { return do(h, "POST", suite.RouteS3+"/doc-team?revision=7", token, nil) }, "no such route": func() *httptest.ResponseRecorder { return do(h, "GET", "/suite/s9", token, nil) }, "no token": func() *httptest.ResponseRecorder { return do(h, "GET", suite.RouteS1, "", nil) }, "a wrong token": func() *httptest.ResponseRecorder { return do(h, "GET", suite.RouteS3+"/doc-team?revision=7", "nope", nil) }, } for name, run := range cases { rec := run() if rec.Header().Get("Cache-Control") != "no-store" || rec.Header().Get("Vary") != "Authorization" { t.Errorf("%s (%d): Cache-Control %q, Vary %q; want no-store and Authorization", name, rec.Code, rec.Header().Get("Cache-Control"), rec.Header().Get("Vary")) } } // The deprecated Routes too, which apps' own tests still call. if rec := do(server.Routes(p, nil), "GET", suite.RouteS3+"/doc-team?revision=7", "", nil); rec.Header().Get("Cache-Control") != "no-store" || rec.Header().Get("Vary") != "Authorization" { t.Errorf("Routes: Cache-Control %q, Vary %q", rec.Header().Get("Cache-Control"), rec.Header().Get("Vary")) } if cases["S3 POST"]().Code == 200 { t.Error("S3 served a POST") } p.feedErr, p.ruleErr, p.contentErr = errors.New("down"), errors.New("down"), errors.New("down") for name, rec := range map[string]*httptest.ResponseRecorder{ "S1 503": do(h, "GET", suite.RouteS1, token, nil), "S2 503": do(h, "POST", suite.RouteS2, token, []byte(ask)), "S3 503": do(h, "GET", suite.RouteS3+"/doc-team?revision=7", token, nil), } { if rec.Code != 503 || rec.Header().Get("Cache-Control") != "no-store" || rec.Header().Get("Vary") != "Authorization" { t.Errorf("%s: %d, Cache-Control %q, Vary %q", name, rec.Code, rec.Header().Get("Cache-Control"), rec.Header().Get("Vary")) } } } // Revision 11's edges (Fable, review of c38e3ad, L5): S2 refuses a revision // under 1 in a pair, as S3 does; a feed row with no touched_at or a revision // under 1 is the producer's mistake, a 500, never a page. // // MUTATION-TESTED 2026-11-01, each reverted: S2's revision check removed → // "revision 0" and "revision -1"; checkFeed's TouchedAt check removed → "no // touched_at"; checkFeed's revision check removed → "revision 0 in the feed". func TestRevision11Edges(t *testing.T) { p := producer(t) h := server.Handler(p, consumers(t, token), nil) for name, rev := range map[string]string{"revision 0": "0", "revision -1": "-1"} { body := `{"asker":{"kind":"person_ref","value":"pers_ana"},"pairs":[{"item_id":"doc-team","revision":` + rev + `}]}` if rec := do(h, "POST", suite.RouteS2, token, []byte(body)); rec.Code != 400 { t.Errorf("%s in an S2 pair: %d, want 400", name, rec.Code) } } p.zeroTime = true if rec := do(h, "GET", suite.RouteS1, token, nil); rec.Code != 500 { t.Errorf("no touched_at: %d, want 500", rec.Code) } p.zeroTime, p.zeroRev = false, true if rec := do(h, "GET", suite.RouteS1, token, nil); rec.Code != 500 { t.Errorf("revision 0 in the feed: %d, want 500", rec.Code) } }