Skip to content

Commit

Permalink
move to one package
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michael committed Aug 23, 2017
1 parent 9c2d0ce commit 15b8587
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 82 deletions.
22 changes: 2 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,22 @@ ifdef CIRCLE_ARTIFACTS
COVERAGEDIR=$(CIRCLE_ARTIFACTS)/coverage
endif

ALL_PACKAGES = \
bindings \
endpoints \
assured \

LDFLAGS = -ldflags '-X main.gitSHA=$(shell git rev-parse HEAD)'

MOCKERY = go run ./vendor/github.com/vektra/mockery/cmd/mockery/mockery.go

all: build test cover
install-deps:
glide install
gen-mocks:
for dir in $(MOCK_PACKAGES); do \
echo "Generating Mocks under $$dir"; \
$(MOCKERY) -dir=./$$dir/ -all -note "*DO NOT EDIT* Generated via mockery"; \
done
rm -rf ./mocks/error.go
build:
if [ ! -d bin ]; then mkdir bin; fi
$(GO) build $(LDFLAGS) -v -o bin/go-rest-assured
fmt:
find . -not -path "./vendor/*" -name '*.go' -type f | sed 's#\(.*\)/.*#\1#' | sort -u | xargs -n1 -I {} bash -c "cd {} && goimports -w *.go && gofmt -w -l -s *.go"
test:
if [ ! -d $(COVERAGEDIR) ]; then mkdir $(COVERAGEDIR); fi
for dir in $(ALL_PACKAGES); do \
$(GO) test -v ./$$dir -race -cover -coverprofile=$(COVERAGEDIR)/$$dir.coverprofile; \
done
$(GO) test -v ./assured -race -cover -coverprofile=$(COVERAGEDIR)/assured.coverprofile
cover:
if [ ! -d $(COVERAGEDIR) ]; then mkdir $(COVERAGEDIR); fi
for dir in $(ALL_PACKAGES); do \
coverfile=$$(echo $$dir | awk '{gsub(/\//, "."); print}') && \
$(GO) tool cover -html=$(COVERAGEDIR)/$${coverfile}.coverprofile -o $(COVERAGEDIR)/$${coverfile}.html; \
done
$(GO) tool cover -html=$(COVERAGEDIR)/assured.coverprofile -o $(COVERAGEDIR)/assured.html
assert-no-diff:
test -z "$(shell git status --porcelain)"
clean:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The HTTP Method you use will be stored in the Assured Call

The Request Body, if present, will be stored in the Assured Call

The stored Status Code will be `200 OK` unless you specify a `"Assured-Status": "[0-9]+"`
The stored Status Code will be `200 OK` unless you specify a `"Assured-Status": "[0-9]+"` HTTP Header

Or..

Expand Down Expand Up @@ -86,10 +86,10 @@ To clear out all stubbed calls on the server, use the endpoint `/clear`
Or..

``` go
// Clear calls for a Method and Path
// Clears calls for a Method and Path
client.Clear("GET", "test/assured")

// Crear all calls
// Crears all calls
client.ClearAll()
```

10 changes: 4 additions & 6 deletions bindings/assured_bindings.go → assured/assured_bindings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bindings
package assured

import (
"context"
Expand All @@ -11,8 +11,6 @@ import (
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jesse0michael/go-rest-assured/assured"
"github.com/jesse0michael/go-rest-assured/endpoints"
)

// StartApplicationHTTPListener creates a Go-routine that has an HTTP listener for the application endpoints
Expand All @@ -29,7 +27,7 @@ func StartApplicationHTTPListener(logger kitlog.Logger, root context.Context, er
// createApplicationRouter sets up the router that will handle all of the application routes
func createApplicationRouter(ctx context.Context, logger kitlog.Logger) *mux.Router {
router := mux.NewRouter()
e := endpoints.NewAssuredEndpoints(logger)
e := NewAssuredEndpoints(logger)
assuredMethods := []string{
http.MethodGet,
http.MethodHead,
Expand Down Expand Up @@ -102,7 +100,7 @@ func createApplicationRouter(ctx context.Context, logger kitlog.Logger) *mux.Rou
// decodeAssuredCall converts an http request into an assured Call object
func decodeAssuredCall(ctx context.Context, req *http.Request) (interface{}, error) {
urlParams := mux.Vars(req)
ac := assured.Call{
ac := Call{
Path: urlParams["path"],
Method: req.Method,
StatusCode: http.StatusOK,
Expand All @@ -123,7 +121,7 @@ func decodeAssuredCall(ctx context.Context, req *http.Request) (interface{}, err

// encodeAssuredCall writes the assured Call to the http response as it is intended to be stubbed
func encodeAssuredCall(ctx context.Context, w http.ResponseWriter, i interface{}) error {
if call, ok := i.(*assured.Call); ok {
if call, ok := i.(*Call); ok {
w.WriteHeader(call.StatusCode)
w.Write([]byte(call.String()))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bindings
package assured

import (
"bytes"
Expand All @@ -10,7 +10,6 @@ import (

kitlog "github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/jesse0michael/go-rest-assured/assured"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -94,7 +93,7 @@ func TestApplicationRouterFailure(t *testing.T) {

func TestDecodeAssuredCall(t *testing.T) {
decoded := false
expected := &assured.Call{
expected := &Call{
Path: "test/assured",
StatusCode: http.StatusOK,
Method: http.MethodPost,
Expand All @@ -121,7 +120,7 @@ func TestDecodeAssuredCall(t *testing.T) {

func TestDecodeAssuredCallNilBody(t *testing.T) {
decoded := false
expected := &assured.Call{
expected := &Call{
Path: "test/assured",
StatusCode: http.StatusOK,
Method: http.MethodDelete,
Expand All @@ -147,7 +146,7 @@ func TestDecodeAssuredCallNilBody(t *testing.T) {

func TestDecodeAssuredCallStatus(t *testing.T) {
decoded := false
expected := &assured.Call{
expected := &Call{
Path: "test/assured",
StatusCode: http.StatusForbidden,
Method: http.MethodGet,
Expand All @@ -174,7 +173,7 @@ func TestDecodeAssuredCallStatus(t *testing.T) {

func TestDecodeAssuredCallStatusFailure(t *testing.T) {
decoded := false
expected := &assured.Call{
expected := &Call{
Path: "test/assured",
StatusCode: http.StatusOK,
Method: http.MethodGet,
Expand All @@ -200,7 +199,7 @@ func TestDecodeAssuredCallStatusFailure(t *testing.T) {
}

func TestEncodeAssuredCall(t *testing.T) {
call := &assured.Call{
call := &Call{
Path: "/test/assured",
StatusCode: http.StatusCreated,
Method: http.MethodPost,
Expand Down
27 changes: 13 additions & 14 deletions endpoints/assured_endpoints.go → assured/assured_endpoints.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
package endpoints
package assured

import (
"context"
"errors"

"github.com/go-kit/kit/endpoint"
kitlog "github.com/go-kit/kit/log"
"github.com/jesse0michael/go-rest-assured/assured"
)

// AssuredEndpoints
type AssuredEndpoints struct {
logger kitlog.Logger
assuredCalls map[string][]*assured.Call
madeCalls map[string][]*assured.Call
assuredCalls map[string][]*Call
madeCalls map[string][]*Call
}

// NewAssuredEndpoints creates a new instance of assured endpoints
func NewAssuredEndpoints(l kitlog.Logger) *AssuredEndpoints {
return &AssuredEndpoints{
logger: l,
assuredCalls: map[string][]*assured.Call{},
madeCalls: map[string][]*assured.Call{},
assuredCalls: map[string][]*Call{},
madeCalls: map[string][]*Call{},
}
}

// WrappedEndpoint is used to validate that the incoming request is an assured call
func (a *AssuredEndpoints) WrappedEndpoint(handler func(context.Context, *assured.Call) (interface{}, error)) endpoint.Endpoint {
func (a *AssuredEndpoints) WrappedEndpoint(handler func(context.Context, *Call) (interface{}, error)) endpoint.Endpoint {
return func(ctx context.Context, i interface{}) (response interface{}, err error) {
a, ok := i.(*assured.Call)
a, ok := i.(*Call)
if !ok {
return nil, errors.New("unable to convert request to assured Call")
}
Expand All @@ -37,14 +36,14 @@ func (a *AssuredEndpoints) WrappedEndpoint(handler func(context.Context, *assure
}

// GivenEndpoint is used to stub out a call for a given path
func (a *AssuredEndpoints) GivenEndpoint(ctx context.Context, call *assured.Call) (interface{}, error) {
func (a *AssuredEndpoints) GivenEndpoint(ctx context.Context, call *Call) (interface{}, error) {
a.assuredCalls[call.ID()] = append(a.assuredCalls[call.ID()], call)
a.logger.Log("message", "assured call set", "path", call.ID())
return call, nil
}

// WhenEndpoint is used to test the assured calls
func (a *AssuredEndpoints) WhenEndpoint(ctx context.Context, call *assured.Call) (interface{}, error) {
func (a *AssuredEndpoints) WhenEndpoint(ctx context.Context, call *Call) (interface{}, error) {
if a.assuredCalls[call.ID()] == nil || len(a.assuredCalls[call.ID()]) == 0 {
a.logger.Log("message", "assured call not found", "path", call.ID())
return nil, errors.New("No assured calls")
Expand All @@ -60,12 +59,12 @@ func (a *AssuredEndpoints) WhenEndpoint(ctx context.Context, call *assured.Call)
}

// ThenEndpoint is used to verify a particular call
func (a *AssuredEndpoints) ThenEndpoint(ctx context.Context, call *assured.Call) (interface{}, error) {
func (a *AssuredEndpoints) ThenEndpoint(ctx context.Context, call *Call) (interface{}, error) {
return a.madeCalls[call.ID()], nil
}

//ClearEndpoint is used to clear a specific assured call
func (a *AssuredEndpoints) ClearEndpoint(ctx context.Context, call *assured.Call) (interface{}, error) {
func (a *AssuredEndpoints) ClearEndpoint(ctx context.Context, call *Call) (interface{}, error) {
delete(a.assuredCalls, call.ID())
delete(a.madeCalls, call.ID())
a.logger.Log("message", "cleared calls for path", "path", call.ID())
Expand All @@ -75,8 +74,8 @@ func (a *AssuredEndpoints) ClearEndpoint(ctx context.Context, call *assured.Call

//ClearAllEndpoint is used to clear all assured calls
func (a *AssuredEndpoints) ClearAllEndpoint(ctx context.Context, i interface{}) (interface{}, error) {
a.assuredCalls = map[string][]*assured.Call{}
a.madeCalls = map[string][]*assured.Call{}
a.assuredCalls = map[string][]*Call{}
a.madeCalls = map[string][]*Call{}
a.logger.Log("message", "cleared all calls")

return nil, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package endpoints
package assured

import (
"context"
Expand All @@ -7,16 +7,15 @@ import (
"testing"

kitlog "github.com/go-kit/kit/log"
"github.com/jesse0michael/go-rest-assured/assured"
"github.com/stretchr/testify/require"
)

func TestNewAssuredEndpoints(t *testing.T) {
logger := kitlog.NewLogfmtLogger(ioutil.Discard)
expected := &AssuredEndpoints{
logger: logger,
assuredCalls: map[string][]*assured.Call{},
madeCalls: map[string][]*assured.Call{},
assuredCalls: map[string][]*Call{},
madeCalls: map[string][]*Call{},
}
actual := NewAssuredEndpoints(logger)

Expand All @@ -25,7 +24,7 @@ func TestNewAssuredEndpoints(t *testing.T) {

func TestWrappedEndpointSuccess(t *testing.T) {
endpoints := NewAssuredEndpoints(kitlog.NewLogfmtLogger(ioutil.Discard))
testEndpoint := func(ctx context.Context, call *assured.Call) (interface{}, error) {
testEndpoint := func(ctx context.Context, call *Call) (interface{}, error) {
return call, nil
}

Expand All @@ -38,7 +37,7 @@ func TestWrappedEndpointSuccess(t *testing.T) {

func TestWrappedEndpointFailure(t *testing.T) {
endpoints := NewAssuredEndpoints(kitlog.NewLogfmtLogger(ioutil.Discard))
testEndpoint := func(ctx context.Context, call *assured.Call) (interface{}, error) {
testEndpoint := func(ctx context.Context, call *Call) (interface{}, error) {
return call, nil
}

Expand Down Expand Up @@ -74,11 +73,11 @@ func TestGivenEndpointSuccess(t *testing.T) {
func TestWhenEndpointSuccess(t *testing.T) {
endpoints := &AssuredEndpoints{
assuredCalls: fullAssuredCalls,
madeCalls: map[string][]*assured.Call{},
madeCalls: map[string][]*Call{},
}
expected := map[string][]*assured.Call{
"GET:test/assured": []*assured.Call{call2, call1},
":teapot/assured": []*assured.Call{call3},
expected := map[string][]*Call{
"GET:test/assured": []*Call{call2, call1},
":teapot/assured": []*Call{call3},
}

c, err := endpoints.WhenEndpoint(ctx, call1)
Expand Down Expand Up @@ -119,12 +118,12 @@ func TestThenEndpointSuccess(t *testing.T) {
c, err := endpoints.ThenEndpoint(ctx, call1)

require.NoError(t, err)
require.Equal(t, []*assured.Call{call1, call2}, c)
require.Equal(t, []*Call{call1, call2}, c)

c, err = endpoints.ThenEndpoint(ctx, call3)

require.NoError(t, err)
require.Equal(t, []*assured.Call{call3}, c)
require.Equal(t, []*Call{call3}, c)
}

func TestClearEndpointSuccess(t *testing.T) {
Expand All @@ -133,8 +132,8 @@ func TestClearEndpointSuccess(t *testing.T) {
assuredCalls: fullAssuredCalls,
madeCalls: fullAssuredCalls,
}
expected := map[string][]*assured.Call{
":teapot/assured": []*assured.Call{call3},
expected := map[string][]*Call{
":teapot/assured": []*Call{call3},
}

c, err := endpoints.ClearEndpoint(ctx, call1)
Expand All @@ -155,8 +154,8 @@ func TestClearEndpointSuccess(t *testing.T) {

require.NoError(t, err)
require.Nil(t, c)
require.Equal(t, map[string][]*assured.Call{}, endpoints.assuredCalls)
require.Equal(t, map[string][]*assured.Call{}, endpoints.madeCalls)
require.Equal(t, map[string][]*Call{}, endpoints.assuredCalls)
require.Equal(t, map[string][]*Call{}, endpoints.madeCalls)
}

func TestClearAllEndpointSuccess(t *testing.T) {
Expand All @@ -170,30 +169,29 @@ func TestClearAllEndpointSuccess(t *testing.T) {

require.NoError(t, err)
require.Nil(t, c)
require.Equal(t, map[string][]*assured.Call{}, endpoints.assuredCalls)
require.Equal(t, map[string][]*assured.Call{}, endpoints.madeCalls)
require.Equal(t, map[string][]*Call{}, endpoints.assuredCalls)
require.Equal(t, map[string][]*Call{}, endpoints.madeCalls)
}

var (
ctx = context.Background()
call1 = &assured.Call{
call1 = &Call{
Path: "test/assured",
Method: "GET",
StatusCode: http.StatusOK,
Response: []byte(`{"assured": true}`),
}
call2 = &assured.Call{
call2 = &Call{
Path: "test/assured",
Method: "GET",
StatusCode: http.StatusConflict,
Response: []byte("error"),
}
call3 = &assured.Call{
call3 = &Call{
Path: "teapot/assured",
StatusCode: http.StatusTeapot,
}
fullAssuredCalls = map[string][]*assured.Call{
"GET:test/assured": []*assured.Call{call1, call2},
":teapot/assured": []*assured.Call{call3},
fullAssuredCalls = map[string][]*Call{
"GET:test/assured": []*Call{call1, call2},
":teapot/assured": []*Call{call3},
}
)
Loading

0 comments on commit 15b8587

Please sign in to comment.