Skip to content

Commit

Permalink
Merge pull request #38 from enrico5b1b4/feature/set_recorder
Browse files Browse the repository at this point in the history
Add method to add an existing Recorder to a test
  • Loading branch information
steinfletcher committed Apr 21, 2019
2 parents 83f5a25 + d0d1f68 commit bf0de5c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
31 changes: 21 additions & 10 deletions apitest.go
Expand Up @@ -31,6 +31,7 @@ var responseDebugPrefix = fmt.Sprintf("<%s", divider)
type APITest struct {
debugEnabled bool
reporter ReportFormatter
recorder *Recorder
handler http.Handler
name string
request *Request
Expand Down Expand Up @@ -97,6 +98,12 @@ func (a *APITest) Report(reporter ReportFormatter) *APITest {
return a
}

// Recorder provides a hook to add a recorder to the test
func (a *APITest) Recorder(recorder *Recorder) *APITest {
a.recorder = recorder
return a
}

// Meta provides a hook to add custom meta data to the test which can be picked up when defining a custom reporter
func (a *APITest) Meta(meta map[string]interface{}) *APITest {
a.meta = meta
Expand Down Expand Up @@ -143,6 +150,7 @@ func (a *APITest) ObserveMocks(observer Observe) *APITest {

// RecorderHook allows the consumer to provider a function that will receive the recorder instance before the
// test runs. This can be used to inject custom events which can then be rendered in diagrams
// Deprecated: use Recorder() instead
func (a *APITest) RecorderHook(hook RecorderHook) *APITest {
a.recorderHook = hook
return a
Expand Down Expand Up @@ -421,16 +429,19 @@ func (a *APITest) report() {
})
}

recorder := NewTestRecorder()
if a.recorder == nil {
a.recorder = NewTestRecorder()
}

if a.recorderHook != nil {
a.recorderHook(recorder)
a.recorderHook(a.recorder)
}

execTime := time.Now().UTC()
a.response.execute()
finishTime := time.Now().UTC()

recorder.
a.recorder.
AddTitle(fmt.Sprintf("%s %s", capturedInboundReq.Method, capturedInboundReq.URL.String())).
AddSubTitle(a.name).
AddHttpRequest(HttpRequest{
Expand All @@ -441,14 +452,14 @@ func (a *APITest) report() {
})

for _, interaction := range capturedMockInteractions {
recorder.AddHttpRequest(HttpRequest{
a.recorder.AddHttpRequest(HttpRequest{
Source: quoted(systemUnderTestDefaultName),
Target: quoted(interaction.request.Host),
Value: interaction.request,
Timestamp: interaction.timestamp,
})
if interaction.response != nil {
recorder.AddHttpResponse(HttpResponse{
a.recorder.AddHttpResponse(HttpResponse{
Source: quoted(interaction.request.Host),
Target: quoted(systemUnderTestDefaultName),
Value: interaction.response,
Expand All @@ -457,15 +468,15 @@ func (a *APITest) report() {
}
}

recorder.AddHttpResponse(HttpResponse{
a.recorder.AddHttpResponse(HttpResponse{
Source: quoted(systemUnderTestDefaultName),
Target: quoted(consumerName),
Value: capturedFinalRes,
Timestamp: finishTime,
})

sort.Slice(recorder.Events, func(i, j int) bool {
return recorder.Events[i].GetTime().Before(recorder.Events[j].GetTime())
sort.Slice(a.recorder.Events, func(i, j int) bool {
return a.recorder.Events[i].GetTime().Before(a.recorder.Events[j].GetTime())
})

meta := map[string]interface{}{}
Expand All @@ -480,9 +491,9 @@ func (a *APITest) report() {
meta["name"] = a.name
meta["hash"] = createHash(meta)

recorder.AddMeta(meta)
a.recorder.AddMeta(meta)

a.reporter.Format(recorder)
a.reporter.Format(a.recorder)
}

func createHash(meta map[string]interface{}) string {
Expand Down
54 changes: 53 additions & 1 deletion apitest_test.go
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestApiTest_AddsJSONBodyToRequest(t *testing.T) {
Expand Down Expand Up @@ -510,6 +511,57 @@ func TestApiTest_Report(t *testing.T) {
assert.Equal(t, "abc.com", r.Meta["host"])
}

func TestApiTest_Recorder(t *testing.T) {
getUser := NewMock().
Get("http://localhost:8080").
RespondWith().
Status(http.StatusOK).
Body("1").
Times(2).
End()

reporter := &RecorderCaptor{}
messageRequest := MessageRequest{
Source: "Source",
Target: "Target",
Header: "Header",
Body: "Body",
Timestamp: time.Now().UTC(),
}
messageResponse := MessageResponse{
Source: "Source",
Target: "Target",
Header: "Header",
Body: "Body",
Timestamp: time.Now().UTC(),
}
recorder := NewTestRecorder()
recorder.AddMessageRequest(messageRequest)
recorder.AddMessageResponse(messageResponse)

New("some test").
Meta(map[string]interface{}{"host": "abc.com"}).
Report(reporter).
Recorder(recorder).
Mocks(getUser).
Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
getUserData()
w.WriteHeader(http.StatusOK)
})).
Post("/hello").
Body(`{"a": 12345}`).
Headers(map[string]string{"Content-Type": "application/json"}).
Expect(t).
Status(http.StatusOK).
End()

r := reporter.capturedRecorder
assert.Len(t, r.Events, 6)
assert.Equal(t, messageRequest, r.Events[0])
assert.Equal(t, messageResponse, r.Events[1])

}

func TestApiTest_Observe(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit bf0de5c

Please sign in to comment.