@@ -481,3 +481,95 @@ func TestGetTagsFromURLEnvironmentAuthentication(t *testing.T) {
481481		})
482482	}
483483}
484+ 
485+ func  TestGetTagsCaching (t  * testing.T ) {
486+ 	requestCount  :=  0 
487+ 	server  :=  httptest .NewTLSServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
488+ 		requestCount ++ 
489+ 		t .Logf ("request %d called %s" , requestCount , r .URL .Path )
490+ 
491+ 		responseTags  :=  fakeTagsList {
492+ 			Tags : []string {
493+ 				"1.0.0" ,
494+ 				"1.1.0" ,
495+ 				"2.0.0_beta" ,
496+ 			},
497+ 		}
498+ 
499+ 		w .Header ().Set ("Content-Type" , "application/json" )
500+ 		w .WriteHeader (http .StatusOK )
501+ 		require .NoError (t , json .NewEncoder (w ).Encode (responseTags ))
502+ 	}))
503+ 	t .Cleanup (server .Close )
504+ 
505+ 	serverURL , err  :=  url .Parse (server .URL )
506+ 	require .NoError (t , err )
507+ 
508+ 	t .Run ("should cache tags correctly" , func (t  * testing.T ) {
509+ 		cache  :=  & fakeIndexCache {}
510+ 		client  :=  NewClient (serverURL .Host , HelmCreds {
511+ 			InsecureSkipVerify : true ,
512+ 		}, true , "" , "" , WithIndexCache (cache ))
513+ 
514+ 		tags1 , err  :=  client .GetTags ("mychart" , false )
515+ 		require .NoError (t , err )
516+ 		assert .ElementsMatch (t , tags1 , []string {
517+ 			"1.0.0" ,
518+ 			"1.1.0" ,
519+ 			"2.0.0+beta" ,
520+ 		})
521+ 		assert .Equal (t , 1 , requestCount )
522+ 
523+ 		requestCount  =  0 
524+ 
525+ 		tags2 , err  :=  client .GetTags ("mychart" , false )
526+ 		require .NoError (t , err )
527+ 		assert .ElementsMatch (t , tags2 , []string {
528+ 			"1.0.0" ,
529+ 			"1.1.0" ,
530+ 			"2.0.0+beta" ,
531+ 		})
532+ 		assert .Equal (t , 0 , requestCount )
533+ 
534+ 		assert .NotEmpty (t , cache .data )
535+ 
536+ 		type  entriesStruct  struct  {
537+ 			Tags  []string 
538+ 		}
539+ 		var  entries  entriesStruct 
540+ 		err  =  json .Unmarshal (cache .data , & entries )
541+ 		require .NoError (t , err )
542+ 		assert .ElementsMatch (t , entries .Tags , []string {
543+ 			"1.0.0" ,
544+ 			"1.1.0" ,
545+ 			"2.0.0+beta" ,
546+ 		})
547+ 	})
548+ 
549+ 	t .Run ("should bypass cache when noCache is true" , func (t  * testing.T ) {
550+ 		cache  :=  & fakeIndexCache {}
551+ 		client  :=  NewClient (serverURL .Host , HelmCreds {
552+ 			InsecureSkipVerify : true ,
553+ 		}, true , "" , "" , WithIndexCache (cache ))
554+ 
555+ 		requestCount  =  0 
556+ 
557+ 		tags1 , err  :=  client .GetTags ("mychart" , true )
558+ 		require .NoError (t , err )
559+ 		assert .ElementsMatch (t , tags1 , []string {
560+ 			"1.0.0" ,
561+ 			"1.1.0" ,
562+ 			"2.0.0+beta" ,
563+ 		})
564+ 		assert .Equal (t , 1 , requestCount )
565+ 
566+ 		tags2 , err  :=  client .GetTags ("mychart" , true )
567+ 		require .NoError (t , err )
568+ 		assert .ElementsMatch (t , tags2 , []string {
569+ 			"1.0.0" ,
570+ 			"1.1.0" ,
571+ 			"2.0.0+beta" ,
572+ 		})
573+ 		assert .Equal (t , 2 , requestCount )
574+ 	})
575+ }
0 commit comments