cookie.html 2.32 KB
<html>
	<head>
		<title>testing Cookies</title>
		<style type="text/css">
			@import "../resources/dojo.css";
		</style>
		<script type="text/javascript" src="../dojo.js" data-dojo-config="isDebug:true"></script>
		<script type="text/javascript">
			require(["doh", "dojo/cookie", "dojo/domReady!"], function(doh, cookie){
				doh.register([
					{
						name: "basicSet",
						runTest: function(t){
							// make sure the cookie is dead
							var old = new Date(1976, 8, 15);
							document.cookie = "dojo_test=blah; expires=" + old.toUTCString();
							t.is(-1, document.cookie.indexOf("dojo_test="));
							
							// set the new one
							var n = "dojo_test";
							var v = "test value";
							cookie(n, v);
							t.t(document.cookie.indexOf(n+"=") >= 0);
							var start = document.cookie.indexOf(n+"=") + n.length + 1;
							var end = document.cookie.indexOf(";", start);
							if(end == -1){ end = document.cookie.length; }
							t.is(v, decodeURIComponent(document.cookie.substring(start, end)));
						}
					},
					{
						name: "basicGet",
						runTest: function(t){
							// set the cookie
							var n = "dojo_test";
							var v = "foofoo";
							document.cookie = n + "=" + v;
							
							t.is(v, cookie(n));
						}
					},
					{
						name: "daysAsNumber",
						runTest: function(t){
							// set a cookie with a numerical expires
							cookie("dojo_num", "foo", { expires: 10 });
							t.is("foo", cookie("dojo_num"));
							
							// remove the cookie by setting it with a negative
							// numerical expires. value doesn't really matter here
							cookie("dojo_num", "-deleted-", { expires: -10 });
							t.is(null, cookie("dojo_num"));
						}
					},
					{
						name: "nameSuffix",
						runTest: function(t){
							// set two cookies with the same suffix
							cookie("user", "123", { expires: 10 });
							cookie("xuser", "abc", { expires: 10 });
							t.is("123", cookie("user"));
							t.is("abc", cookie("xuser"));

							// remove the cookie by setting it with a negative
							// numerical expires. value doesn't really matter here
							cookie("user", "-deleted-", { expires: -10 });
							t.is(null, cookie("user"));
							cookie("xuser", "-deleted-", { expires: -10 });
							t.is(null, cookie("xuser"));
						}
					}
				]);

				doh.run();
			});
		</script>
	</head>
	<body>
	</body>
</html>