|
| 1 | +package cookies |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/sharat87/httpbun/exchange" |
| 5 | + "github.com/stretchr/testify/suite" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +type CookiesSuite struct { |
| 11 | + suite.Suite |
| 12 | +} |
| 13 | + |
| 14 | +func TestCookiesSuite(t *testing.T) { |
| 15 | + suite.Run(t, new(CookiesSuite)) |
| 16 | +} |
| 17 | + |
| 18 | +func (s *CookiesSuite) TestGetCookiesSingular() { |
| 19 | + resp := exchange.InvokeHandlerForTest( |
| 20 | + "cookie", |
| 21 | + http.Request{ |
| 22 | + Header: http.Header{ |
| 23 | + "Cookie": []string{"foo=bar"}, |
| 24 | + }, |
| 25 | + }, |
| 26 | + CookiesRoute, |
| 27 | + Routes[CookiesRoute], |
| 28 | + ) |
| 29 | + |
| 30 | + s.Equal(0, resp.Status) |
| 31 | + s.Equal(map[string]any{"cookies": map[string]string{"foo": "bar"}}, resp.Body) |
| 32 | +} |
| 33 | + |
| 34 | +func (s *CookiesSuite) TestGetCookiesPlural() { |
| 35 | + resp := exchange.InvokeHandlerForTest( |
| 36 | + "cookies", |
| 37 | + http.Request{ |
| 38 | + Header: http.Header{ |
| 39 | + "Cookie": []string{"foo=bar", "baz=qux"}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + CookiesRoute, |
| 43 | + Routes[CookiesRoute], |
| 44 | + ) |
| 45 | + |
| 46 | + s.Equal(0, resp.Status) |
| 47 | + s.Equal(map[string]any{"cookies": map[string]string{"foo": "bar", "baz": "qux"}}, resp.Body) |
| 48 | +} |
| 49 | + |
| 50 | +func (s *CookiesSuite) TestDeleteCookies() { |
| 51 | + resp := exchange.InvokeHandlerForTest( |
| 52 | + "cookies/delete?foo=1", |
| 53 | + http.Request{ |
| 54 | + Header: http.Header{ |
| 55 | + "Cookie": []string{"foo=bar", "baz=qux"}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + CookiesDeleteRoute, |
| 59 | + Routes[CookiesDeleteRoute], |
| 60 | + ) |
| 61 | + |
| 62 | + s.Equal(302, resp.Status) |
| 63 | + |
| 64 | + s.Equal(1, len(resp.Header)) |
| 65 | + s.Equal("/cookies", resp.Header.Get("Location")) |
| 66 | + |
| 67 | + s.Equal(1, len(resp.Cookies)) |
| 68 | + s.Equal("foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0", resp.Cookies[0].String()) |
| 69 | +} |
| 70 | + |
| 71 | +func (s *CookiesSuite) TestDeleteCookiesSingularAndNoValue() { |
| 72 | + resp := exchange.InvokeHandlerForTest( |
| 73 | + "cookies/delete?foo", |
| 74 | + http.Request{ |
| 75 | + Header: http.Header{ |
| 76 | + "Cookie": []string{"foo=bar", "baz=qux"}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + CookiesDeleteRoute, |
| 80 | + Routes[CookiesDeleteRoute], |
| 81 | + ) |
| 82 | + |
| 83 | + s.Equal(302, resp.Status) |
| 84 | + |
| 85 | + s.Equal(1, len(resp.Header)) |
| 86 | + s.Equal("/cookies", resp.Header.Get("Location")) |
| 87 | + |
| 88 | + s.Equal(1, len(resp.Cookies)) |
| 89 | + s.Equal("foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0", resp.Cookies[0].String()) |
| 90 | +} |
| 91 | + |
| 92 | +func (s *CookiesSuite) TestSetCookiesWithNameAndValueInPath() { |
| 93 | + resp := exchange.InvokeHandlerForTest( |
| 94 | + "cookies/set/foo/bar", |
| 95 | + http.Request{}, |
| 96 | + CookiesSetRoute, |
| 97 | + Routes[CookiesSetRoute], |
| 98 | + ) |
| 99 | + |
| 100 | + s.Equal(302, resp.Status) |
| 101 | + |
| 102 | + s.Equal(1, len(resp.Header)) |
| 103 | + s.Equal("/cookies", resp.Header.Get("Location")) |
| 104 | + |
| 105 | + s.Equal(1, len(resp.Cookies)) |
| 106 | + s.Equal("foo=bar; Path=/", resp.Cookies[0].String()) |
| 107 | +} |
| 108 | + |
| 109 | +func (s *CookiesSuite) TestSetCookiesWithNameAndValueInQuery() { |
| 110 | + resp := exchange.InvokeHandlerForTest( |
| 111 | + "cookies/set?foo=bar", |
| 112 | + http.Request{}, |
| 113 | + CookiesSetRoute, |
| 114 | + Routes[CookiesSetRoute], |
| 115 | + ) |
| 116 | + |
| 117 | + s.Equal(302, resp.Status) |
| 118 | + |
| 119 | + s.Equal(1, len(resp.Header)) |
| 120 | + s.Equal("/cookies", resp.Header.Get("Location")) |
| 121 | + |
| 122 | + s.Equal(1, len(resp.Cookies)) |
| 123 | + s.Equal("foo=bar; Path=/", resp.Cookies[0].String()) |
| 124 | +} |
| 125 | + |
| 126 | +func (s *CookiesSuite) TestSetCookiesWithNameAndValueInQueryMultiple() { |
| 127 | + resp := exchange.InvokeHandlerForTest( |
| 128 | + "cookies/set?foo=bar&baz=qux", |
| 129 | + http.Request{}, |
| 130 | + CookiesSetRoute, |
| 131 | + Routes[CookiesSetRoute], |
| 132 | + ) |
| 133 | + |
| 134 | + s.Equal(302, resp.Status) |
| 135 | + |
| 136 | + s.Equal(1, len(resp.Header)) |
| 137 | + s.Equal("/cookies", resp.Header.Get("Location")) |
| 138 | + |
| 139 | + s.Equal(2, len(resp.Cookies)) |
| 140 | + s.Equal("foo=bar; Path=/", resp.Cookies[0].String()) |
| 141 | + s.Equal("baz=qux; Path=/", resp.Cookies[1].String()) |
| 142 | +} |
0 commit comments