Created
October 8, 2017 16:50
-
-
Save jessejlt/a5171e7406ede2f2f1581bb3db630898 to your computer and use it in GitHub Desktop.
AWS SDK S3 mocking
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"github.com/aws/aws-sdk-go/service/s3/s3iface" | |
) | |
func main() { | |
svc := NewS3("http://beep.boop") | |
input := &s3.GetObjectInput{ | |
Bucket: aws.String("foo"), | |
Key: aws.String("bar"), | |
} | |
resp, err := svc.GetObject(input) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(resp) | |
} | |
func NewS3(uri string) ObjectStore { | |
// TODO Add URI to session | |
sess := session.Must(session.NewSession()) | |
client := s3.New(sess) | |
return ObjectStore{ | |
Client: client, | |
URL: uri, | |
} | |
} | |
type ObjectStore struct { | |
Client s3iface.S3API | |
URL string | |
} | |
func (svc ObjectStore) GetObject(in *s3.GetObjectInput) (*s3.GetObjectOutput, error) { | |
return svc.Client.GetObject(in) | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"github.com/aws/aws-sdk-go/service/s3/s3iface" | |
) | |
func TestGetObject(t *testing.T) { | |
// Create mock receiver | |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(w, "Hello, client") | |
})) | |
defer ts.Close() | |
// Replaces NewS3 | |
svc := ObjectStore{ | |
Client: mockGetObject{Resp: s3.GetObjectOutput{}}, | |
URL: ts.URL, | |
} | |
input := &s3.GetObjectInput{ | |
Bucket: aws.String("foo"), | |
Key: aws.String("bar"), | |
} | |
resp, err := svc.GetObject(input) | |
if err != nil { | |
t.Fatal(err) | |
} | |
t.Log(resp) | |
} | |
type mockGetObject struct { | |
s3iface.S3API | |
Resp s3.GetObjectOutput | |
} | |
func (m mockGetObject) GetObject(in *s3.GetObjectInput) (*s3.GetObjectOutput, error) { | |
return &m.Resp, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment