1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.tests;
20
21 import java.util.List;
22 import net.sourceforge.jwebunit.api.HttpHeader;
23 import org.junit.Test;
24
25 import static net.sourceforge.jwebunit.junit.JWebUnit.assertCookiePresent;
26 import static net.sourceforge.jwebunit.junit.JWebUnit.assertCookieValueEquals;
27 import static net.sourceforge.jwebunit.junit.JWebUnit.assertCookieValueMatch;
28 import static net.sourceforge.jwebunit.junit.JWebUnit.assertTextPresent;
29 import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
30 import static net.sourceforge.jwebunit.junit.JWebUnit.getResponseHeaders;
31 import static net.sourceforge.jwebunit.junit.JWebUnit.getTestContext;
32 import static net.sourceforge.jwebunit.junit.JWebUnit.gotoPage;
33 import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
34 import static org.junit.Assert.assertTrue;
35
36
37
38
39
40
41
42 public class WebCookieTest extends JWebUnitAPITestCase {
43
44
45 public void setUp() throws Exception {
46 super.setUp();
47 getTestContext().addCookie("cookie1", "Cookievalue1", "localhost");
48 setBaseUrl(HOST_PATH);
49 }
50
51 @Test
52 public void testAddCookie() {
53 beginAt("/cookies.jsp");
54 assertTextPresent("cookie1=Cookievalue1");
55 }
56
57 @Test
58 public void testAddAnotherCookie() {
59 getTestContext().addCookie("cookie2", "Cookievalue2", "localhost");
60 beginAt("/cookies.jsp");
61 assertTextPresent("cookie1=Cookievalue1");
62 assertTextPresent("cookie2=Cookievalue2");
63 }
64
65 @Test
66 public void testAssertCookiePresent() throws Throwable {
67 beginAt("/cookies.jsp");
68 assertCookiePresent("serveurCookie");
69 }
70
71 @Test
72 public void testAssertCookieValue() throws Throwable {
73 beginAt("/cookies.jsp");
74 assertCookieValueEquals("serveurCookie", "foo");
75 }
76
77 @Test
78 public void testAssertCookieMatch() throws Throwable {
79 beginAt("/cookies.jsp");
80 assertCookieValueMatch("serveurCookie", "fo*");
81 }
82
83
84
85
86
87
88 @Test
89 public void testCookieMatchLastCookie() {
90 beginAt("/cookies.jsp?set_by_headers=true&dont_set=1");
91 assertCookieValueMatch("JSESSIONID", "(.)*worker2");
92 }
93
94
95
96
97
98
99 @Test
100 public void testCookieWithoutExplicitSet() {
101 beginAt("/cookies.jsp");
102 assertCookieValueEquals("serveurCookie", "foo");
103 gotoPage("/cookies.jsp?dont_set=1");
104 assertCookieValueEquals("serveurCookie", "foo");
105 gotoPage("/cookies.jsp?dont_set=1");
106 assertCookieValueEquals("serveurCookie", "foo");
107 gotoPage("/cookies.jsp?dont_set=1");
108 assertCookieValueEquals("serveurCookie", "foo");
109 }
110
111
112
113
114
115
116
117 @Test
118 public void testCookieSetInHeaders() {
119 beginAt("/cookies.jsp?set_by_headers=true&dont_set=1");
120 List<HttpHeader> headers = getResponseHeaders();
121 boolean foundCookie1 = false;
122 boolean foundCookie2 = false;
123 for (HttpHeader h : headers) {
124 if (h.getName().equals("Set-Cookie")) {
125 if (h.getValue().contains(".worker1")) {
126 foundCookie1 = true;
127 }
128 else if (h.getValue().contains(".worker2")) {
129 foundCookie2 = true;
130 }
131 }
132 }
133 assertTrue("getResponseHeaders should return all headers even duplicates", foundCookie1 && foundCookie2);
134 }
135 }