@@ -293,6 +293,62 @@ func TestNPMRewriteMetadataCooldownExemptPackage(t *testing.T) {
293293 }
294294}
295295
296+ func TestNPMHandlerUsesAbbreviatedMetadata (t * testing.T ) {
297+ var gotAccept string
298+ upstream := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
299+ gotAccept = r .Header .Get ("Accept" )
300+ w .Header ().Set ("Content-Type" , "application/json" )
301+ _ , _ = w .Write ([]byte (`{
302+ "name": "testpkg",
303+ "versions": {
304+ "1.0.0": {
305+ "name": "testpkg",
306+ "version": "1.0.0",
307+ "dist": {
308+ "tarball": "https://registry.npmjs.org/testpkg/-/testpkg-1.0.0.tgz"
309+ }
310+ }
311+ }
312+ }` ))
313+ }))
314+ defer upstream .Close ()
315+
316+ t .Run ("no cooldown uses abbreviated metadata" , func (t * testing.T ) {
317+ h := & NPMHandler {
318+ proxy : testProxy (),
319+ upstreamURL : upstream .URL ,
320+ proxyURL : "http://proxy.local" ,
321+ }
322+
323+ req := httptest .NewRequest (http .MethodGet , "/testpkg" , nil )
324+ w := httptest .NewRecorder ()
325+ h .handlePackageMetadata (w , req )
326+
327+ if gotAccept != "application/vnd.npm.install-v1+json" {
328+ t .Errorf ("Accept = %q, want abbreviated metadata header" , gotAccept )
329+ }
330+ })
331+
332+ t .Run ("cooldown enabled uses full metadata" , func (t * testing.T ) {
333+ proxy := testProxy ()
334+ proxy .Cooldown = & cooldown.Config {Default : "3d" }
335+
336+ h := & NPMHandler {
337+ proxy : proxy ,
338+ upstreamURL : upstream .URL ,
339+ proxyURL : "http://proxy.local" ,
340+ }
341+
342+ req := httptest .NewRequest (http .MethodGet , "/testpkg" , nil )
343+ w := httptest .NewRecorder ()
344+ h .handlePackageMetadata (w , req )
345+
346+ if gotAccept == "application/vnd.npm.install-v1+json" {
347+ t .Error ("cooldown enabled should use full metadata, not abbreviated" )
348+ }
349+ })
350+ }
351+
296352func TestNPMHandlerMetadataNotFound (t * testing.T ) {
297353 upstream := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
298354 w .WriteHeader (http .StatusNotFound )
0 commit comments