001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestEngineCookies.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.meterware.httpunit.GetMethodWebRequest;
011: import com.meterware.httpunit.WebConversation;
012: import com.meterware.httpunit.WebRequest;
013: import com.meterware.httpunit.WebResponse;
014: import com.uwyn.rife.TestCaseServerside;
015: import com.uwyn.rife.engine.exceptions.IncookieUnknownException;
016: import com.uwyn.rife.tools.HttpUtils;
017: import javax.servlet.http.Cookie;
018:
019: public class TestEngineCookies extends TestCaseServerside {
020: public TestEngineCookies(int siteType, String name) {
021: super (siteType, name);
022: }
023:
024: public void testCookiesValid() throws Exception {
025: setupSite("site/cookies.xml");
026:
027: HttpUtils.Page page = null;
028:
029: // initial page that accepts and overrides cookies
030: page = new HttpUtils.Request(
031: "http://localhost:8181/cookies/valid/source").cookie(
032: "cookie1", "this is the first cookie").cookie(
033: "cookie2", "this is the second cookie").cookie(
034: "cookie3", "this is the third cookie").retrieve();
035:
036: // check if the correct cookies were returned
037: assertTrue(page.checkReceivedCookies(new Cookie[] {
038: new Cookie("cookie3",
039: "this%20is%20the%20first%20cookie"),
040: new Cookie("cookie4",
041: "this%20is%20the%20second%20cookie") }));
042:
043: // new page with cookie context
044: page = new HttpUtils.Request(
045: "http://localhost:8181/cookies/valid/destination")
046: .cookie("cookie1", "this is the first cookie").cookie(
047: "cookie2", "this is the second cookie").cookie(
048: "cookie3", "this is the first cookie").cookie(
049: "cookie4", "this is the second cookie")
050: .retrieve();
051:
052: assertEquals(
053: "this is the second cookie,this is the first cookie,this is the second cookie",
054: page.getContent());
055: }
056:
057: public void testCookiesBijection() throws Exception {
058: setupSite("site/cookies.xml");
059:
060: HttpUtils.Page page = null;
061:
062: // initial page that accepts and overrides cookies
063: page = new HttpUtils.Request(
064: "http://localhost:8181/cookies/valid/source/bijection")
065: .cookie("cookie1", "this is the first cookie").cookie(
066: "cookie2", "this is the second cookie").cookie(
067: "cookie3", "this is the third cookie")
068: .retrieve();
069:
070: // check if the correct cookies were returned
071: assertTrue(page.checkReceivedCookies(new Cookie[] {
072: new Cookie("cookie3",
073: "this%20is%20the%20first%20cookie"),
074: new Cookie("cookie4",
075: "this%20is%20the%20second%20cookie") }));
076:
077: // new page with cookie context
078: page = new HttpUtils.Request(
079: "http://localhost:8181/cookies/valid/destination")
080: .cookie("cookie1", "this is the first cookie").cookie(
081: "cookie2", "this is the second cookie").cookie(
082: "cookie3", "this is the first cookie").cookie(
083: "cookie4", "this is the second cookie")
084: .retrieve();
085:
086: assertEquals(
087: "this is the second cookie,this is the first cookie,this is the second cookie",
088: page.getContent());
089: }
090:
091: public void testCookiesInvalid() throws Exception {
092: setupSite("site/cookies.xml");
093:
094: HttpUtils.Page page = null;
095:
096: try {
097: page = new HttpUtils.Request(
098: "http://localhost:8181/cookies/invalid/source")
099: .cookie("cookie1", "this is the first cookie")
100: .cookie("cookie2", "this is the second cookie")
101: .cookie("cookie3", "this is the third cookie")
102: .retrieve();
103: fail();
104: assertNotNull(page);
105: } catch (Exception e) {
106: assertTrue(getLogSink().getInternalException() instanceof IncookieUnknownException);
107:
108: IncookieUnknownException e2 = (IncookieUnknownException) getLogSink()
109: .getInternalException();
110: assertEquals("cookie3", e2.getIncookieName());
111: }
112:
113: try {
114: page = new HttpUtils.Request(
115: "http://localhost:8181/cookies/invalid/destination")
116: .cookie("cookie1", "this is the first cookie")
117: .cookie("cookie2", "this is the second cookie")
118: .cookie("cookie3", "this is the third cookie")
119: .retrieve();
120: fail();
121: } catch (Exception e) {
122: assertTrue(getLogSink().getInternalException() instanceof IncookieUnknownException);
123:
124: IncookieUnknownException e2 = (IncookieUnknownException) getLogSink()
125: .getInternalException();
126: assertEquals("cookie2", e2.getIncookieName());
127: }
128: }
129:
130: public void testCookiesDefault() throws Exception {
131: setupSite("site/cookies.xml");
132:
133: HttpUtils.Page page = HttpUtils
134: .retrievePage("http://localhost:8181/cookies/defaults");
135: assertEquals("cookie1 : the first cookie"
136: + "cookie3 : the element config value", page
137: .getContent());
138:
139: // check if the correct cookies were returned
140: assertTrue(page
141: .checkReceivedCookies(new Cookie[] {
142: new Cookie("cookie4",
143: "the%20element%20config%20value"),
144: new Cookie("cookie5", "the%20fifth%20cookie") }));
145: }
146:
147: public void testIncookiesInjection() throws Exception {
148: setupSite("site/cookies.xml");
149: HttpUtils.Page page = new HttpUtils.Request(
150: "http://localhost:8181/incookies/injection").cookie(
151: "firstname", "Geert").cookie("lastname", "Bevin")
152: .cookie("globalcookie1", "globalcookievalue1")
153: .retrieve();
154:
155: assertEquals(
156: "Welcome Geert Bevin\nglobalcookievalue1 globalcookievalue2 ",
157: page.getContent());
158: }
159:
160: public void testIncookiesGenerated() throws Exception {
161: setupSite("site/cookies.xml");
162: HttpUtils.Page page = new HttpUtils.Request(
163: "http://localhost:8181/incookies/generated").cookie(
164: "firstname", "Geert").cookie("lastname", "Bevin")
165: .cookie("globalcookie1", "globalcookievalue1")
166: .retrieve();
167:
168: assertEquals(
169: "Welcome Geert <!--V 'INCOOKIE:middlename'/--> Bevin\nglobalcookievalue1 globalcookievalue2 <!--V 'INCOOKIE:globalcookie3'/--> <!--V 'INCOOKIE:unknown'/-->\n",
170: page.getContent());
171: }
172:
173: public void testOutcookiesGenerated() throws Exception {
174: setupSite("site/cookies.xml");
175: WebConversation conversation = new WebConversation();
176: WebRequest request = new GetMethodWebRequest(
177: "http://localhost:8181/outcookies/generated");
178: WebResponse response = conversation.getResponse(request);
179: assertEquals(
180: "Welcome Geert Bevin\n<!--V 'OUTCOOKIE:globalcookie1'/--> <!--V 'OUTCOOKIE:globalcookie2'/--> <!--V 'OUTCOOKIE:globalcookie3'/--> <!--V 'OUTCOOKIE:unknown'/-->\n",
181: response.getText());
182: }
183:
184: public void testOutcookiesOutjection() throws Exception {
185: setupSite("site/cookies.xml");
186: WebConversation conversation = new WebConversation();
187: WebRequest request = new GetMethodWebRequest(
188: "http://localhost:8181/outcookies/outjection");
189: WebResponse response = conversation.getResponse(request);
190: assertEquals(
191: "Welcome John Darryl\n<!--V 'OUTCOOKIE:globalcookie1'/--> <!--V 'OUTCOOKIE:globalcookie2'/--> <!--V 'OUTCOOKIE:globalcookie3'/--> <!--V 'OUTCOOKIE:unknown'/-->\n",
192: response.getText());
193: }
194: }
|