| 9 | ) |
| 10 | |
| 11 | func TestHTTPResponseCookie(t *testing.T) { |
| 12 | type Props map[string]any |
| 13 | |
| 14 | cases := []struct { |
| 15 | Name string |
| 16 | DSL func() |
| 17 | Props Props |
| 18 | }{ |
| 19 | {"cookie", testdata.CookieObjectResultDSL, nil}, |
| 20 | {"cookie", testdata.CookieStringResultDSL, nil}, |
| 21 | {"max-age", testdata.CookieMaxAgeDSL, Props{"cookie:max-age": testdata.CookieMaxAgeValue}}, |
| 22 | {"domain", testdata.CookieDomainDSL, Props{"cookie:domain": testdata.CookieDomainValue}}, |
| 23 | {"path", testdata.CookiePathDSL, Props{"cookie:path": testdata.CookiePathValue}}, |
| 24 | {"secure", testdata.CookieSecureDSL, Props{"cookie:secure": "Secure"}}, |
| 25 | {"http-only", testdata.CookieHTTPOnlyDSL, Props{"cookie:http-only": "HttpOnly"}}, |
| 26 | {"same-site", testdata.CookieSameSiteDSL, Props{"cookie:same-site": testdata.CookieSameSiteValue}}, |
| 27 | } |
| 28 | for _, c := range cases { |
| 29 | t.Run(c.Name, func(t *testing.T) { |
| 30 | root := expr.RunDSL(t, c.DSL) |
| 31 | e := root.API.HTTP.Services[len(root.API.HTTP.Services)-1].HTTPEndpoints[0] |
| 32 | cookies := e.Responses[0].Cookies.AttributeExpr |
| 33 | if len(*expr.AsObject(cookies.Type)) != 1 { |
| 34 | t.Errorf("got %d cookie(s), expected exactly one", len(*expr.AsObject(cookies.Type))) |
| 35 | } else { |
| 36 | m := cookies.Meta |
| 37 | for n, v := range c.Props { |
| 38 | switch { |
| 39 | case len(m) != 1: |
| 40 | t.Errorf("got cookies metadata with length %d, expected 1", len(m)) |
| 41 | case len(m[n]) != 1: |
| 42 | t.Errorf("got cookies metadata %q with length %d, expected 1", n, len(m[n])) |
| 43 | case m[n][0] != fmt.Sprintf("%v", v): |
| 44 | t.Errorf("got value %q for cookies metadata %q, expected %q", m[n][0], n, fmt.Sprintf("%v", v)) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |