diff --git a/README.md b/README.md index 5b25025..0788f6b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/assured/assured_bindings.go b/assured/assured_bindings.go index a39db46..5917a9b 100644 --- a/assured/assured_bindings.go +++ b/assured/assured_bindings.go @@ -3,6 +3,7 @@ package assured import ( "context" "encoding/json" + "fmt" "io/ioutil" "net/http" "strconv" @@ -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)) }() } @@ -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), diff --git a/assured/assured_bindings_test.go b/assured/assured_bindings_test.go index c2813fb..c12cca4 100644 --- a/assured/assured_bindings_test.go +++ b/assured/assured_bindings_test.go @@ -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) @@ -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) diff --git a/assured/assured_endpoints.go b/assured/assured_endpoints.go index dfb792f..dce296f 100644 --- a/assured/assured_endpoints.go +++ b/assured/assured_endpoints.go @@ -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 } diff --git a/assured/assured_endpoints_test.go b/assured/assured_endpoints_test.go index 09584d7..8142f43 100644 --- a/assured/assured_endpoints_test.go +++ b/assured/assured_endpoints_test.go @@ -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) diff --git a/glide.lock b/glide.lock index e9c8804..e59fc0d 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: b8d28b462db121c1427751ec35a0bb47eb50703d922892a6c2d8497e68ecc047 -updated: 2017-08-23T09:29:41.973922387-07:00 +hash: ecfa2d2574c5121c8a1148111bfd67ecf5a28ff395450c2e6c390f2394a21235 +updated: 2017-08-23T20:53:22.895944611-07:00 imports: - name: github.com/go-kit/kit version: f01651a61ac759c6b508539bddda24f63ea34bbb @@ -19,6 +19,8 @@ imports: version: ac112f7d75a0714af1bd86ab17749b31f7809640 - name: github.com/kr/logfmt version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0 +- name: github.com/phayes/freeport + version: e7681b37614941bf73b404e0caa37f19e33b5fed - name: github.com/stretchr/testify version: 890a5c3458b43e6104ff5da8dfa139d013d77544 subpackages: diff --git a/glide.yaml b/glide.yaml index cedd4a9..272e741 100644 --- a/glide.yaml +++ b/glide.yaml @@ -10,4 +10,5 @@ import: subpackages: - log - transport/http - - endpoint \ No newline at end of file + - endpoint +- package: github.com/phayes/freeport diff --git a/main.go b/main.go index 73d247e..99badb3 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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) }