|
| 1 | +package strategy_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/alecthomas/assert/v2" |
| 14 | + |
| 15 | + "github.com/block/cachew/internal/cache" |
| 16 | + "github.com/block/cachew/internal/logging" |
| 17 | + "github.com/block/cachew/internal/strategy" |
| 18 | +) |
| 19 | + |
| 20 | +// httpTransportMutexAndroidSDK ensures android-sdk tests don't run in parallel |
| 21 | +// since they modify the global http.DefaultTransport |
| 22 | +var httpTransportMutexAndroidSDK sync.Mutex //nolint:gochecknoglobals |
| 23 | + |
| 24 | +type mockAndroidSDKTransport struct { |
| 25 | + backend *httptest.Server |
| 26 | + originalTransport http.RoundTripper |
| 27 | +} |
| 28 | + |
| 29 | +func (m *mockAndroidSDKTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 30 | + if req.Host == "example.com" { |
| 31 | + newReq := req.Clone(req.Context()) |
| 32 | + newReq.URL.Scheme = "http" |
| 33 | + newReq.URL.Host = m.backend.Listener.Addr().String() |
| 34 | + return m.originalTransport.RoundTrip(newReq) |
| 35 | + } |
| 36 | + return m.originalTransport.RoundTrip(req) |
| 37 | +} |
| 38 | + |
| 39 | +// ttlSpyCache wraps a cache and records the TTL passed to each Create call. |
| 40 | +type ttlSpyCache struct { |
| 41 | + cache.Cache |
| 42 | + mu sync.Mutex |
| 43 | + ttls []time.Duration |
| 44 | +} |
| 45 | + |
| 46 | +func (c *ttlSpyCache) Create(ctx context.Context, key cache.Key, headers http.Header, ttl time.Duration) (io.WriteCloser, error) { |
| 47 | + c.mu.Lock() |
| 48 | + c.ttls = append(c.ttls, ttl) |
| 49 | + c.mu.Unlock() |
| 50 | + return c.Cache.Create(ctx, key, headers, ttl) |
| 51 | +} |
| 52 | + |
| 53 | +func setupAndroidSDKWithSpy(t *testing.T, feedTTL time.Duration, backend *httptest.Server) (*http.ServeMux, *ttlSpyCache, context.Context) { |
| 54 | + t.Helper() |
| 55 | + |
| 56 | + httpTransportMutexAndroidSDK.Lock() |
| 57 | + t.Cleanup(httpTransportMutexAndroidSDK.Unlock) |
| 58 | + |
| 59 | + originalTransport := http.DefaultTransport |
| 60 | + t.Cleanup(func() { http.DefaultTransport = originalTransport }) //nolint:reassign |
| 61 | + http.DefaultTransport = &mockAndroidSDKTransport{backend: backend, originalTransport: originalTransport} //nolint:reassign |
| 62 | + |
| 63 | + _, ctx := logging.Configure(context.Background(), logging.Config{Level: slog.LevelError}) |
| 64 | + memCache, err := cache.NewMemory(ctx, cache.MemoryConfig{MaxTTL: time.Hour}) |
| 65 | + assert.NoError(t, err) |
| 66 | + t.Cleanup(func() { memCache.Close() }) |
| 67 | + |
| 68 | + spy := &ttlSpyCache{Cache: memCache} |
| 69 | + mux := http.NewServeMux() |
| 70 | + _, err = strategy.NewAndroidSDK(ctx, strategy.AndroidSDKConfig{FeedTTL: feedTTL}, spy, mux) |
| 71 | + assert.NoError(t, err) |
| 72 | + |
| 73 | + return mux, spy, ctx |
| 74 | +} |
| 75 | + |
| 76 | +func TestAndroidSDKTTLByFileType(t *testing.T) { |
| 77 | + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 78 | + w.WriteHeader(http.StatusOK) |
| 79 | + _, _ = w.Write([]byte("response")) |
| 80 | + })) |
| 81 | + defer backend.Close() |
| 82 | + |
| 83 | + feedTTL := 30 * time.Minute |
| 84 | + mux, spy, ctx := setupAndroidSDKWithSpy(t, feedTTL, backend) |
| 85 | + |
| 86 | + tests := []struct { |
| 87 | + name string |
| 88 | + path string |
| 89 | + expectedTTL time.Duration |
| 90 | + }{ |
| 91 | + {"XML feed gets FeedTTL", "/android-sdk/example.com/repository2-3.xml", feedTTL}, |
| 92 | + {"ZIP archive gets long TTL", "/android-sdk/example.com/platform-36.zip", 365 * 24 * time.Hour}, |
| 93 | + {"TXT file gets long TTL", "/android-sdk/example.com/checksums.txt", 365 * 24 * time.Hour}, |
| 94 | + {"JAR file gets long TTL", "/android-sdk/example.com/some-tool.jar", 365 * 24 * time.Hour}, |
| 95 | + } |
| 96 | + |
| 97 | + for i, tt := range tests { |
| 98 | + req := httptest.NewRequestWithContext(ctx, http.MethodGet, tt.path, nil) |
| 99 | + w := httptest.NewRecorder() |
| 100 | + mux.ServeHTTP(w, req) |
| 101 | + assert.Equal(t, http.StatusOK, w.Code, tt.name) |
| 102 | + |
| 103 | + spy.mu.Lock() |
| 104 | + assert.Equal(t, tt.expectedTTL, spy.ttls[i], tt.name) |
| 105 | + spy.mu.Unlock() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestAndroidSDKCaching(t *testing.T) { |
| 110 | + callCount := 0 |
| 111 | + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 112 | + callCount++ |
| 113 | + w.WriteHeader(http.StatusOK) |
| 114 | + _, _ = w.Write([]byte("cached-content")) |
| 115 | + })) |
| 116 | + defer backend.Close() |
| 117 | + |
| 118 | + mux, _, ctx := setupAndroidSDKWithSpy(t, time.Hour, backend) |
| 119 | + |
| 120 | + // First request: cache miss |
| 121 | + req1 := httptest.NewRequestWithContext(ctx, http.MethodGet, "/android-sdk/example.com/sdk/platform-36_r02.zip", nil) |
| 122 | + w1 := httptest.NewRecorder() |
| 123 | + mux.ServeHTTP(w1, req1) |
| 124 | + assert.Equal(t, http.StatusOK, w1.Code) |
| 125 | + assert.Equal(t, 1, callCount) |
| 126 | + |
| 127 | + // Second request: cache hit, no additional backend call |
| 128 | + req2 := httptest.NewRequestWithContext(ctx, http.MethodGet, "/android-sdk/example.com/sdk/platform-36_r02.zip", nil) |
| 129 | + w2 := httptest.NewRecorder() |
| 130 | + mux.ServeHTTP(w2, req2) |
| 131 | + assert.Equal(t, http.StatusOK, w2.Code) |
| 132 | + assert.Equal(t, 1, callCount, "should be served from cache") |
| 133 | + assert.Equal(t, "cached-content", w2.Body.String()) |
| 134 | +} |
| 135 | + |
| 136 | +func TestAndroidSDKURLReconstruction(t *testing.T) { |
| 137 | + var receivedURL string |
| 138 | + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | + receivedURL = r.RequestURI |
| 140 | + w.WriteHeader(http.StatusOK) |
| 141 | + _, _ = w.Write([]byte("ok")) |
| 142 | + })) |
| 143 | + defer backend.Close() |
| 144 | + |
| 145 | + mux, _, ctx := setupAndroidSDKWithSpy(t, time.Hour, backend) |
| 146 | + |
| 147 | + tests := []struct { |
| 148 | + name string |
| 149 | + requestPath string |
| 150 | + expectedURI string |
| 151 | + }{ |
| 152 | + {"simple path", "/android-sdk/example.com/path/to/resource.zip", "/path/to/resource.zip"}, |
| 153 | + {"with query params", "/android-sdk/example.com/repo.xml?v=2&channel=stable", "/repo.xml?v=2&channel=stable"}, |
| 154 | + } |
| 155 | + |
| 156 | + for _, tt := range tests { |
| 157 | + receivedURL = "" |
| 158 | + req := httptest.NewRequestWithContext(ctx, http.MethodGet, tt.requestPath, nil) |
| 159 | + w := httptest.NewRecorder() |
| 160 | + mux.ServeHTTP(w, req) |
| 161 | + assert.Equal(t, http.StatusOK, w.Code, tt.name) |
| 162 | + assert.Equal(t, tt.expectedURI, receivedURL, tt.name) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func TestAndroidSDKMultipleFeedTypes(t *testing.T) { |
| 167 | + callCount := 0 |
| 168 | + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 169 | + callCount++ |
| 170 | + w.WriteHeader(http.StatusOK) |
| 171 | + _, _ = w.Write([]byte("response")) |
| 172 | + })) |
| 173 | + defer backend.Close() |
| 174 | + |
| 175 | + mux, _, ctx := setupAndroidSDKWithSpy(t, time.Hour, backend) |
| 176 | + |
| 177 | + paths := []string{ |
| 178 | + "/android-sdk/example.com/repository2-3.xml", |
| 179 | + "/android-sdk/example.com/platform-36.zip", |
| 180 | + "/android-sdk/example.com/checksums.txt", |
| 181 | + } |
| 182 | + |
| 183 | + for i, path := range paths { |
| 184 | + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) |
| 185 | + w := httptest.NewRecorder() |
| 186 | + mux.ServeHTTP(w, req) |
| 187 | + assert.Equal(t, http.StatusOK, w.Code) |
| 188 | + assert.Equal(t, i+1, callCount) |
| 189 | + |
| 190 | + // Second request should always be cached |
| 191 | + req2 := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) |
| 192 | + w2 := httptest.NewRecorder() |
| 193 | + mux.ServeHTTP(w2, req2) |
| 194 | + assert.Equal(t, i+1, callCount, "path %s should be served from cache", path) |
| 195 | + } |
| 196 | +} |
0 commit comments