From f3fc241849af1f1ccda9fb26de2777b3f84c876a Mon Sep 17 00:00:00 2001 From: Rohan Verma Date: Tue, 15 Jan 2019 16:27:02 +0530 Subject: [PATCH] fix: decouple http client from library --- policy.go | 1 - simples3.go | 26 +++++++++++++++++++++++--- simples3_test.go | 12 ------------ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/policy.go b/policy.go index aaa8ec6..b169358 100644 --- a/policy.go +++ b/policy.go @@ -51,7 +51,6 @@ const ( serviceName = "s3" defaultUploadURLFormat = "http://%s.s3.amazonaws.com/" // - defaultBucketURLFormat = "%s.s3.amazonaws.com/" // defaultExpirationHour = 1 * time.Hour ) diff --git a/simples3.go b/simples3.go index ea77b67..6296aee 100644 --- a/simples3.go +++ b/simples3.go @@ -27,6 +27,7 @@ type S3 struct { AccessKey string SecretKey string Region string + Client *http.Client URIFormat string } @@ -135,6 +136,13 @@ func newUsingIAMImpl(baseURL, region string) (*S3, error) { }, nil } +func (s3 *S3) getClient() *http.Client { + if s3.Client == nil { + return http.DefaultClient + } + return s3.Client +} + func detectFileSize(body multipart.File) int64 { switch r := body.(type) { case io.Seeker: @@ -150,6 +158,18 @@ func detectFileSize(body multipart.File) int64 { return -1 } +// SetClient can be used to set the http client to be +// used by the package. If client passed is nil, +// http.DefaultClient is used. +func (s3 *S3) SetClient(client *http.Client) *S3 { + if client != nil { + s3.Client = client + } else { + s3.Client = http.DefaultClient + } + return s3 +} + // FileUpload makes a POST call with the file written as multipart // and on successful upload, checks for 200 OK. func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) { @@ -172,7 +192,7 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) { w := multipart.NewWriter(&b) for k, v := range policies.Form { - if err := w.WriteField(k, v); err != nil { + if err = w.WriteField(k, v); err != nil { return UploadResponse{}, err } } @@ -200,7 +220,7 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) { req.Header.Set("Content-Type", w.FormDataContentType()) // Submit the request - client := &http.Client{} + client := s3.getClient() res, err := client.Do(req) if err != nil { return UploadResponse{}, err @@ -268,7 +288,7 @@ func (s3 *S3) FileDelete(u DeleteInput) error { req.Header.Set("Authorization", auth.String()) // Submit the request - client := &http.Client{} + client := s3.getClient() res, err := client.Do(req) if err != nil { return err diff --git a/simples3_test.go b/simples3_test.go index 5249249..d08e7d5 100644 --- a/simples3_test.go +++ b/simples3_test.go @@ -127,18 +127,6 @@ 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`