Skip to content

Commit

Permalink
Merge pull request #7 from twharmon/fakes
Browse files Browse the repository at this point in the history
Add basic fakes
  • Loading branch information
twharmon authored Apr 8, 2022
2 parents 39f4cda + 5f04136 commit 6fbe7c2
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 0 deletions.
66 changes: 66 additions & 0 deletions fakes/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fakes

import (
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/ses/sesiface"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
)

type AWS struct {
dynamodb dynamodbiface.DynamoDBAPI
ses sesiface.SESAPI
s3 s3iface.S3API
sts stsiface.STSAPI
ssm ssmiface.SSMAPI
}

func NewAWS() *AWS {
return &AWS{}
}

func (a *AWS) DynamoDB() dynamodbiface.DynamoDBAPI {
return a.dynamodb
}

func (a *AWS) SES() sesiface.SESAPI {
return a.ses
}

func (a *AWS) S3() s3iface.S3API {
return a.s3
}

func (a *AWS) STS() stsiface.STSAPI {
return a.sts
}

func (a *AWS) SSM() ssmiface.SSMAPI {
return a.ssm
}

func (a *AWS) WithDynamoDB(svc dynamodbiface.DynamoDBAPI) *AWS {
a.dynamodb = svc
return a
}

func (a *AWS) WithSES(svc sesiface.SESAPI) *AWS {
a.ses = svc
return a
}

func (a *AWS) WithS3(svc s3iface.S3API) *AWS {
a.s3 = svc
return a
}

func (a *AWS) WithSTS(svc stsiface.STSAPI) *AWS {
a.sts = svc
return a
}

func (a *AWS) WithSSM(svc ssmiface.SSMAPI) *AWS {
a.ssm = svc
return a
}
70 changes: 70 additions & 0 deletions fakes/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package fakes

type Context struct {
request *Request
response *Response
aws *AWS
}

func NewContext() *Context {
return &Context{}
}

func (c *Context) Request() *Request {
return c.request
}

func (c *Context) AWS() *AWS {
return c.aws
}

func (c *Context) Response() *Response {
return c.response
}

func (c *Context) WithRequest(r *Request) *Context {
c.request = r
return c
}

func (c *Context) WithAWS(a *AWS) *Context {
c.aws = a
return c
}

func (c *Context) WithResponse(r *Response) *Context {
c.response = r
return c
}

func (c *Context) LogDebug(message string, args ...interface{}) {

}

func (c *Context) LogInfo(message string, args ...interface{}) {

}

func (c *Context) LogNotice(message string, args ...interface{}) {

}

func (c *Context) LogWarning(message string, args ...interface{}) {

}

func (c *Context) LogError(message string, args ...interface{}) {

}

func (c *Context) LogCritical(message string, args ...interface{}) {

}

func (c *Context) LogAlert(message string, args ...interface{}) {

}

func (c *Context) LogEmergency(message string, args ...interface{}) {

}
83 changes: 83 additions & 0 deletions fakes/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package fakes

import (
"encoding/json"
"net/http"
)

type Request struct {
query map[string]string
path map[string]string
header map[string]string
cookie map[string]*http.Cookie
body []byte
rawPath string
}

func NewRequest() *Request {
return &Request{
query: make(map[string]string),
path: make(map[string]string),
header: make(map[string]string),
cookie: make(map[string]*http.Cookie),
}
}

func (r *Request) Query(key string) string {
return r.query[key]
}

func (r *Request) Path(key string) string {
return r.path[key]
}

func (r *Request) Header(key string) string {
return r.header[key]
}

func (r *Request) Headers() map[string]string {
return r.header
}

func (r *Request) Cookie(key string) *http.Cookie {
return r.cookie[key]
}

func (r *Request) RawPath() string {
return r.rawPath
}

func (r *Request) Body(v interface{}) error {
return json.Unmarshal(r.body, v)
}

func (r *Request) WithQuery(query map[string]string) *Request {
r.query = query
return r
}

func (r *Request) WithPath(path map[string]string) *Request {
r.path = path
return r
}

func (r *Request) WithHeaders(header map[string]string) *Request {
r.header = header
return r
}

func (r *Request) WithCookies(cookie map[string]*http.Cookie) *Request {
r.cookie = cookie
return r
}

func (r *Request) WithRawPath(rawPath string) *Request {
r.rawPath = rawPath
return r
}

func (r *Request) WithBody(v interface{}) error {
var err error
r.body, err = json.Marshal(v)
return err
}
100 changes: 100 additions & 0 deletions fakes/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package fakes_test

import (
"net/http"
"reflect"
"testing"

"github.com/twharmon/golamb"
"github.com/twharmon/golamb/fakes"
)

func TestRequestWithQuery(t *testing.T) {
m := fakes.NewRequest()
want := "bar"
m.WithQuery(map[string]string{"foo": want})
var r golamb.Request
r = m
got := r.Query("foo")
if want != got {
t.Fatalf("want: %v; got: %v", want, got)
}
}

func TestRequestWithPath(t *testing.T) {
m := fakes.NewRequest()
want := "bar"
m.WithPath(map[string]string{"foo": want})
var r golamb.Request
r = m
got := r.Path("foo")
if want != got {
t.Fatalf("want: %v; got: %v", want, got)
}
}

func TestRequestWithHeader(t *testing.T) {
m := fakes.NewRequest()
want := "bar"
m.WithHeaders(map[string]string{"foo": want})
var r golamb.Request
r = m
got := r.Header("foo")
if want != got {
t.Fatalf("want: %v; got: %v", want, got)
}
}

func TestRequestWithHeaders(t *testing.T) {
m := fakes.NewRequest()
want := map[string]string{"foo": "bar"}
m.WithHeaders(want)
var r golamb.Request
r = m
got := r.Headers()
if !reflect.DeepEqual(want, got) {
t.Fatalf("want: %v; got: %v", want, got)
}
}

func TestRequestWithCookies(t *testing.T) {
m := fakes.NewRequest()
want := "bar"
m.WithCookies(map[string]*http.Cookie{"foo": {Value: want}})
var r golamb.Request
r = m
got := r.Cookie("foo").Value
if want != got {
t.Fatalf("want: %v; got: %v", want, got)
}
}

func TestRequestWithRawPath(t *testing.T) {
m := fakes.NewRequest()
want := "bar"
m.WithRawPath(want)
var r golamb.Request
r = m
got := r.RawPath()
if want != got {
t.Fatalf("want: %v; got: %v", want, got)
}
}

func TestRequestWithBody(t *testing.T) {
m := fakes.NewRequest()
type T struct {
Foo string
}
want := T{Foo: "bar"}
if err := m.WithBody(&want); err != nil {
t.Fatalf("unexpected err: %s", err)
}
var r golamb.Request
r = m
var got T
r.Body(&got)
if !reflect.DeepEqual(want, got) {
t.Fatalf("want: %v; got: %v", want, got)
}
}
19 changes: 19 additions & 0 deletions fakes/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fakes

import "github.com/aws/aws-lambda-go/events"

type Response struct {
response *events.APIGatewayV2HTTPResponse
err error
}

func NewResponse(response *events.APIGatewayV2HTTPResponse, err error) *Response {
return &Response{
response: response,
err: err,
}
}

func (r *Response) Respond() (*events.APIGatewayV2HTTPResponse, error) {
return r.response, r.err
}

0 comments on commit 6fbe7c2

Please sign in to comment.