View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
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   * Test the Cookies methods provided by WebTestCase.
39   * 
40   * @author Julien HENRY
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       * When there are several cookies with the same name, it is the last one that should
85       * be taken.
86       * See <a href="http://tools.ietf.org/html/draft-ietf-httpstate-cookie-21#section-5.3">the spec</a> (ยง 11)
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       * Test that the cookie still exists across multiple requests,
97       * even if the cookie is not explicitly set each time.
98       */
99      @Test
100     public void testCookieWithoutExplicitSet() {
101     	beginAt("/cookies.jsp");		// beginAt also resets cookies
102     	assertCookieValueEquals("serveurCookie", "foo");
103     	gotoPage("/cookies.jsp?dont_set=1");
104     	assertCookieValueEquals("serveurCookie", "foo");	// should still be there
105     	gotoPage("/cookies.jsp?dont_set=1");
106     	assertCookieValueEquals("serveurCookie", "foo");	// should still be there
107     	gotoPage("/cookies.jsp?dont_set=1");
108     	assertCookieValueEquals("serveurCookie", "foo");	// should still be there
109     }
110     
111     /**
112      * Tests if all cookies are received when the server sets several cookies 
113      * with same domain, path and name.<p>
114      * 
115      * See http://tools.ietf.org/html/draft-ietf-httpstate-cookie-21#section-5.3, 11
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 }