forked from gavv/httpexpect
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
278 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package httpexpect | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCookieFailed(t *testing.T) { | ||
chain := makeChain(newMockReporter(t)) | ||
|
||
chain.fail("fail") | ||
|
||
value := &Cookie{chain, nil} | ||
|
||
assert.True(t, value.Raw() == nil) | ||
assert.True(t, value.Name() != nil) | ||
assert.True(t, value.Value() != nil) | ||
assert.True(t, value.Domain() != nil) | ||
assert.True(t, value.Path() != nil) | ||
assert.True(t, value.Expires() != nil) | ||
} | ||
|
||
func TestCookieGetters(t *testing.T) { | ||
reporter := newMockReporter(t) | ||
|
||
NewCookie(reporter, nil).chain.assertFailed(t) | ||
|
||
value := NewCookie(reporter, &http.Cookie{ | ||
Name: "name", | ||
Value: "value", | ||
Domain: "example.com", | ||
Path: "/path", | ||
Expires: time.Unix(1234, 0), | ||
}) | ||
|
||
value.chain.assertOK(t) | ||
|
||
value.Name().chain.assertOK(t) | ||
value.Value().chain.assertOK(t) | ||
value.Domain().chain.assertOK(t) | ||
value.Path().chain.assertOK(t) | ||
value.Expires().chain.assertOK(t) | ||
|
||
assert.Equal(t, "name", value.Name().Raw()) | ||
assert.Equal(t, "value", value.Value().Raw()) | ||
assert.Equal(t, "example.com", value.Domain().Raw()) | ||
assert.Equal(t, "/path", value.Path().Raw()) | ||
assert.True(t, time.Unix(1234, 0).Equal(value.Expires().Raw())) | ||
|
||
value.chain.assertOK(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters