Skip to content

Commit

Permalink
change then to verify, and use free port
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michael committed Aug 24, 2017
1 parent 99921e8 commit 4fe7037
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ As requests come in, the will be stored


## Verifying
To verify the calls made against your go-rest-assured service, use the endpoint `/then/{path:.*}`
To verify the calls made against your go-rest-assured service, use the endpoint `/verify/{path:.*}`

This endpoint returns a list of assured calls made against the matching Method/Path

Expand Down
11 changes: 6 additions & 5 deletions assured/assured_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assured
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
Expand All @@ -14,13 +15,13 @@ import (
)

// StartApplicationHTTPListener creates a Go-routine that has an HTTP listener for the application endpoints
func StartApplicationHTTPListener(logger kitlog.Logger, root context.Context, errc chan error) {
func StartApplicationHTTPListener(port int, logger kitlog.Logger, root context.Context, errc chan error) {
go func() {
ctx, cancel := context.WithCancel(root)
defer cancel()
router := createApplicationRouter(ctx, logger)
logger.Log("message", "starting go rest assured on port 11011")
errc <- http.ListenAndServe(":11011", handlers.RecoveryHandler()(router))
logger.Log("message", fmt.Sprintf("starting go rest assured on port %d", port))
errc <- http.ListenAndServe(fmt.Sprintf(":%d", port), handlers.RecoveryHandler()(router))
}()
}

Expand Down Expand Up @@ -62,9 +63,9 @@ func createApplicationRouter(ctx context.Context, logger kitlog.Logger) *mux.Rou
).Methods(assuredMethods...)

router.Handle(
"/then/{path:.*}",
"/verify/{path:.*}",
kithttp.NewServer(
e.WrappedEndpoint(e.ThenEndpoint),
e.WrappedEndpoint(e.VerifyEndpoint),
decodeAssuredCall,
encodeJSONResponse,
kithttp.ServerErrorLogger(logger),
Expand Down
8 changes: 4 additions & 4 deletions assured/assured_bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestApplicationRouterWhenBinding(t *testing.T) {
}
}

func TestApplicationRouterThenBinding(t *testing.T) {
func TestApplicationRouterVerifyBinding(t *testing.T) {
router := createApplicationRouter(ctx, kitlog.NewLogfmtLogger(ioutil.Discard))

for _, verb := range verbs {
req, err := http.NewRequest(verb, "/then/rest/assured", nil)
req, err := http.NewRequest(verb, "/verify/rest/assured", nil)
require.NoError(t, err)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
Expand Down Expand Up @@ -107,11 +107,11 @@ func TestDecodeAssuredCall(t *testing.T) {
decoded = true
}

req, err := http.NewRequest(http.MethodPost, "/then/test/assured?test=positive", bytes.NewBuffer([]byte(`{"assured": true}`)))
req, err := http.NewRequest(http.MethodPost, "/verify/test/assured?test=positive", bytes.NewBuffer([]byte(`{"assured": true}`)))
require.NoError(t, err)

router := mux.NewRouter()
router.HandleFunc("/then/{path:.*}", testDecode).Methods(http.MethodPost)
router.HandleFunc("/verify/{path:.*}", testDecode).Methods(http.MethodPost)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)

Expand Down
4 changes: 2 additions & 2 deletions assured/assured_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (a *AssuredEndpoints) WhenEndpoint(ctx context.Context, call *Call) (interf
return assured, nil
}

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

Expand Down
6 changes: 3 additions & 3 deletions assured/assured_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ func TestWhenEndpointNotFound(t *testing.T) {
require.Equal(t, "No assured calls", err.Error())
}

func TestThenEndpointSuccess(t *testing.T) {
func TestVerifyEndpointSuccess(t *testing.T) {
endpoints := &AssuredEndpoints{
madeCalls: fullAssuredCalls,
}

c, err := endpoints.ThenEndpoint(ctx, call1)
c, err := endpoints.VerifyEndpoint(ctx, call1)

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

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

require.NoError(t, err)
require.Equal(t, []*Call{call3}, c)
Expand Down
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import:
subpackages:
- log
- transport/http
- endpoint
- endpoint
- package: github.com/phayes/freeport
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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

func main() {
Expand All @@ -20,7 +21,8 @@ func main() {
errc <- interrupt()
}()

assured.StartApplicationHTTPListener(logger, rootCtx, errc)
port := freeport.GetPort()
assured.StartApplicationHTTPListener(port, logger, rootCtx, errc)

logger.Log("fatal", <-errc)
}
Expand Down

0 comments on commit 4fe7037

Please sign in to comment.