This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
storage: Implement StorageHTTPSigner test #49
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3869dec
storage: Implement HTTPSigner test
JinnyYi c72c256
Upgrade go-storage to 4.5.0
JinnyYi 9e9a29a
Set content-length for write
JinnyYi 3a786e1
Implement StorageHTTPSiger test
JinnyYi f4576e3
Merge master into gsp-706
JinnyYi bacddab
Upgrade go-storage
JinnyYi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 TestHTTPSignerRead(t *testing.T, store types.Storager) { | ||
Convey("Given a basic Storager", t, func() { | ||
signer, ok := store.(types.HTTPSigner) | ||
So(ok, ShouldBeTrue) | ||
|
||
Convey("When read via QuerySignHTTP", 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.QuerySignHTTP(types.OpStoragerRead, 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 TestHTTPSignerWrite(t *testing.T, store types.Storager) { | ||
Convey("Given a basic Storager", t, func() { | ||
signer, ok := store.(types.HTTPSigner) | ||
So(ok, ShouldBeTrue) | ||
|
||
Convey("When write via QuerySignHTTP", 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.QuerySignHTTP(types.OpStoragerWrite, path, 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)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to set
size
here?I think we need a demo to make sure our test logic is correct. Maybe in s3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dose it mean set
Content-Length
? I tried to addreq.Header.Add("Content-Length", strconv.FormatInt(size,10))
and tested with qingstor, the request was successful, but the request is chunked transfer encoding. So I removed the content-length setting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the comment for
Header
inhttp.Request
, for client requests, certain headers such as Content-Length and Connection are automatically written when needed and values in Header may be ignored.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll privde a demo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange, chunked transfer encoding should be used for requests that don't have
content-length
header.Let's discuss the detail in the demo of s3 instead.