cookie.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package httpexpect
  2. import (
  3. "errors"
  4. "net/http"
  5. "time"
  6. )
  7. // Cookie provides methods to inspect attached http.Cookie value.
  8. type Cookie struct {
  9. noCopy noCopy
  10. chain *chain
  11. value *http.Cookie
  12. }
  13. // NewCookie returns a new Cookie instance.
  14. //
  15. // If reporter is nil, the function panics.
  16. // If value is nil, failure is reported.
  17. //
  18. // Example:
  19. //
  20. // cookie := NewCookie(t, &http.Cookie{...})
  21. //
  22. // cookie.Domain().IsEqual("example.com")
  23. // cookie.Path().IsEqual("/")
  24. // cookie.Expires().InRange(time.Now(), time.Now().Add(time.Hour * 24))
  25. func NewCookie(reporter Reporter, value *http.Cookie) *Cookie {
  26. return newCookie(newChainWithDefaults("Cookie()", reporter), value)
  27. }
  28. // NewCookieC returns a new Cookie instance with config.
  29. //
  30. // Requirements for config are same as for WithConfig function.
  31. // If value is nil, failure is reported.
  32. //
  33. // See NewCookie for usage example.
  34. func NewCookieC(config Config, value *http.Cookie) *Cookie {
  35. return newCookie(newChainWithConfig("Cookie()", config.withDefaults()), value)
  36. }
  37. func newCookie(parent *chain, val *http.Cookie) *Cookie {
  38. c := &Cookie{chain: parent.clone(), value: nil}
  39. opChain := c.chain.enter("")
  40. defer opChain.leave()
  41. if val == nil {
  42. opChain.fail(AssertionFailure{
  43. Type: AssertNotNil,
  44. Actual: &AssertionValue{val},
  45. Errors: []error{
  46. errors.New("expected: non-nil cookie"),
  47. },
  48. })
  49. } else {
  50. c.value = val
  51. }
  52. return c
  53. }
  54. // Raw returns underlying http.Cookie value attached to Cookie.
  55. // This is the value originally passed to NewCookie.
  56. //
  57. // Example:
  58. //
  59. // cookie := NewCookie(t, c)
  60. // assert.Equal(t, c, cookie.Raw())
  61. func (c *Cookie) Raw() *http.Cookie {
  62. return c.value
  63. }
  64. // Alias is similar to Value.Alias.
  65. func (c *Cookie) Alias(name string) *Cookie {
  66. opChain := c.chain.enter("Alias(%q)", name)
  67. defer opChain.leave()
  68. c.chain.setAlias(name)
  69. return c
  70. }
  71. // Name returns a new String instance with cookie name.
  72. //
  73. // Example:
  74. //
  75. // cookie := NewCookie(t, &http.Cookie{...})
  76. // cookie.Name().IsEqual("session")
  77. func (c *Cookie) Name() *String {
  78. opChain := c.chain.enter("Name()")
  79. defer opChain.leave()
  80. if opChain.failed() {
  81. return newString(opChain, "")
  82. }
  83. return newString(opChain, c.value.Name)
  84. }
  85. // Value returns a new String instance with cookie value.
  86. //
  87. // Example:
  88. //
  89. // cookie := NewCookie(t, &http.Cookie{...})
  90. // cookie.Value().IsEqual("gH6z7Y")
  91. func (c *Cookie) Value() *String {
  92. opChain := c.chain.enter("Value()")
  93. defer opChain.leave()
  94. if opChain.failed() {
  95. return newString(opChain, "")
  96. }
  97. return newString(opChain, c.value.Value)
  98. }
  99. // Domain returns a new String instance with cookie domain.
  100. //
  101. // Example:
  102. //
  103. // cookie := NewCookie(t, &http.Cookie{...})
  104. // cookie.Domain().IsEqual("example.com")
  105. func (c *Cookie) Domain() *String {
  106. opChain := c.chain.enter("Domain()")
  107. defer opChain.leave()
  108. if opChain.failed() {
  109. return newString(opChain, "")
  110. }
  111. return newString(opChain, c.value.Domain)
  112. }
  113. // Path returns a new String instance with cookie path.
  114. //
  115. // Example:
  116. //
  117. // cookie := NewCookie(t, &http.Cookie{...})
  118. // cookie.Path().IsEqual("/foo")
  119. func (c *Cookie) Path() *String {
  120. opChain := c.chain.enter("Path()")
  121. defer opChain.leave()
  122. if opChain.failed() {
  123. return newString(opChain, "")
  124. }
  125. return newString(opChain, c.value.Path)
  126. }
  127. // Expires returns a new DateTime instance with cookie expiration date.
  128. //
  129. // Example:
  130. //
  131. // cookie := NewCookie(t, &http.Cookie{...})
  132. // cookie.Expires().InRange(time.Now(), time.Now().Add(time.Hour * 24))
  133. func (c *Cookie) Expires() *DateTime {
  134. opChain := c.chain.enter("Expires()")
  135. defer opChain.leave()
  136. if opChain.failed() {
  137. return newDateTime(opChain, time.Unix(0, 0))
  138. }
  139. return newDateTime(opChain, c.value.Expires)
  140. }
  141. // HasMaxAge succeeds if cookie has Max-Age field.
  142. //
  143. // In particular, if Max-Age is present and is zero (which means delete
  144. // cookie now), method succeeds.
  145. //
  146. // Example:
  147. //
  148. // cookie := NewCookie(t, &http.Cookie{...})
  149. // cookie.HasMaxAge()
  150. func (c *Cookie) HasMaxAge() *Cookie {
  151. opChain := c.chain.enter("HasMaxAge()")
  152. defer opChain.leave()
  153. if opChain.failed() {
  154. return c
  155. }
  156. if c.value.MaxAge == 0 {
  157. opChain.fail(AssertionFailure{
  158. Type: AssertValid,
  159. Actual: &AssertionValue{c.value},
  160. Errors: []error{
  161. errors.New("expected: cookie has Max-Age field"),
  162. },
  163. })
  164. }
  165. return c
  166. }
  167. // NotHasMaxAge succeeds if cookie does not have Max-Age field.
  168. //
  169. // In particular, if Max-Age is present and is zero (which means delete
  170. // cookie now), method fails.
  171. //
  172. // Example:
  173. //
  174. // cookie := NewCookie(t, &http.Cookie{...})
  175. // cookie.NotHasMaxAge()
  176. func (c *Cookie) NotHasMaxAge() *Cookie {
  177. opChain := c.chain.enter("NotHasMaxAge()")
  178. defer opChain.leave()
  179. if opChain.failed() {
  180. return c
  181. }
  182. if c.value.MaxAge != 0 {
  183. opChain.fail(AssertionFailure{
  184. Type: AssertNotValid,
  185. Actual: &AssertionValue{c.value},
  186. Errors: []error{
  187. errors.New("expected: cookie does not have Max-Age field"),
  188. },
  189. })
  190. }
  191. return c
  192. }
  193. // Deprecated: use HasMaxAge instead.
  194. func (c *Cookie) HaveMaxAge() *Cookie {
  195. return c.HasMaxAge()
  196. }
  197. // Deprecated: use NotHasMaxAge instead.
  198. func (c *Cookie) NotHaveMaxAge() *Cookie {
  199. return c.NotHasMaxAge()
  200. }
  201. // MaxAge returns a new Duration instance with cookie Max-Age field.
  202. //
  203. // If Max-Age is not present, method fails.
  204. //
  205. // If Max-Age is present and is zero (which means delete cookie now),
  206. // methods succeeds and the returned Duration is equal to zero.
  207. //
  208. // Example:
  209. //
  210. // cookie := NewCookie(t, &http.Cookie{...})
  211. // cookie.HasMaxAge()
  212. // cookie.MaxAge().InRange(time.Minute, time.Minute*10)
  213. func (c *Cookie) MaxAge() *Duration {
  214. opChain := c.chain.enter("MaxAge()")
  215. defer opChain.leave()
  216. if opChain.failed() {
  217. return newDuration(opChain, nil)
  218. }
  219. switch {
  220. case c.value.MaxAge == 0: // zero value means not present
  221. // TODO: after removing Duration.IsSet, add failure here (breaking change)
  222. _ = (*Duration).IsSet
  223. return newDuration(opChain, nil)
  224. case c.value.MaxAge < 0: // negative value means present and zero
  225. age := time.Duration(0)
  226. return newDuration(opChain, &age)
  227. default:
  228. age := time.Duration(c.value.MaxAge) * time.Second
  229. return newDuration(opChain, &age)
  230. }
  231. }