Skip to content

Commit

Permalink
Rename Cookie methods
Browse files Browse the repository at this point in the history
- HasMaxAge() => ContainsMaxAge()
- NotHasMaxAge() => NotContainsMaxAge()
  • Loading branch information
gavv committed Oct 5, 2023
1 parent c038021 commit 346651b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
36 changes: 23 additions & 13 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,17 @@ func (c *Cookie) Expires() *DateTime {
return newDateTime(opChain, c.value.Expires)
}

// HasMaxAge succeeds if cookie has Max-Age field.
// ContainsMaxAge succeeds if cookie has Max-Age field.
//
// In particular, if Max-Age is present and is zero (which means delete
// cookie now), method succeeds.
//
// Example:
//
// cookie := NewCookie(t, &http.Cookie{...})
// cookie.HasMaxAge()
func (c *Cookie) HasMaxAge() *Cookie {
opChain := c.chain.enter("HasMaxAge()")
// cookie.ContainsMaxAge()
func (c *Cookie) ContainsMaxAge() *Cookie {
opChain := c.chain.enter("ContainsMaxAge()")
defer opChain.leave()

if opChain.failed() {
Expand All @@ -195,17 +195,17 @@ func (c *Cookie) HasMaxAge() *Cookie {
return c
}

// NotHasMaxAge succeeds if cookie does not have Max-Age field.
// NotContainsMaxAge succeeds if cookie does not have Max-Age field.
//
// In particular, if Max-Age is present and is zero (which means delete
// cookie now), method fails.
//
// Example:
//
// cookie := NewCookie(t, &http.Cookie{...})
// cookie.NotHasMaxAge()
func (c *Cookie) NotHasMaxAge() *Cookie {
opChain := c.chain.enter("NotHasMaxAge()")
// cookie.NotContainsMaxAge()
func (c *Cookie) NotContainsMaxAge() *Cookie {
opChain := c.chain.enter("NotContainsMaxAge()")
defer opChain.leave()

if opChain.failed() {
Expand All @@ -225,14 +225,24 @@ func (c *Cookie) NotHasMaxAge() *Cookie {
return c
}

// Deprecated: use HasMaxAge instead.
// Deprecated: use ContainsMaxAge instead.
func (c *Cookie) HasMaxAge() *Cookie {
return c.ContainsMaxAge()
}

// Deprecated: use NotContainsMaxAge instead.
func (c *Cookie) NotHasMaxAge() *Cookie {
return c.NotContainsMaxAge()
}

// Deprecated: use ContainsMaxAge instead.
func (c *Cookie) HaveMaxAge() *Cookie {
return c.HasMaxAge()
return c.ContainsMaxAge()
}

// Deprecated: use NotHasMaxAge instead.
// Deprecated: use NotContainsMaxAge instead.
func (c *Cookie) NotHaveMaxAge() *Cookie {
return c.NotHasMaxAge()
return c.NotContainsMaxAge()
}

// MaxAge returns a new Duration instance with cookie Max-Age field.
Expand All @@ -245,7 +255,7 @@ func (c *Cookie) NotHaveMaxAge() *Cookie {
// Example:
//
// cookie := NewCookie(t, &http.Cookie{...})
// cookie.HasMaxAge()
// cookie.ContainsMaxAge()
// cookie.MaxAge().InRange(time.Minute, time.Minute*10)
func (c *Cookie) MaxAge() *Duration {
opChain := c.chain.enter("MaxAge()")
Expand Down
44 changes: 22 additions & 22 deletions cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestCookie_FailedChain(t *testing.T) {
value.Expires().chain.assert(t, failure)
value.MaxAge().chain.assert(t, failure)

value.HasMaxAge()
value.NotHasMaxAge()
value.ContainsMaxAge()
value.NotContainsMaxAge()
}

t.Run("failed chain", func(t *testing.T) {
Expand Down Expand Up @@ -158,27 +158,27 @@ func TestCookie_Getters(t *testing.T) {

func TestCookie_MaxAge(t *testing.T) {
cases := []struct {
name string
maxAge int
wantHasMaxAge chainResult
wantDuration time.Duration
name string
maxAge int
wantContainsMaxAge chainResult
wantDuration time.Duration
}{
{
name: "unset",
maxAge: 0,
wantHasMaxAge: failure,
name: "unset",
maxAge: 0,
wantContainsMaxAge: failure,
},
{
name: "zero",
maxAge: -1,
wantHasMaxAge: success,
wantDuration: time.Duration(0),
name: "zero",
maxAge: -1,
wantContainsMaxAge: success,
wantDuration: time.Duration(0),
},
{
name: "non-zero",
maxAge: 3,
wantHasMaxAge: success,
wantDuration: time.Duration(3) * time.Second,
name: "non-zero",
maxAge: 3,
wantContainsMaxAge: success,
wantDuration: time.Duration(3) * time.Second,
},
}

Expand All @@ -189,13 +189,13 @@ func TestCookie_MaxAge(t *testing.T) {
MaxAge: tc.maxAge,
}

NewCookie(reporter, data).HasMaxAge().
chain.assert(t, tc.wantHasMaxAge)
NewCookie(reporter, data).ContainsMaxAge().
chain.assert(t, tc.wantContainsMaxAge)

NewCookie(reporter, data).NotHasMaxAge().
chain.assert(t, !tc.wantHasMaxAge)
NewCookie(reporter, data).NotContainsMaxAge().
chain.assert(t, !tc.wantContainsMaxAge)

if tc.wantHasMaxAge {
if tc.wantContainsMaxAge {
require.NotNil(t,
NewCookie(reporter, data).MaxAge().value)

Expand Down
4 changes: 2 additions & 2 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (d *Duration) Alias(name string) *Duration {

// Deprecated: support for unset durations will be removed. The only method that
// can create unset duration is Cookie.MaxAge. Instead of Cookie.MaxAge().IsSet(),
// please use Cookie.HasMaxAge().
// please use Cookie.ContainsMaxAge().
func (d *Duration) IsSet() *Duration {
opChain := d.chain.enter("IsSet()")
defer opChain.leave()
Expand All @@ -89,7 +89,7 @@ func (d *Duration) IsSet() *Duration {

// Deprecated: support for unset durations will be removed. The only method that
// can create unset duration is Cookie.MaxAge. Instead of Cookie.MaxAge().NotSet(),
// please use Cookie.NotHasMaxAge().
// please use Cookie.NotContainsMaxAge().
func (d *Duration) NotSet() *Duration {
opChain := d.chain.enter("NotSet()")
defer opChain.leave()
Expand Down

0 comments on commit 346651b

Please sign in to comment.