审查视图

vendor/github.com/gavv/httpexpect/cookie.go 3.4 KB
tangxvhui authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
package httpexpect

import (
	"net/http"
	"time"
)

// Cookie provides methods to inspect attached http.Cookie value.
type Cookie struct {
	chain chain
	value *http.Cookie
}

// NewCookie returns a new Cookie object given a reporter used to report
// failures and cookie value to be inspected.
//
// reporter and value should not be nil.
//
// Example:
//   cookie := NewCookie(reporter, &http.Cookie{...})
//   cookie.Domain().Equal("example.com")
//   cookie.Path().Equal("/")
//   cookie.Expires().InRange(time.Now(), time.Now().Add(time.Hour * 24))
func NewCookie(reporter Reporter, value *http.Cookie) *Cookie {
	chain := makeChain(reporter)
	if value == nil {
		chain.fail("expected non-nil cookie")
	}
	return &Cookie{chain, value}
}

// Raw returns underlying http.Cookie value attached to Cookie.
// This is the value originally passed to NewCookie.
//
// Example:
//  cookie := NewCookie(t, c)
//  assert.Equal(t, c, cookie.Raw())
func (c *Cookie) Raw() *http.Cookie {
	return c.value
}

// Name returns a new String object that may be used to inspect
// cookie name.
//
// Example:
//  cookie := NewCookie(t, &http.Cookie{...})
//  cookie.Name().Equal("session")
func (c *Cookie) Name() *String {
	if c.chain.failed() {
		return &String{c.chain, ""}
	}
	return &String{c.chain, c.value.Name}
}

// Value returns a new String object that may be used to inspect
// cookie value.
//
// Example:
//  cookie := NewCookie(t, &http.Cookie{...})
//  cookie.Value().Equal("gH6z7Y")
func (c *Cookie) Value() *String {
	if c.chain.failed() {
		return &String{c.chain, ""}
	}
	return &String{c.chain, c.value.Value}
}

// Domain returns a new String object that may be used to inspect
// cookie domain.
//
// Example:
//  cookie := NewCookie(t, &http.Cookie{...})
//  cookie.Domain().Equal("example.com")
func (c *Cookie) Domain() *String {
	if c.chain.failed() {
		return &String{c.chain, ""}
	}
	return &String{c.chain, c.value.Domain}
}

// Path returns a new String object that may be used to inspect
// cookie path.
//
// Example:
//  cookie := NewCookie(t, &http.Cookie{...})
//  cookie.Path().Equal("/foo")
func (c *Cookie) Path() *String {
	if c.chain.failed() {
		return &String{c.chain, ""}
	}
	return &String{c.chain, c.value.Path}
}

// Expires returns a new DateTime object that may be used to inspect
// cookie expiration date.
//
// Example:
//  cookie := NewCookie(t, &http.Cookie{...})
//  cookie.Expires().InRange(time.Now(), time.Now().Add(time.Hour * 24))
func (c *Cookie) Expires() *DateTime {
	if c.chain.failed() {
		return &DateTime{c.chain, time.Unix(0, 0)}
	}
	return &DateTime{c.chain, c.value.Expires}
}

// MaxAge returns a new Duration object that may be used to inspect
// cookie Max-age field.
//
// If MaxAge is not set, the returned Duration is unset. Whether a Duration
// is set or not can be chacked using its IsSet and NotSet methods.
//
// If MaxAge is zero (which means delete cookie now), the returned Duration
// is set and equals to zero.
//
// Example:
//  cookie := NewCookie(t, &http.Cookie{...})
//  cookie.MaxAge().IsSet()
//  cookie.MaxAge().InRange(time.Minute, time.Minute*10)
func (c *Cookie) MaxAge() *Duration {
	if c.chain.failed() {
		return &Duration{c.chain, nil}
	}
	if c.value.MaxAge == 0 {
		return &Duration{c.chain, nil}
	}
	if c.value.MaxAge < 0 {
		var zero time.Duration
		return &Duration{c.chain, &zero}
	}
	d := time.Duration(c.value.MaxAge) * time.Second
	return &Duration{c.chain, &d}
}