001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/cookie/TestCookieVersionSupport.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: */
028:
029: package org.apache.commons.httpclient.cookie;
030:
031: import java.io.IOException;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035:
036: import org.apache.commons.httpclient.Cookie;
037: import org.apache.commons.httpclient.Header;
038: import org.apache.commons.httpclient.HttpClientTestBase;
039: import org.apache.commons.httpclient.HttpState;
040: import org.apache.commons.httpclient.HttpStatus;
041: import org.apache.commons.httpclient.HttpVersion;
042: import org.apache.commons.httpclient.methods.GetMethod;
043: import org.apache.commons.httpclient.server.HttpService;
044: import org.apache.commons.httpclient.server.SimpleRequest;
045: import org.apache.commons.httpclient.server.SimpleResponse;
046:
047: /**
048: * Cookie version support tests.
049: *
050: * @author Oleg Kalnichevski
051: *
052: * @version $Revision: 480424 $
053: */
054: public class TestCookieVersionSupport extends HttpClientTestBase {
055:
056: // ------------------------------------------------------------ Constructor
057: public TestCookieVersionSupport(final String testName)
058: throws IOException {
059: super (testName);
060: }
061:
062: // ------------------------------------------------------------------- Main
063: public static void main(String args[]) {
064: String[] testCaseName = { TestCookieVersionSupport.class
065: .getName() };
066: junit.textui.TestRunner.main(testCaseName);
067: }
068:
069: // ------------------------------------------------------- TestCase Methods
070:
071: public static Test suite() {
072: return new TestSuite(TestCookieVersionSupport.class);
073: }
074:
075: private static class CookieVer0Service implements HttpService {
076:
077: public CookieVer0Service() {
078: super ();
079: }
080:
081: public boolean process(final SimpleRequest request,
082: final SimpleResponse response) throws IOException {
083: HttpVersion httpversion = request.getRequestLine()
084: .getHttpVersion();
085: response.setStatusLine(httpversion, HttpStatus.SC_OK);
086: response.addHeader(new Header("Set-Cookie",
087: "name1=value1; path=/test"));
088: response.setBodyString("whatever");
089: return true;
090: }
091: }
092:
093: public void testCookieVersionSupportHeader1() throws IOException {
094: this .server.setHttpService(new CookieVer0Service());
095: this .client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
096: GetMethod httpget1 = new GetMethod("/test/");
097: try {
098: this .client.executeMethod(httpget1);
099: } finally {
100: httpget1.releaseConnection();
101: }
102: GetMethod httpget2 = new GetMethod("/test/");
103: try {
104: this .client.executeMethod(httpget2);
105: } finally {
106: httpget2.releaseConnection();
107: }
108: Header cookiesupport = httpget2.getRequestHeader("Cookie2");
109: assertNotNull(cookiesupport);
110: assertEquals("$Version=\"1\"", cookiesupport.getValue());
111: }
112:
113: private static class CookieVer1Service implements HttpService {
114:
115: public CookieVer1Service() {
116: super ();
117: }
118:
119: public boolean process(final SimpleRequest request,
120: final SimpleResponse response) throws IOException {
121: HttpVersion httpversion = request.getRequestLine()
122: .getHttpVersion();
123: response.setStatusLine(httpversion, HttpStatus.SC_OK);
124: response.addHeader(new Header("Set-Cookie",
125: "name1=value1; Path=\"/test\"; Version=\"1\""));
126: response.addHeader(new Header("Set-Cookie2",
127: "name2=value2; Path=\"/test\"; Version=\"1\""));
128: response.setBodyString("whatever");
129: return true;
130: }
131: }
132:
133: public void testCookieVersionSupportHeader2() throws IOException {
134: this .server.setHttpService(new CookieVer1Service());
135: this .client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
136: GetMethod httpget1 = new GetMethod("/test/");
137: try {
138: this .client.executeMethod(httpget1);
139: } finally {
140: httpget1.releaseConnection();
141: }
142: GetMethod httpget2 = new GetMethod("/test/");
143: try {
144: this .client.executeMethod(httpget2);
145: } finally {
146: httpget2.releaseConnection();
147: }
148: Header cookiesupport = httpget2.getRequestHeader("Cookie2");
149: assertNull(cookiesupport);
150: }
151:
152: private static class CookieVer2Service implements HttpService {
153:
154: public CookieVer2Service() {
155: super ();
156: }
157:
158: public boolean process(final SimpleRequest request,
159: final SimpleResponse response) throws IOException {
160: HttpVersion httpversion = request.getRequestLine()
161: .getHttpVersion();
162: response.setStatusLine(httpversion, HttpStatus.SC_OK);
163: response.addHeader(new Header("Set-Cookie2",
164: "name2=value2; Path=\"/test\"; Version=\"2\""));
165: response.setBodyString("whatever");
166: return true;
167: }
168: }
169:
170: public void testCookieVersionSupportHeader3() throws IOException {
171: this .server.setHttpService(new CookieVer2Service());
172: this .client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
173: GetMethod httpget1 = new GetMethod("/test/");
174: try {
175: this .client.executeMethod(httpget1);
176: } finally {
177: httpget1.releaseConnection();
178: }
179: GetMethod httpget2 = new GetMethod("/test/");
180: try {
181: this .client.executeMethod(httpget2);
182: } finally {
183: httpget2.releaseConnection();
184: }
185: Header cookiesupport = httpget2.getRequestHeader("Cookie2");
186: assertNotNull(cookiesupport);
187: assertEquals("$Version=\"1\"", cookiesupport.getValue());
188: }
189:
190: private static class SetCookieVersionMixService implements
191: HttpService {
192:
193: public SetCookieVersionMixService() {
194: super ();
195: }
196:
197: public boolean process(final SimpleRequest request,
198: final SimpleResponse response) throws IOException {
199: HttpVersion httpversion = request.getRequestLine()
200: .getHttpVersion();
201: response.setStatusLine(httpversion, HttpStatus.SC_OK);
202: response.addHeader(new Header("Set-Cookie",
203: "name=wrong; Path=/test"));
204: response.addHeader(new Header("Set-Cookie2",
205: "name=right; Path=\"/test\"; Version=\"1\""));
206: response.setBodyString("whatever");
207: return true;
208: }
209: }
210:
211: public static class TestHttpState extends HttpState {
212:
213: public synchronized void addCookie(Cookie cookie) {
214: if (cookie != null) {
215: if ("localhost.local".equals(cookie.getDomain())) {
216: cookie.setDomain("localhost");
217: }
218: super .addCookie(cookie);
219: }
220: }
221: }
222:
223: public void testSetCookieVersionMix() throws IOException {
224: this .server.setHttpService(new SetCookieVersionMixService());
225: this .client.setState(new TestHttpState());
226: this .client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
227: GetMethod httpget1 = new GetMethod("/test/");
228: try {
229: this .client.executeMethod(httpget1);
230: } finally {
231: httpget1.releaseConnection();
232: }
233: Cookie[] cookies = this .client.getState().getCookies();
234: assertNotNull(cookies);
235: assertEquals(1, cookies.length);
236: assertEquals("right", cookies[0].getValue());
237: assertTrue(cookies[0] instanceof Cookie2);
238: }
239:
240: }
|