Skip to content

Commit

Permalink
Interface change: Added Response.Reset()
Browse files Browse the repository at this point in the history
The `Response` interface now requires an additional `Reset` method.
`Run` and `RunOrPanic` will trigger this `Reset` method at the
beginning.

This will fix bug #2.
  • Loading branch information
yookoala committed Aug 16, 2014
1 parent b44c66a commit 5de6eac
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
14 changes: 12 additions & 2 deletions case.go
Expand Up @@ -21,9 +21,9 @@ package restit
import (
"fmt"
"github.com/jmcvetta/napping"
"net/http"
"path"
"runtime"
"net/http"
)

type Case struct {
Expand All @@ -47,7 +47,8 @@ func (c *Case) InitForRun() *Case {
// load default logging behaviour
c.Tester.LogDefault()

// there must be a request
// c.Request must be a valid napping.Request
// this will be sent through napping.Send
if c.Request == nil {
c.Request = new(napping.Request)
}
Expand All @@ -58,6 +59,15 @@ func (c *Case) InitForRun() *Case {
c.Request.Result = new(nilResp)
}

// trigger reset
r, ok := c.Request.Result.(Response)
if !ok {
panic(fmt.Errorf(
"The provided response %T does not implement restit.Response",
r))
}
r.Reset()

return c
}

Expand Down
52 changes: 52 additions & 0 deletions case_test.go
Expand Up @@ -33,6 +33,58 @@ func Test_Case_WithResponseAs_nil(t *testing.T) {

}

func Test_Case_WithResponseAs_invalid(t *testing.T) {

pass := false

defer func() {
if r := recover(); r != nil {
t.Logf("Recovered in f: %#v", r)
pass = true
t.Logf("Invalid response type triggers error")
}
}()

resp := "some invalid response"
r := napping.Request{}
r.Result = &resp
c := Case{
Request: &r,
Session: new(dummyNilSession),
}
c.RunOrPanic()

if pass != true {
t.Errorf("Invalid response type does not raise error")
}

}

func Test_Case_WithResponseAs_reset(t *testing.T) {

// test reset with filled response
r := dummyResponse{
Dummies: []dummy{
dummy{},
dummy{},
dummy{},
},
}

c := Case{
Request: &napping.Request{},
Session: new(dummyNilSession),
}
t.Logf("resp: %#v", r)
c.WithResponseAs(&r)
c.Run()

if len(r.Dummies) != 0 {
t.Errorf("Case.Run() fails to trigger Response.Reset")
}

}

func Test_Case_AddHeader(t *testing.T) {

r := napping.Request{}
Expand Down
1 change: 0 additions & 1 deletion response.go
Expand Up @@ -40,7 +40,6 @@ type Response interface {

// reset the result, prevent the problem of null result
Reset()

}

// The default response type
Expand Down

0 comments on commit 5de6eac

Please sign in to comment.