|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/git-pkgs/cooldown" |
| 13 | +) |
| 14 | + |
| 15 | +// cutMetadataUpstream serves one npm packument and, when cut is set, closes |
| 16 | +// the connection halfway through the document it announced. |
| 17 | +type cutMetadataUpstream struct { |
| 18 | + *httptest.Server |
| 19 | + mu sync.Mutex |
| 20 | + body string |
| 21 | + cut bool |
| 22 | +} |
| 23 | + |
| 24 | +func newCutMetadataUpstream(t *testing.T, body string) *cutMetadataUpstream { |
| 25 | + t.Helper() |
| 26 | + u := &cutMetadataUpstream{body: body} |
| 27 | + u.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | + u.mu.Lock() |
| 29 | + body, cut := u.body, u.cut |
| 30 | + u.mu.Unlock() |
| 31 | + if r.URL.Path != "/pkg" { |
| 32 | + http.NotFound(w, r) |
| 33 | + return |
| 34 | + } |
| 35 | + w.Header().Set("Content-Type", "application/json") |
| 36 | + w.Header().Set("Content-Length", fmt.Sprint(len(body))) |
| 37 | + if cut { |
| 38 | + _, _ = io.WriteString(w, body[:len(body)/2]) |
| 39 | + return |
| 40 | + } |
| 41 | + _, _ = io.WriteString(w, body) |
| 42 | + })) |
| 43 | + t.Cleanup(u.Close) |
| 44 | + return u |
| 45 | +} |
| 46 | + |
| 47 | +func (u *cutMetadataUpstream) set(body string, cut bool) { |
| 48 | + u.mu.Lock() |
| 49 | + defer u.mu.Unlock() |
| 50 | + u.body, u.cut = body, cut |
| 51 | +} |
| 52 | + |
| 53 | +// truncPackument is an npm packument with n versions published long ago. |
| 54 | +func truncPackument(n int) string { |
| 55 | + var versions, times []string |
| 56 | + for i := range n { |
| 57 | + v := fmt.Sprintf("1.0.%d", i) |
| 58 | + versions = append(versions, fmt.Sprintf(`%q:{"name":"pkg","version":%q,"dist":{"tarball":"https://registry.npmjs.org/pkg/-/pkg-%s.tgz"}}`, v, v, v)) |
| 59 | + times = append(times, fmt.Sprintf(`%q:"2020-01-01T00:00:00Z"`, v)) |
| 60 | + } |
| 61 | + return `{"name":"pkg","dist-tags":{"latest":"1.0.0"},"versions":{` + strings.Join(versions, ",") + |
| 62 | + `},"time":{` + strings.Join(times, ",") + `}}` |
| 63 | +} |
| 64 | + |
| 65 | +// TestMetadataTruncatedUpstream checks that a document upstream cuts off |
| 66 | +// before its Content-Length is neither served nor cached: the cached copy is |
| 67 | +// served instead, with and without cooldown. |
| 68 | +func TestMetadataTruncatedUpstream(t *testing.T) { |
| 69 | + for _, withCooldown := range []bool{false, true} { |
| 70 | + t.Run(map[bool]string{false: "no cooldown", true: "cooldown"}[withCooldown], func(t *testing.T) { |
| 71 | + u := newCutMetadataUpstream(t, truncPackument(50)) |
| 72 | + proxy, db, _, _ := setupTestProxy(t) |
| 73 | + proxy.HTTPClient = u.Client() |
| 74 | + proxy.CacheMetadata = true |
| 75 | + if withCooldown { |
| 76 | + proxy.Cooldown = &cooldown.Config{Default: "3d"} |
| 77 | + } |
| 78 | + handler := NewNPMHandler(proxy, "http://proxy.example", u.URL).Routes() |
| 79 | + |
| 80 | + first := serve(handler, "/pkg", nil) |
| 81 | + if first.Code != http.StatusOK { |
| 82 | + t.Fatalf("first request: status %d", first.Code) |
| 83 | + } |
| 84 | + before, err := db.GetMetadataCache("npm", "pkg") |
| 85 | + if err != nil || before == nil { |
| 86 | + t.Fatalf("no metadata row after the first request: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + u.set(truncPackument(51), true) |
| 90 | + expireMetadata(t, db, "npm", "pkg") |
| 91 | + proxy.SetMetadataRewriteCacheSize(0) |
| 92 | + |
| 93 | + rec := serve(handler, "/pkg", nil) |
| 94 | + if rec.Code != http.StatusOK || rec.Body.String() != first.Body.String() { |
| 95 | + t.Errorf("status %d, want 200 with the cached document", rec.Code) |
| 96 | + } |
| 97 | + after, err := db.GetMetadataCache("npm", "pkg") |
| 98 | + if err != nil || after == nil || after.ContentDigest != before.ContentDigest { |
| 99 | + t.Error("the cut-off document replaced the cached copy") |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// TestMetadataTruncatedUpstreamUncached checks that a cut-off document with |
| 106 | +// nothing cached fails the request and is not cached. |
| 107 | +func TestMetadataTruncatedUpstreamUncached(t *testing.T) { |
| 108 | + u := newCutMetadataUpstream(t, truncPackument(50)) |
| 109 | + u.set(truncPackument(50), true) |
| 110 | + proxy, db, _, _ := setupTestProxy(t) |
| 111 | + proxy.HTTPClient = u.Client() |
| 112 | + proxy.CacheMetadata = true |
| 113 | + handler := NewNPMHandler(proxy, "http://proxy.example", u.URL).Routes() |
| 114 | + |
| 115 | + if rec := serve(handler, "/pkg", nil); rec.Code != http.StatusBadGateway { |
| 116 | + t.Errorf("status %d, want 502", rec.Code) |
| 117 | + } |
| 118 | + if entry, _ := db.GetMetadataCache("npm", "pkg"); entry != nil { |
| 119 | + t.Error("cached the cut-off document") |
| 120 | + } |
| 121 | +} |
0 commit comments