From beb677da0edfb24b65cd46f1a241c9223876e179 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Tue, 15 Nov 2022 16:29:27 +0400 Subject: [PATCH] Fix cloning builders & matchers --- expect.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/expect.go b/expect.go index c1a2e0165..10741a477 100644 --- a/expect.go +++ b/expect.go @@ -340,6 +340,18 @@ func NewJar() http.CookieJar { return jar } +func (e *Expect) clone() *Expect { + ret := *e + + ret.builders = nil + ret.builders = append(ret.builders, e.builders...) + + ret.matchers = nil + ret.matchers = append(ret.matchers, e.matchers...) + + return &ret +} + // Builder returns a copy of Expect instance with given builder attached to it. // Returned copy contains all previously attached builders plus a new one. // Builders are invoked from Request method, after constructing every new request. @@ -359,13 +371,10 @@ func NewJar() http.CookieJar { // Expect(). // Status(http.StatusOK) func (e *Expect) Builder(builder func(*Request)) *Expect { - ret := *e + ret := e.clone() - ret.builders = nil - ret.builders = append(ret.builders, e.builders...) ret.builders = append(ret.builders, builder) - - return &ret + return ret } // Matcher returns a copy of Expect instance with given matcher attached to it. @@ -387,13 +396,10 @@ func (e *Expect) Builder(builder func(*Request)) *Expect { // Expect(). // Status(http.StatusNotFound) func (e *Expect) Matcher(matcher func(*Response)) *Expect { - ret := *e + ret := e.clone() - ret.matchers = nil - ret.matchers = append(ret.matchers, e.matchers...) ret.matchers = append(ret.matchers, matcher) - - return &ret + return ret } // Request returns a new Request object.