|
| 1 | +// =========================================================================== |
| 2 | +// blogger_proxy_readiness_test.go — Regression tests for two Blogger-proxy bugs |
| 3 | +// |
| 4 | +// Issue observed: 2026-07-23 (Goyabu / Naruto Shippuden, debug log) |
| 5 | +// The FIRST play of an episode failed with: |
| 6 | +// "mpv exited before IPC socket was ready: exit status 2 ... (no stderr)" |
| 7 | +// Hint: bundled mpv may be missing DLLs |
| 8 | +// The second attempt (next episode) played fine. mpv was healthy — the hint |
| 9 | +// was misleading. |
| 10 | +// |
| 11 | +// Root causes (player/scraper.go): |
| 12 | +// 1. Redirect handling. The proxy's upstream client was built with |
| 13 | +// .NotFollowRedirects(); when the signed googlevideo URL 302-redirected to |
| 14 | +// a CDN node, surf surfaced that as a "net/http: use last response" error, |
| 15 | +// and the handler turned it into HTTP 502. |
| 16 | +// 2. Readiness masking. The readiness probe treated ANY transport-level |
| 17 | +// success as ready — a 502 (headErr==nil) counted as ready — so mpv was |
| 18 | +// launched against a proxy serving 502 and exited 2. |
| 19 | +// |
| 20 | +// Fixes: |
| 21 | +// 1. proxyClient.CheckRedirect = nil (follow redirects, net/http default). |
| 22 | +// 2. Readiness rejects non-2xx upstream status; on the deadline it returns an |
| 23 | +// error so extractBloggerVideoURL's retry loop re-resolves a fresh CDN host |
| 24 | +// instead of handing mpv a dead stream. |
| 25 | +// |
| 26 | +// Function tested: startBloggerProxyServer (the network-free seam split out of |
| 27 | +// startBloggerProxy so the forwarding + readiness gating are testable). |
| 28 | +// =========================================================================== |
| 29 | + |
| 30 | +package player |
| 31 | + |
| 32 | +import ( |
| 33 | + "io" |
| 34 | + "net/http" |
| 35 | + "net/http/httptest" |
| 36 | + "testing" |
| 37 | + "time" |
| 38 | + |
| 39 | + "github.com/stretchr/testify/assert" |
| 40 | + "github.com/stretchr/testify/require" |
| 41 | +) |
| 42 | + |
| 43 | +// okVideoHandler answers HEAD with 200 and GET with the given body — a stand-in |
| 44 | +// for a healthy googlevideo CDN node. |
| 45 | +func okVideoHandler(body string) http.HandlerFunc { |
| 46 | + return func(w http.ResponseWriter, r *http.Request) { |
| 47 | + w.Header().Set("Content-Type", "video/mp4") |
| 48 | + if r.Method == http.MethodHead { |
| 49 | + w.WriteHeader(http.StatusOK) |
| 50 | + return |
| 51 | + } |
| 52 | + w.WriteHeader(http.StatusOK) |
| 53 | + _, _ = io.WriteString(w, body) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// getBloggerProxyBody performs a GET against the proxy and returns the body. |
| 58 | +func getBloggerProxyBody(t *testing.T, proxyURL string) string { |
| 59 | + t.Helper() |
| 60 | + resp, err := http.Get(proxyURL) //nolint:noctx // local proxy, test-only |
| 61 | + require.NoError(t, err) |
| 62 | + defer func() { _ = resp.Body.Close() }() |
| 63 | + b, err := io.ReadAll(resp.Body) |
| 64 | + require.NoError(t, err) |
| 65 | + return string(b) |
| 66 | +} |
| 67 | + |
| 68 | +func TestStartBloggerProxyServer(t *testing.T) { |
| 69 | + // Mutates the bloggerReadiness* package vars and the bloggerProxy global — |
| 70 | + // not parallel, and subtests run sequentially so they don't fight over the |
| 71 | + // single shared proxy. |
| 72 | + prevTimeout, prevInterval := bloggerReadinessTimeout, bloggerReadinessInterval |
| 73 | + bloggerReadinessTimeout = 300 * time.Millisecond |
| 74 | + bloggerReadinessInterval = 10 * time.Millisecond |
| 75 | + t.Cleanup(func() { |
| 76 | + bloggerReadinessTimeout, bloggerReadinessInterval = prevTimeout, prevInterval |
| 77 | + StopBloggerProxy() |
| 78 | + }) |
| 79 | + |
| 80 | + const videoBody = "FAKE-MP4-BYTES" |
| 81 | + |
| 82 | + t.Run("healthy 200 upstream is served", func(t *testing.T) { |
| 83 | + up := httptest.NewServer(okVideoHandler(videoBody)) |
| 84 | + t.Cleanup(up.Close) |
| 85 | + t.Cleanup(StopBloggerProxy) |
| 86 | + |
| 87 | + proxyURL, err := startBloggerProxyServer(up.URL, &http.Client{}) |
| 88 | + require.NoError(t, err) |
| 89 | + assert.Equal(t, videoBody, getBloggerProxyBody(t, proxyURL)) |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("follows an upstream redirect (regression: use-last-response -> 502)", func(t *testing.T) { |
| 93 | + final := httptest.NewServer(okVideoHandler(videoBody)) |
| 94 | + t.Cleanup(final.Close) |
| 95 | + redir := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 96 | + http.Redirect(w, r, final.URL, http.StatusFound) |
| 97 | + })) |
| 98 | + t.Cleanup(redir.Close) |
| 99 | + t.Cleanup(StopBloggerProxy) |
| 100 | + |
| 101 | + // A default client (CheckRedirect==nil) follows the redirect — this is |
| 102 | + // the production fix. Before it, the proxy 502'd on the redirect. |
| 103 | + proxyURL, err := startBloggerProxyServer(redir.URL, &http.Client{}) |
| 104 | + require.NoError(t, err, "a redirect-following client must reach the real 200 video") |
| 105 | + assert.Equal(t, videoBody, getBloggerProxyBody(t, proxyURL)) |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("non-following client never yields a 2xx (the old bug shape)", func(t *testing.T) { |
| 109 | + final := httptest.NewServer(okVideoHandler(videoBody)) |
| 110 | + t.Cleanup(final.Close) |
| 111 | + redir := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 112 | + http.Redirect(w, r, final.URL, http.StatusFound) |
| 113 | + })) |
| 114 | + t.Cleanup(redir.Close) |
| 115 | + t.Cleanup(StopBloggerProxy) |
| 116 | + |
| 117 | + // Models the removed .NotFollowRedirects(): the proxy forwards the 302 |
| 118 | + // (never the final 200), so readiness never observes a 2xx and fails. |
| 119 | + noFollow := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { |
| 120 | + return http.ErrUseLastResponse |
| 121 | + }} |
| 122 | + _, err := startBloggerProxyServer(redir.URL, noFollow) |
| 123 | + require.Error(t, err, "without redirect-follow the proxy never serves a 2xx video") |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("rejects a 502 upstream instead of masking it", func(t *testing.T) { |
| 127 | + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 128 | + w.WriteHeader(http.StatusBadGateway) |
| 129 | + })) |
| 130 | + t.Cleanup(up.Close) |
| 131 | + t.Cleanup(StopBloggerProxy) |
| 132 | + |
| 133 | + _, err := startBloggerProxyServer(up.URL, &http.Client{}) |
| 134 | + require.Error(t, err, "a 502-serving upstream must not be reported as ready") |
| 135 | + assert.Contains(t, err.Error(), "502") |
| 136 | + }) |
| 137 | + |
| 138 | + t.Run("accepts a 206 partial-content upstream", func(t *testing.T) { |
| 139 | + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 140 | + w.Header().Set("Content-Type", "video/mp4") |
| 141 | + w.WriteHeader(http.StatusPartialContent) |
| 142 | + if r.Method == http.MethodGet { |
| 143 | + _, _ = io.WriteString(w, videoBody) |
| 144 | + } |
| 145 | + })) |
| 146 | + t.Cleanup(up.Close) |
| 147 | + t.Cleanup(StopBloggerProxy) |
| 148 | + |
| 149 | + _, err := startBloggerProxyServer(up.URL, &http.Client{}) |
| 150 | + require.NoError(t, err, "206 is a valid streaming status and must count as ready") |
| 151 | + }) |
| 152 | +} |
0 commit comments