[483] | 1 | <html> |
---|
| 2 | <head> |
---|
| 3 | <title>testing Cookies</title> |
---|
| 4 | <style type="text/css"> |
---|
| 5 | @import "../resources/dojo.css"; |
---|
| 6 | </style> |
---|
| 7 | <script type="text/javascript" src="../dojo.js" data-dojo-config="isDebug:true"></script> |
---|
| 8 | <script type="text/javascript"> |
---|
| 9 | require(["doh", "dojo/cookie", "dojo/domReady!"], function(doh, cookie){ |
---|
| 10 | doh.register([ |
---|
| 11 | { |
---|
| 12 | name: "basicSet", |
---|
| 13 | runTest: function(t){ |
---|
| 14 | // make sure the cookie is dead |
---|
| 15 | var old = new Date(1976, 8, 15); |
---|
| 16 | document.cookie = "dojo_test=blah; expires=" + old.toUTCString(); |
---|
| 17 | t.is(-1, document.cookie.indexOf("dojo_test=")); |
---|
| 18 | |
---|
| 19 | // set the new one |
---|
| 20 | var n = "dojo_test"; |
---|
| 21 | var v = "test value"; |
---|
| 22 | cookie(n, v); |
---|
| 23 | t.t(document.cookie.indexOf(n+"=") >= 0); |
---|
| 24 | var start = document.cookie.indexOf(n+"=") + n.length + 1; |
---|
| 25 | var end = document.cookie.indexOf(";", start); |
---|
| 26 | if(end == -1){ end = document.cookie.length; } |
---|
| 27 | t.is(v, decodeURIComponent(document.cookie.substring(start, end))); |
---|
| 28 | } |
---|
| 29 | }, |
---|
| 30 | { |
---|
| 31 | name: "basicGet", |
---|
| 32 | runTest: function(t){ |
---|
| 33 | // set the cookie |
---|
| 34 | var n = "dojo_test"; |
---|
| 35 | var v = "foofoo"; |
---|
| 36 | document.cookie = n + "=" + v; |
---|
| 37 | |
---|
| 38 | t.is(v, cookie(n)); |
---|
| 39 | } |
---|
| 40 | }, |
---|
| 41 | { |
---|
| 42 | name: "daysAsNumber", |
---|
| 43 | runTest: function(t){ |
---|
| 44 | // set a cookie with a numerical expires |
---|
| 45 | cookie("dojo_num", "foo", { expires: 10 }); |
---|
| 46 | t.is("foo", cookie("dojo_num")); |
---|
| 47 | |
---|
| 48 | // remove the cookie by setting it with a negative |
---|
| 49 | // numerical expires. value doesn't really matter here |
---|
| 50 | cookie("dojo_num", "-deleted-", { expires: -10 }); |
---|
| 51 | t.is(null, cookie("dojo_num")); |
---|
| 52 | } |
---|
| 53 | }, |
---|
| 54 | { |
---|
| 55 | name: "nameSuffix", |
---|
| 56 | runTest: function(t){ |
---|
| 57 | // set two cookies with the same suffix |
---|
| 58 | cookie("user", "123", { expires: 10 }); |
---|
| 59 | cookie("xuser", "abc", { expires: 10 }); |
---|
| 60 | t.is("123", cookie("user")); |
---|
| 61 | t.is("abc", cookie("xuser")); |
---|
| 62 | |
---|
| 63 | // remove the cookie by setting it with a negative |
---|
| 64 | // numerical expires. value doesn't really matter here |
---|
| 65 | cookie("user", "-deleted-", { expires: -10 }); |
---|
| 66 | t.is(null, cookie("user")); |
---|
| 67 | cookie("xuser", "-deleted-", { expires: -10 }); |
---|
| 68 | t.is(null, cookie("xuser")); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | ]); |
---|
| 72 | |
---|
| 73 | doh.run(); |
---|
| 74 | }); |
---|
| 75 | </script> |
---|
| 76 | </head> |
---|
| 77 | <body> |
---|
| 78 | </body> |
---|
| 79 | </html> |
---|