From d82cc3d8d80430421c2b8d805f3727aadf0c5204 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Sun, 2 Jun 2024 12:58:54 +0800 Subject: [PATCH] New Case.ModifyCase method --- v2/case.go | 6 ++++++ v2/case_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/v2/case.go b/v2/case.go index 950d25f..c235133 100644 --- a/v2/case.go +++ b/v2/case.go @@ -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) diff --git a/v2/case_test.go b/v2/case_test.go index 42ff871..1c000ed 100644 --- a/v2/case_test.go +++ b/v2/case_test.go @@ -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)