Skip to content

Commit

Permalink
Merge pull request #17 from twharmon/docs
Browse files Browse the repository at this point in the history
Docs
  • Loading branch information
twharmon authored Apr 12, 2022
2 parents a2cec03 + 1fcda31 commit 764f0d3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
13 changes: 13 additions & 0 deletions fakes/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/sts/stsiface"
)

// AWS implements the golamb.AWSServiceProvider interface.
type AWS struct {
dynamodb dynamodbiface.DynamoDBAPI
ses sesiface.SESAPI
Expand All @@ -16,50 +17,62 @@ type AWS struct {
ssm ssmiface.SSMAPI
}

// NewAWS creates a value that implements the
// golamb.AWSServiceProvider interface.
func NewAWS() *AWS {
return &AWS{}
}

// DynamoDB implements the golamb.AWSServiceProvider interface.
func (a *AWS) DynamoDB() dynamodbiface.DynamoDBAPI {
return a.dynamodb
}

// SES implements the golamb.AWSServiceProvider interface.
func (a *AWS) SES() sesiface.SESAPI {
return a.ses
}

// S3 implements the golamb.AWSServiceProvider interface.
func (a *AWS) S3() s3iface.S3API {
return a.s3
}

// STS implements the golamb.AWSServiceProvider interface.
func (a *AWS) STS() stsiface.STSAPI {
return a.sts
}

// SSM implements the golamb.AWSServiceProvider interface.
func (a *AWS) SSM() ssmiface.SSMAPI {
return a.ssm
}

// WithDynamoDB sets the dynamodb client of the AWSServiceProvider.
func (a *AWS) WithDynamoDB(svc dynamodbiface.DynamoDBAPI) *AWS {
a.dynamodb = svc
return a
}

// WithSES sets the ses client of the AWSServiceProvider.
func (a *AWS) WithSES(svc sesiface.SESAPI) *AWS {
a.ses = svc
return a
}

// WithS3 sets the s3 client of the AWSServiceProvider.
func (a *AWS) WithS3(svc s3iface.S3API) *AWS {
a.s3 = svc
return a
}

// WithSTS sets the sts client of the AWSServiceProvider.
func (a *AWS) WithSTS(svc stsiface.STSAPI) *AWS {
a.sts = svc
return a
}

// WithSSM sets the ssm client of the AWSServiceProvider.
func (a *AWS) WithSSM(svc ssmiface.SSMAPI) *AWS {
a.ssm = svc
return a
Expand Down
40 changes: 20 additions & 20 deletions fakes/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
// your lambda handler functions.
//
// Example:
// package main
// package main
//
// func handler(c golamb.Context) golamb.Responder {
// foo := c.Request().Query("foo")
// body := map[string]string{"foo": foo}
// return c.Response(200, body)
// }
// func handler(c golamb.Context) golamb.Responder {
// foo := c.Request().Query("foo")
// body := map[string]string{"foo": foo}
// return c.Response(200, body)
// }
//
// func main() {
// golamb.Start(handler)
// }
// func main() {
// golamb.Start(handler)
// }
//
// func TestHandler(t *testing.T) {
// req := fakes.NewRequest().WithQuery(map[string]string{"foo": "bar"})
// ctx := fakes.NewContext().WithRequest(req)
// resp, err := handler(ctx).Respond()
// if err != nil {
// t.Fatalf("unexpected err: %s", err)
// }
// if resp.Body != `{"foo":"bar"}` {
// t.Fatalf("incorrect response: %v", resp)
// }
// }
// func TestHandler(t *testing.T) {
// req := fakes.NewRequest().WithQuery(map[string]string{"foo": "bar"})
// ctx := fakes.NewContext().WithRequest(req)
// resp, err := handler(ctx).Respond()
// if err != nil {
// t.Fatalf("unexpected err: %s", err)
// }
// if resp.Body != `{"foo":"bar"}` {
// t.Fatalf("incorrect response: %v", resp)
// }
// }
package fakes

import (
Expand Down
16 changes: 16 additions & 0 deletions fakes/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
)

// Request implements the golamb.Request interface.
type Request struct {
query map[string]string
path map[string]string
Expand All @@ -14,6 +15,8 @@ type Request struct {
rawPath string
}

// NewRequest creates a value that implements the golamb.Request
// interface.
func NewRequest() *Request {
return &Request{
query: make(map[string]string),
Expand All @@ -23,59 +26,72 @@ func NewRequest() *Request {
}
}

// Query implements the golamb.Request interface.
func (r *Request) Query(key string) string {
return r.query[key]
}

// Path implements the golamb.Request interface.
func (r *Request) Path(key string) string {
return r.path[key]
}

// Header implements the golamb.Request interface.
func (r *Request) Header(key string) string {
return r.header[key]
}

// Headers implements the golamb.Request interface.
func (r *Request) Headers() map[string]string {
return r.header
}

// Cookie implements the golamb.Request interface.
func (r *Request) Cookie(key string) *http.Cookie {
return r.cookie[key]
}

// RawPath implements the golamb.Request interface.
func (r *Request) RawPath() string {
return r.rawPath
}

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

// WithQuery sets the query parameters of the fake Request.
func (r *Request) WithQuery(query map[string]string) *Request {
r.query = query
return r
}

// WithPath sets the path parameters of the fake Request.
func (r *Request) WithPath(path map[string]string) *Request {
r.path = path
return r
}

// WithHeaders sets the headers of the fake Request.
func (r *Request) WithHeaders(header map[string]string) *Request {
r.header = header
return r
}

// WithCookies sets the cookies of the fake Request.
func (r *Request) WithCookies(cookie map[string]*http.Cookie) *Request {
r.cookie = cookie
return r
}

// WithRawPath sets the raw path of the fake Request.
func (r *Request) WithRawPath(rawPath string) *Request {
r.rawPath = rawPath
return r
}

// WithBody sets the body of the fake Request.
func (r *Request) WithBody(v interface{}) error {
var err error
r.body, err = json.Marshal(v)
Expand Down
6 changes: 6 additions & 0 deletions fakes/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"github.com/twharmon/golamb"
)

// Response implements the golamb.Responder interface.
type Response struct {
response *events.APIGatewayV2HTTPResponse
err error
}

// NewResponse creates a value that implements the golamb.Responder
// interface.
func NewResponse() *Response {
return &Response{
response: &events.APIGatewayV2HTTPResponse{
Expand All @@ -20,15 +23,18 @@ func NewResponse() *Response {
}
}

// Response implements the golamb.Responder interface.
func (r *Response) Respond() (*events.APIGatewayV2HTTPResponse, error) {
return r.response, r.err
}

// SetCookie implements the golamb.Responder interface.
func (r *Response) SetCookie(cookie *http.Cookie) golamb.Responder {
r.response.Cookies = append(r.response.Cookies, cookie.String())
return r
}

// SetHeader implements the golamb.Responder interface.
func (r *Response) SetHeader(key string, val string) golamb.Responder {
r.response.Headers[key] = val
return r
Expand Down

0 comments on commit 764f0d3

Please sign in to comment.