1   /*
2    * $HeadURL:https://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/cookie/TestCookieVersionSupport.java $
3    * $Revision:400312 $
4    * $Date:2006-05-06 14:49:41 +0200 (Sat, 06 May 2006) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   */
27  
28  package org.apache.commons.httpclient.cookie;
29  
30  import java.io.IOException;
31  
32  import junit.framework.Test;
33  import junit.framework.TestSuite;
34  
35  import org.apache.commons.httpclient.Cookie;
36  import org.apache.commons.httpclient.Header;
37  import org.apache.commons.httpclient.HttpClientTestBase;
38  import org.apache.commons.httpclient.HttpState;
39  import org.apache.commons.httpclient.HttpStatus;
40  import org.apache.commons.httpclient.HttpVersion;
41  import org.apache.commons.httpclient.methods.GetMethod;
42  import org.apache.commons.httpclient.server.HttpService;
43  import org.apache.commons.httpclient.server.SimpleRequest;
44  import org.apache.commons.httpclient.server.SimpleResponse;
45  
46  /***
47   * Cookie version support tests.
48   *
49   * @author Oleg Kalnichevski
50   * 
51   * @version $Revision:400312 $
52   */
53  public class TestCookieVersionSupport extends HttpClientTestBase {
54  
55      // ------------------------------------------------------------ Constructor
56      public TestCookieVersionSupport(final String testName) throws IOException {
57          super(testName);
58      }
59  
60      // ------------------------------------------------------------------- Main
61      public static void main(String args[]) {
62          String[] testCaseName = { TestCookieVersionSupport.class.getName() };
63          junit.textui.TestRunner.main(testCaseName);
64      }
65  
66      // ------------------------------------------------------- TestCase Methods
67  
68      public static Test suite() {
69          return new TestSuite(TestCookieVersionSupport.class);
70      }
71  
72      private static class CookieVer0Service implements HttpService {
73  
74          public CookieVer0Service() {
75              super();
76          }
77  
78          public boolean process(final SimpleRequest request, final SimpleResponse response)
79              throws IOException
80          {
81              HttpVersion httpversion = request.getRequestLine().getHttpVersion();
82              response.setStatusLine(httpversion, HttpStatus.SC_OK);
83              response.addHeader(new Header("Set-Cookie", "name1=value1; path=/test"));
84              response.setBodyString("whatever");
85              return true;
86          }
87      }
88      
89      
90      public void testCookieVersionSupportHeader1() throws IOException {
91          this.server.setHttpService(new CookieVer0Service());
92          this.client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
93          GetMethod httpget1 = new GetMethod("/test/");
94          try {
95              this.client.executeMethod(httpget1);
96          } finally {
97              httpget1.releaseConnection();
98          }
99          GetMethod httpget2 = new GetMethod("/test/");
100         try {
101             this.client.executeMethod(httpget2);
102         } finally {
103             httpget2.releaseConnection();
104         }
105         Header cookiesupport = httpget2.getRequestHeader("Cookie2");
106         assertNotNull(cookiesupport);
107         assertEquals("$Version=\"1\"", cookiesupport.getValue());
108     }
109     
110     private static class CookieVer1Service implements HttpService {
111 
112         public CookieVer1Service() {
113             super();
114         }
115 
116         public boolean process(final SimpleRequest request, final SimpleResponse response)
117             throws IOException
118         {
119             HttpVersion httpversion = request.getRequestLine().getHttpVersion();
120             response.setStatusLine(httpversion, HttpStatus.SC_OK);
121             response.addHeader(new Header("Set-Cookie", "name1=value1; Path=\"/test\"; Version=\"1\""));
122             response.addHeader(new Header("Set-Cookie2", "name2=value2; Path=\"/test\"; Version=\"1\""));
123             response.setBodyString("whatever");
124             return true;
125         }
126     }
127     
128     
129     public void testCookieVersionSupportHeader2() throws IOException {
130         this.server.setHttpService(new CookieVer1Service());
131         this.client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
132         GetMethod httpget1 = new GetMethod("/test/");
133         try {
134             this.client.executeMethod(httpget1);
135         } finally {
136             httpget1.releaseConnection();
137         }
138         GetMethod httpget2 = new GetMethod("/test/");
139         try {
140             this.client.executeMethod(httpget2);
141         } finally {
142             httpget2.releaseConnection();
143         }
144         Header cookiesupport = httpget2.getRequestHeader("Cookie2");
145         assertNull(cookiesupport);
146     }
147 
148     private static class CookieVer2Service implements HttpService {
149 
150         public CookieVer2Service() {
151             super();
152         }
153 
154         public boolean process(final SimpleRequest request, final SimpleResponse response)
155             throws IOException
156         {
157             HttpVersion httpversion = request.getRequestLine().getHttpVersion();
158             response.setStatusLine(httpversion, HttpStatus.SC_OK);
159             response.addHeader(new Header("Set-Cookie2", "name2=value2; Path=\"/test\"; Version=\"2\""));
160             response.setBodyString("whatever");
161             return true;
162         }
163     }
164     
165     
166     public void testCookieVersionSupportHeader3() throws IOException {
167         this.server.setHttpService(new CookieVer2Service());
168         this.client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
169         GetMethod httpget1 = new GetMethod("/test/");
170         try {
171             this.client.executeMethod(httpget1);
172         } finally {
173             httpget1.releaseConnection();
174         }
175         GetMethod httpget2 = new GetMethod("/test/");
176         try {
177             this.client.executeMethod(httpget2);
178         } finally {
179             httpget2.releaseConnection();
180         }
181         Header cookiesupport = httpget2.getRequestHeader("Cookie2");
182         assertNotNull(cookiesupport);
183         assertEquals("$Version=\"1\"", cookiesupport.getValue());
184     }
185 
186     private static class SetCookieVersionMixService implements HttpService {
187 
188         public SetCookieVersionMixService() {
189             super();
190         }
191 
192         public boolean process(final SimpleRequest request, final SimpleResponse response)
193             throws IOException
194         {
195             HttpVersion httpversion = request.getRequestLine().getHttpVersion();
196             response.setStatusLine(httpversion, HttpStatus.SC_OK);
197             response.addHeader(new Header("Set-Cookie", "name=wrong; Path=/test"));
198             response.addHeader(new Header("Set-Cookie2", "name=right; Path=\"/test\"; Version=\"1\""));
199             response.setBodyString("whatever");
200             return true;
201         }
202     }
203     
204     public static class TestHttpState extends HttpState {
205         
206         public synchronized void addCookie(Cookie cookie) {
207             if (cookie != null) {
208                 if ("localhost.local".equals(cookie.getDomain())) {
209                     cookie.setDomain("localhost");
210                 }
211                 super.addCookie(cookie);
212             }
213         }
214     }
215     
216     public void testSetCookieVersionMix() throws IOException {
217         this.server.setHttpService(new SetCookieVersionMixService());
218         this.client.setState(new TestHttpState());
219         this.client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
220         GetMethod httpget1 = new GetMethod("/test/");
221         try {
222             this.client.executeMethod(httpget1);
223         } finally {
224             httpget1.releaseConnection();
225         }
226         Cookie[] cookies = this.client.getState().getCookies();
227         assertNotNull(cookies);
228         assertEquals(1, cookies.length);
229         assertEquals("right", cookies[0].getValue());
230         assertTrue(cookies[0] instanceof Cookie2);
231     }
232 
233     
234 }