1package tinyclient23import (4 "context"5 "encoding/json"6 "net/http"7 "net/http/httptest"8 "strings"9 "testing"10)1112type actorFeedBackend struct{ fakeBackend }1314func (b *actorFeedBackend) Query(_ context.Context, method string, _ []json.RawMessage, actor string) (any, error) {15 if method != "queryevents" {16 return nil, nil17 }18 items := []any{map[string]any{"id": "public-article", "content": "Public article", "created_at": 1}}19 if actor != "" {20 items = append(items, map[string]any{"id": "session-only-article", "content": "Session-only article", "created_at": 1})21 }22 return items, nil23}2425func TestRemotePublicFeedsRemainAnonymousWithSession(t *testing.T) {26 backend := &actorFeedBackend{fakeBackend{policy: DefaultPolicy(strings.Repeat("a", 64))}}27 app, err := New(backend, Options{Actor: func(r *http.Request) (string, error) {28 if cookie, err := r.Cookie("tiny_session"); err == nil && cookie.Value == "valid-session" {29 return backend.policy.Owner, nil30 }31 return "", nil32 }})33 if err != nil {34 t.Fatal(err)35 }36 upstream := httptest.NewServer(app)37 defer upstream.Close()38 remote, err := NewRemote(RemoteOptions{BackendURL: upstream.URL, PublicURL: backend.URL()})39 if err != nil {40 t.Fatal(err)41 }42 for _, path := range []string{"/articles.json", "/feed.xml", "/feed"} {43 t.Run(path, func(t *testing.T) {44 anonymous := httptest.NewRecorder()45 app.ServeHTTP(anonymous, httptest.NewRequest("GET", backend.URL()+path, nil))46 if anonymous.Code != 200 || !strings.Contains(anonymous.Body.String(), "public-article") || strings.Contains(anonymous.Body.String(), "session-only-article") {47 t.Fatalf("invalid anonymous feed: %d %s", anonymous.Code, anonymous.Body.String())48 }49 for _, authenticated := range []bool{false, true} {50 request := httptest.NewRequest("GET", backend.URL()+path, nil)51 if authenticated {52 request.AddCookie(&http.Cookie{Name: "tiny_session", Value: "valid-session"})53 }54 integrated, standalone := httptest.NewRecorder(), httptest.NewRecorder()55 app.ServeHTTP(integrated, request.Clone(request.Context()))56 remote.ServeHTTP(standalone, request)57 for name, got := range map[string]*httptest.ResponseRecorder{"integrated": integrated, "standalone": standalone} {58 if got.Code != anonymous.Code || got.Body.String() != anonymous.Body.String() {59 t.Errorf("%s authenticated=%v feed differs from anonymous: %d %s", name, authenticated, got.Code, got.Body.String())60 }61 }62 }63 })64 }65}