@@ -549,13 +549,57 @@ func TestCatalogStage(t *testing.T) {
549549 fmt .Fprint (w , generateComponentsResponse (prefix , apiComponentCount ))
550550 })
551551
552+ url := "s3-download"
553+
554+ fakeServer .MockAPI (url , func (w http.ResponseWriter , r * http.Request ) {
555+ dir , _ := os .MkdirTemp ("" , "cdk-component-stage-tar-gz-" )
556+
557+ path := MakeGzip (name , MakeTar (name , "1.0.0" , dir , "component" , "sig" ))
558+
559+ data , err := os .ReadFile (path )
560+ if err != nil {
561+ panic (err )
562+ }
563+
564+ w .Write (data )
565+ })
566+
567+ XMLUrl := "s3-error"
568+
569+ fakeServer .MockAPI (XMLUrl , func (w http.ResponseWriter , r * http.Request ) {
570+ data := []byte (`<?xml version="1.0" encoding="UTF-8"?>
571+ <Error><Code>PermanentRedirect</Code>
572+ <Message>The bucket you are attempting to access must be addressed using the specified endpoint.
573+ Please send all future requests to this endpoint.</Message>
574+ <Endpoint>lw-cdk-store.s3-us-west-2.amazonaws.com</Endpoint>
575+ <Bucket>lw-cdk-store</Bucket><RequestId>VFXE02WRA7339CW6</RequestId><HostId></HostId></Error>` )
576+ w .Write (data )
577+ })
578+
579+ EOFUrl := "eof"
580+
581+ fakeServer .MockAPI (EOFUrl , func (w http.ResponseWriter , r * http.Request ) {
582+ data := []byte ("" )
583+ w .Write (data )
584+ })
585+
552586 fakeServer .MockAPI ("Components/Artifact/1" , func (w http.ResponseWriter , r * http.Request ) {
553587 assert .Equal (t , "GET" , r .Method , "Components API only accepts HTTP GET" )
554588
555- if r .URL .Query ().Get ("version" ) != version {
556- http .Error (w , "component version not found" , http .StatusNotFound )
557- } else {
558- fmt .Fprint (w , generateFetchResponse (1 , name , version , "" ))
589+ l := r .URL .Query ().Get ("version" )
590+ switch l {
591+ case "1.0.0" :
592+ {
593+ fmt .Fprint (w , generateFetchResponse (1 , name , version , fmt .Sprintf ("%s/api/v2/%s" , fakeServer .URL (), url )))
594+ }
595+ case "3.0.1" :
596+ {
597+ fmt .Fprint (w , generateFetchResponse (1 , name , version , fmt .Sprintf ("%s/api/v2/%s" , fakeServer .URL (), EOFUrl )))
598+ }
599+ case "5.4.3" :
600+ {
601+ fmt .Fprint (w , generateFetchResponse (1 , name , version , fmt .Sprintf ("%s/api/v2/%s" , fakeServer .URL (), XMLUrl )))
602+ }
559603 }
560604 })
561605
@@ -575,7 +619,7 @@ func TestCatalogStage(t *testing.T) {
575619 api .WithURL (fakeServer .URL ()),
576620 )
577621
578- catalog , err := lwcomponent .NewCatalog (client , newTestStage )
622+ catalog , err := lwcomponent .NewCatalog (client , lwcomponent . NewStageTarGz )
579623 assert .NotNil (t , catalog )
580624 assert .Nil (t , err )
581625
@@ -589,6 +633,29 @@ func TestCatalogStage(t *testing.T) {
589633 defer stageClose ()
590634 })
591635
636+ // @jon-stewart: TODO GROW-2765
637+ // t.Run("EOF Error", func(t *testing.T) {
638+ // component, err := catalog.GetComponent(name)
639+ // assert.NotNil(t, component)
640+ // assert.Nil(t, err)
641+
642+ // stageClose, err := catalog.Stage(component, "3.0.1", ProgressClosure)
643+ // assert.NotNil(t, err)
644+ // defer stageClose()
645+ // })
646+
647+ t .Run ("AWS XML Error" , func (t * testing.T ) {
648+ component , err := catalog .GetComponent (name )
649+ assert .NotNil (t , component )
650+ assert .Nil (t , err )
651+
652+ stageClose , err := catalog .Stage (component , "5.4.3" , ProgressClosure )
653+ assert .NotNil (t , err )
654+ assert .Contains (t , err .Error (), "PermanentRedirect" )
655+
656+ defer stageClose ()
657+ })
658+
592659 t .Run ("already installed" , func (t * testing.T ) {
593660 CreateLocalComponent (name , version , false )
594661
@@ -646,8 +713,19 @@ func (t *testStage) Directory() string {
646713 return t .dir
647714}
648715
716+ // Filename implements lwcomponent.Stager.
717+ func (t * testStage ) Filename () string {
718+ return "newTestStageFile"
719+ }
720+
649721// Download implements lwcomponent.Stager.
650- func (* testStage ) Download (func (string , int64 )) error {
722+ func (t * testStage ) Download (func (string , int64 )) error {
723+ file , err := os .Create (filepath .Join (t .dir , t .Filename ()))
724+ if err != nil {
725+ log .Fatal (err )
726+ }
727+ defer file .Close ()
728+
651729 return nil
652730}
653731
@@ -678,7 +756,12 @@ func (*testStage) Validate() error {
678756}
679757
680758func newTestStage (name , artifactUrl string , size int64 ) (stage lwcomponent.Stager , err error ) {
681- stage = & testStage {}
759+ dir , err := os .MkdirTemp ("" , "newTestStage" )
760+ if err != nil {
761+ panic (err )
762+ }
763+
764+ stage = & testStage {dir : dir }
682765
683766 return
684767}
0 commit comments