diff --git a/go.mod b/go.mod index 47638d8..dbb9983 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/beyondstorage/go-integration-test/v4 go 1.15 require ( - github.com/beyondstorage/go-storage/v4 v4.6.0 + github.com/beyondstorage/go-storage/v4 v4.7.0 github.com/google/uuid v1.3.0 github.com/smartystreets/goconvey v1.6.4 ) diff --git a/go.sum b/go.sum index 04eacbb..31a2faa 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/Xuanwo/templateutils v0.1.0/go.mod h1:OdE0DJ+CJxDBq6psX5DPV+gOZi8bhuHuVUpPCG++Wb8= -github.com/beyondstorage/go-storage/v4 v4.6.0 h1:a05dtbYjMZB7LrUSvVzzHwlx33B4yEmd5oQB7Itk7VY= -github.com/beyondstorage/go-storage/v4 v4.6.0/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw= +github.com/beyondstorage/go-storage/v4 v4.7.0 h1:7hpRFNoPY0vWRSH/p2biD2dUQdZMrs1TuIkkqDefmCE= +github.com/beyondstorage/go-storage/v4 v4.7.0/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw= github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU= github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ= github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= diff --git a/storage_http_signer.go b/storage_http_signer.go new file mode 100644 index 0000000..2fe0b42 --- /dev/null +++ b/storage_http_signer.go @@ -0,0 +1,124 @@ +package tests + +import ( + "bytes" + "crypto/sha256" + "io" + "io/ioutil" + "math/rand" + "net/http" + "testing" + "time" + + "github.com/google/uuid" + . "github.com/smartystreets/goconvey/convey" + + "github.com/beyondstorage/go-storage/v4/pkg/randbytes" + "github.com/beyondstorage/go-storage/v4/types" +) + +func TestStorageHTTPSignerRead(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + signer, ok := store.(types.StorageHTTPSigner) + So(ok, ShouldBeTrue) + + Convey("When Read via QuerySignHTTPRead", func() { + size := rand.Int63n(4 * 1024 * 1024) + content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size)) + if err != nil { + t.Error(err) + } + + path := uuid.New().String() + _, err = store.Write(path, bytes.NewReader(content), size) + if err != nil { + t.Error(err) + } + defer func() { + err := store.Delete(path) + if err != nil { + t.Error(err) + } + }() + + req, err := signer.QuerySignHTTPRead(path, time.Duration(time.Hour)) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + + So(req, ShouldNotBeNil) + So(req.URL, ShouldNotBeNil) + }) + + client := http.Client{} + resp, err := client.Do(req) + Convey("The request returned error should be nil", func() { + So(err, ShouldBeNil) + So(resp, ShouldNotBeNil) + }) + + defer resp.Body.Close() + + buf, err := ioutil.ReadAll(resp.Body) + Convey("The content should be match", func() { + So(err, ShouldBeNil) + So(buf, ShouldNotBeNil) + + So(resp.ContentLength, ShouldEqual, size) + So(sha256.Sum256(buf), ShouldResemble, sha256.Sum256(content)) + }) + }) + }) +} + +func TestStorageHTTPSignerWrite(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + signer, ok := store.(types.StorageHTTPSigner) + So(ok, ShouldBeTrue) + + Convey("When Write via QuerySignHTTPWrite", func() { + size := rand.Int63n(4 * 1024 * 1024) + content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size)) + if err != nil { + t.Error(err) + } + + path := uuid.New().String() + req, err := signer.QuerySignHTTPWrite(path, size, time.Duration(time.Hour)) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + So(req, ShouldNotBeNil) + So(req.URL, ShouldNotBeNil) + }) + + req.Body = ioutil.NopCloser(bytes.NewReader(content)) + + client := http.Client{} + _, err = client.Do(req) + Convey("The request returned error should be nil", func() { + So(err, ShouldBeNil) + }) + + defer func() { + err := store.Delete(path) + if err != nil { + t.Error(err) + } + }() + + Convey("Read should get object data without error", func() { + var buf bytes.Buffer + n, err := store.Read(path, &buf) + + Convey("The content should be match", func() { + So(err, ShouldBeNil) + So(buf, ShouldNotBeNil) + + So(n, ShouldEqual, size) + So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content)) + }) + }) + }) + }) +}