1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.doc;
16
17 import java.net.URL;
18 import java.util.Map;
19
20 import org.htmlunit.MockWebConnection;
21 import org.htmlunit.WebClient;
22 import org.htmlunit.WebServerTestCase;
23 import org.junit.Test;
24
25
26
27
28
29
30
31 public class StorageHolderTest extends WebServerTestCase {
32
33
34
35
36 @Test
37 public void sessionStorage() throws Exception {
38 final MockWebConnection conn = getMockWebConnection();
39 conn.setDefaultResponse("<script>sessionStorage.setItem('myNewKey', 'myNewData');</script>");
40
41 startWebServer(conn);
42 final URL url = URL_FIRST;
43
44 try (WebClient webClient = new WebClient()) {
45
46
47 final Map<String, String> sessionStorage =
48 webClient.getStorageHolder().getSessionStorage(webClient.getCurrentWindow());
49
50
51 sessionStorage.put("myKey", "myData");
52
53
54 webClient.getPage(url);
55
56
57 assertEquals("myNewData", sessionStorage.get("myNewKey"));
58 }
59 }
60
61
62
63
64 @Test
65 public void localStorage() throws Exception {
66 final MockWebConnection conn = getMockWebConnection();
67 conn.setDefaultResponse("<script>localStorage.setItem('myNewKey', 'myNewData');</script>");
68
69 startWebServer(conn);
70 final URL url = URL_FIRST;
71
72 try (WebClient webClient = new WebClient()) {
73
74
75
76 final Map<String, String> localStorage = webClient.getStorageHolder().getLocalStorage(url);
77
78
79 localStorage.put("myKey", "myData");
80
81
82 webClient.getPage(url);
83
84
85 assertEquals("myNewData", localStorage.get("myNewKey"));
86 }
87 }
88 }