Skip to content
Permalink
Browse files

return result at end of test to support multiple executions per test

This is useful if you want to run multiple operations against an api in
a given test, e.g. for an api that uses a database you might want to
ensure multiple requests maintain database consistency
  • Loading branch information...
steinfletcher committed Apr 22, 2019
1 parent bbb45b4 commit 97ea6a2f022b9da75ed31ad15568a0c72bb0b1ba
Showing with 44 additions and 10 deletions.
  1. +19 −10 apitest.go
  2. +25 −0 apitest_test.go
@@ -395,13 +395,18 @@ func (r *Response) Assert(fn func(*http.Response, *http.Request) error) *Respons
return r.apiTest.response
}

// End runs the test and all defined assertions
func (r *Response) End() {
// End runs the test returning the result to the caller
func (r *Response) End() Result {
if r.apiTest.reporter != nil {
r.apiTest.report()
return
res := r.apiTest.report()
return Result{Response: res}
}
r.execute()
res := r.execute()
return Result{Response: res}
}

type Result struct {
Response *http.Response
}

type mockInteraction struct {
@@ -418,7 +423,7 @@ func (r *mockInteraction) GetRequestHost() string {
return host
}

func (a *APITest) report() {
func (a *APITest) report() *http.Response {
var capturedInboundReq *http.Request
var capturedFinalRes *http.Response
var capturedMockInteractions []*mockInteraction
@@ -446,7 +451,7 @@ func (a *APITest) report() {
}

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

a.recorder.
@@ -502,6 +507,8 @@ func (a *APITest) report() {
a.recorder.AddMeta(meta)

a.reporter.Format(a.recorder)

return res
}

func createHash(meta map[string]interface{}) string {
@@ -524,7 +531,7 @@ func createHash(meta map[string]interface{}) string {
return fmt.Sprintf("%d_%d", prefix.Sum32(), suffix.Sum32())
}

func (r *Response) execute() {
func (r *Response) execute() *http.Response {
apiTest := r.apiTest
if len(apiTest.mocks) > 0 {
apiTest.transport = newTransport(
@@ -537,10 +544,10 @@ func (r *Response) execute() {
defer apiTest.transport.Reset()
apiTest.transport.Hijack()
}
apiTest.run()
return apiTest.run()
}

func (a *APITest) run() {
func (a *APITest) run() *http.Response {
res, req := a.runTest()

defer func() {
@@ -558,6 +565,8 @@ func (a *APITest) run() {
if err != nil {
a.t.Fatal(err.Error())
}

return copyHttpResponse(res.Result())
}

func (a *APITest) assertFunc(res *httptest.ResponseRecorder, req *http.Request) error {
@@ -425,6 +425,31 @@ func TestApiTest_MatchesResponseHeaders_WithMixedKeyCase(t *testing.T) {
End()
}

func TestApiTest_EndReturnsTheResult(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"a": 12345, "b": "hi"}`))
if err != nil {
panic(err)
}
})

res := New().
Handler(handler).
Get("/hello").
Expect(t).
Body(`{
"a": 12345,
"b": "hi"
}`).
Status(http.StatusCreated).
End()

assert.Equal(t, http.StatusCreated, res.Response.StatusCode)
}

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

0 comments on commit 97ea6a2

Please sign in to comment.
You can’t perform that action at this time.