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) {
|
||||
t.Run("Test", func(t *testing.T) {
|
||||
s := New(
|
||||
|
||||
@@ -129,13 +129,15 @@ func writeHeaderList(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
|
||||
// http://docs.amazonwebservices.com/general/latest/gr/sigv4-create-canonical-request.html
|
||||
if r.Body == nil {
|
||||
b = []byte("")
|
||||
} else {
|
||||
var err error
|
||||
b, err = ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
+8
-13
@@ -13,7 +13,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -89,9 +88,12 @@ func New(region, accessKey, secretKey string) *S3 {
|
||||
// NewUsingIAM automatically generates an Instance of S3
|
||||
// using instance metatdata.
|
||||
func NewUsingIAM(region string) (*S3, error) {
|
||||
return newUsingIAMImpl(securityCredentialsURL, region)
|
||||
}
|
||||
|
||||
func newUsingIAMImpl(baseURL, region string) (*S3, error) {
|
||||
// Get the IAM role
|
||||
resp, err := http.Get(securityCredentialsURL)
|
||||
resp, err := http.Get(baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -105,7 +107,7 @@ func NewUsingIAM(region string) (*S3, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = http.Get(securityCredentialsURL + "/" + string(role))
|
||||
resp, err = http.Get(baseURL + "/" + string(role))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -207,10 +209,7 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
||||
|
||||
// Check the response
|
||||
if res.Status != "201 Created" {
|
||||
err = fmt.Errorf("status code: %s", res.Status)
|
||||
data, _ := ioutil.ReadAll(res.Body)
|
||||
log.Printf("response:%s\n", string(data))
|
||||
return UploadResponse{}, err
|
||||
return UploadResponse{}, fmt.Errorf("status code: %s", res.Status)
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(res.Body)
|
||||
@@ -239,7 +238,6 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
||||
date := req.Header.Get("Date")
|
||||
t := time.Now().UTC()
|
||||
if date != "" {
|
||||
var err error
|
||||
t, err = time.Parse(http.TimeFormat, date)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -276,12 +274,9 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// // Check the response
|
||||
// Check the response
|
||||
if res.Status != "204 No Content" {
|
||||
err = fmt.Errorf("status code: %s", res.Status)
|
||||
data, _ := ioutil.ReadAll(res.Body)
|
||||
log.Printf("response:%s\n", string(data))
|
||||
return err
|
||||
return fmt.Errorf("status code: %s", res.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package simples3
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"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