Skip to content

Commit

Permalink
New Case.ModifyCase method
Browse files Browse the repository at this point in the history
  • Loading branch information
yookoala committed Jun 2, 2024
1 parent e9de5af commit ed51ad5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions v2/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func (c *Case) AddQuery(key, value string) *Case {
return c
}

// ModifyCase allows user do whatever to the case (even
// rewrite a new one) without interrupting the chaining.
func (c *Case) ModifyCase(fn func(c *Case) *Case) *Case {
return fn(c)
}

// Expect appends an expectation to the Case
func (c *Case) Expect(exp Expectation) *Case {
c.Expectations = append(c.Expectations, exp)
Expand Down
24 changes: 24 additions & 0 deletions v2/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,30 @@ func TestCase_Describe(t *testing.T) {
}
}

func TestCase_ModifyCase(t *testing.T) {
var urlResult string

testVal := RandString(20)
r, err := http.NewRequest("GET", "http://example.com/foo/bar", nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

c := &restit.Case{
Request: r,
}
c.
AddQuery("hello", testVal).
ModifyCase(func(c *restit.Case) *restit.Case {
urlResult = c.Request.URL.String()
return c
})

if want, have := "http://example.com/foo/bar?hello="+testVal, urlResult; want != have {
t.Errorf("expected %#v, got %#v", want, have)
}
}

func TestCase_Expect(t *testing.T) {
c := &restit.Case{}
str := RandString(20)
Expand Down

0 comments on commit ed51ad5

Please sign in to comment.