TestCookieVersionSupport xref

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