test: NewUsingIAM mock
This commit is contained in:
@@ -49,6 +49,29 @@ func TestS3_GeneratePresignedURL_Personal(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestS3_GeneratePresignedURL_ExtraHeader(t *testing.T) {
|
||||||
|
t.Run("Test", func(t *testing.T) {
|
||||||
|
s := New(
|
||||||
|
os.Getenv("AWS_S3_REGION"),
|
||||||
|
os.Getenv("AWS_S3_ACCESS_KEY"),
|
||||||
|
os.Getenv("AWS_S3_SECRET_KEY"),
|
||||||
|
)
|
||||||
|
dontwant := ""
|
||||||
|
if got := s.GeneratePresignedURL(PresignedInput{
|
||||||
|
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
||||||
|
ObjectKey: "test2.txt",
|
||||||
|
Method: "GET",
|
||||||
|
Timestamp: NowTime(),
|
||||||
|
ExpirySeconds: 3600,
|
||||||
|
ExtraHeaders: map[string]string{
|
||||||
|
"x-amz-meta-test": "test",
|
||||||
|
},
|
||||||
|
}); got == dontwant {
|
||||||
|
t.Errorf("S3.GeneratePresignedURL() = %v, dontwant %v", got, dontwant)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestS3_GeneratePresignedURL_PUT(t *testing.T) {
|
func TestS3_GeneratePresignedURL_PUT(t *testing.T) {
|
||||||
t.Run("Test", func(t *testing.T) {
|
t.Run("Test", func(t *testing.T) {
|
||||||
s := New(
|
s := New(
|
||||||
|
|||||||
@@ -129,13 +129,15 @@ func writeHeaderList(w io.Writer, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeBody(w io.Writer, r *http.Request) {
|
func writeBody(w io.Writer, r *http.Request) {
|
||||||
var b []byte
|
var (
|
||||||
|
b []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
// If the payload is empty, use the empty string as the input to the SHA256 function
|
// If the payload is empty, use the empty string as the input to the SHA256 function
|
||||||
// http://docs.amazonwebservices.com/general/latest/gr/sigv4-create-canonical-request.html
|
// http://docs.amazonwebservices.com/general/latest/gr/sigv4-create-canonical-request.html
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
b = []byte("")
|
b = []byte("")
|
||||||
} else {
|
} else {
|
||||||
var err error
|
|
||||||
b, err = ioutil.ReadAll(r.Body)
|
b, err = ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
+8
-13
@@ -13,7 +13,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -89,9 +88,12 @@ func New(region, accessKey, secretKey string) *S3 {
|
|||||||
// NewUsingIAM automatically generates an Instance of S3
|
// NewUsingIAM automatically generates an Instance of S3
|
||||||
// using instance metatdata.
|
// using instance metatdata.
|
||||||
func NewUsingIAM(region string) (*S3, error) {
|
func NewUsingIAM(region string) (*S3, error) {
|
||||||
|
return newUsingIAMImpl(securityCredentialsURL, region)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newUsingIAMImpl(baseURL, region string) (*S3, error) {
|
||||||
// Get the IAM role
|
// Get the IAM role
|
||||||
resp, err := http.Get(securityCredentialsURL)
|
resp, err := http.Get(baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -105,7 +107,7 @@ func NewUsingIAM(region string) (*S3, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err = http.Get(securityCredentialsURL + "/" + string(role))
|
resp, err = http.Get(baseURL + "/" + string(role))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -207,10 +209,7 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
|||||||
|
|
||||||
// Check the response
|
// Check the response
|
||||||
if res.Status != "201 Created" {
|
if res.Status != "201 Created" {
|
||||||
err = fmt.Errorf("status code: %s", res.Status)
|
return UploadResponse{}, fmt.Errorf("status code: %s", res.Status)
|
||||||
data, _ := ioutil.ReadAll(res.Body)
|
|
||||||
log.Printf("response:%s\n", string(data))
|
|
||||||
return UploadResponse{}, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(res.Body)
|
data, err := ioutil.ReadAll(res.Body)
|
||||||
@@ -239,7 +238,6 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
|||||||
date := req.Header.Get("Date")
|
date := req.Header.Get("Date")
|
||||||
t := time.Now().UTC()
|
t := time.Now().UTC()
|
||||||
if date != "" {
|
if date != "" {
|
||||||
var err error
|
|
||||||
t, err = time.Parse(http.TimeFormat, date)
|
t, err = time.Parse(http.TimeFormat, date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -276,12 +274,9 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Check the response
|
// Check the response
|
||||||
if res.Status != "204 No Content" {
|
if res.Status != "204 No Content" {
|
||||||
err = fmt.Errorf("status code: %s", res.Status)
|
return fmt.Errorf("status code: %s", res.Status)
|
||||||
data, _ := ioutil.ReadAll(res.Body)
|
|
||||||
log.Printf("response:%s\n", string(data))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package simples3
|
package simples3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -123,3 +126,48 @@ func TestS3_FileDelete(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MockClient struct {
|
||||||
|
DoFunc func(req *http.Request) (*http.Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
if m.DoFunc != nil {
|
||||||
|
return m.DoFunc(req)
|
||||||
|
}
|
||||||
|
// just in case you want default correct return value
|
||||||
|
return &http.Response{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestS3_NewUsingIAM(t *testing.T) {
|
||||||
|
var (
|
||||||
|
iam = `test-new-s3-using-iam`
|
||||||
|
resp = `{"Code" : "Success","LastUpdated" : "2018-12-24T10:18:01Z",
|
||||||
|
"Type" : "AWS-HMAC","AccessKeyId" : "abc",
|
||||||
|
"SecretAccessKey" : "abc","Token" : "abc",
|
||||||
|
"Expiration" : "2018-12-24T16:24:59Z"}`
|
||||||
|
)
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "GET" {
|
||||||
|
t.Errorf("Expected 'GET' request, got '%s'", r.Method)
|
||||||
|
}
|
||||||
|
if r.URL.EscapedPath() == "/" {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
io.WriteString(w, iam)
|
||||||
|
}
|
||||||
|
if r.URL.EscapedPath() == "/"+iam {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
io.WriteString(w, resp)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
s3, err := newUsingIAMImpl(ts.URL, "abc")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("S3.FileDelete() error = %v", err)
|
||||||
|
}
|
||||||
|
if s3.AccessKey != "abc" && s3.SecretKey != "abc" && s3.Region != "abc" {
|
||||||
|
t.Errorf("S3.FileDelete() got = %v", s3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user