Do not include obsolete Cookie2 request header by default

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1647647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-12-23 19:03:27 +00:00
parent 46613dfcdc
commit f114594c6f
4 changed files with 5 additions and 295 deletions

View File

@ -52,7 +52,6 @@ import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.cookie.SetCookie2;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;
import org.apache.http.util.TextUtils;
@ -192,19 +191,10 @@ public class RequestAddCookies implements HttpRequestInterceptor {
final int ver = cookieSpec.getVersion();
if (ver > 0) {
boolean needVersionHeader = false;
for (final Cookie cookie : matchedCookies) {
if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) {
needVersionHeader = true;
}
}
if (needVersionHeader) {
final Header header = cookieSpec.getVersionHeader();
if (header != null) {
// Advertise cookie version support
request.addHeader(header);
}
final Header header = cookieSpec.getVersionHeader();
if (header != null) {
// Advertise cookie version support
request.addHeader(header);
}
}

View File

@ -210,7 +210,7 @@ public class DefaultCookieSpec implements CookieSpec {
@Override
public Header getVersionHeader() {
return strict.getVersionHeader();
return null;
}
@Override

View File

@ -472,41 +472,4 @@ public class TestRequestAddCookies {
Assert.assertEquals("name1=value; name2=value; name3=value", headers1[0].getValue());
}
@Test
public void testAddSpecVersionHeader() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.cookieStore.clear();
final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value1");
cookie1.setVersion(0);
cookie1.setDomain("localhost.local");
cookie1.setPath("/");
this.cookieStore.addCookie(cookie1);
final BasicClientCookie cookie2 = new BasicClientCookie("name2", "value2");
cookie2.setVersion(0);
cookie2.setDomain("localhost.local");
cookie2.setPath("/");
this.cookieStore.addCookie(cookie2);
final HttpRoute route = new HttpRoute(this.target, null, false);
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
final HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
final Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(1, headers1.length);
Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue());
final Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(1, headers2.length);
Assert.assertEquals("$Version=1", headers2[0].getValue());
}
}

View File

@ -1,243 +0,0 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.client.integration;
import java.io.IOException;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.SM;
import org.apache.http.cookie.SetCookie2;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Test;
/**
* Cookie2 support tests.
*/
public class TestCookie2Support extends LocalServerTestBase {
private static class CookieVer0Service implements HttpRequestHandler {
@Override
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
response.setStatusLine(httpversion, HttpStatus.SC_OK);
response.addHeader(new BasicHeader("Set-Cookie", "name1=value1; path=/test"));
final StringEntity entity = new StringEntity("whatever");
response.setEntity(entity);
}
}
@Test
public void testCookieVersionSupportHeader1() throws Exception {
this.serverBootstrap.registerHandler("*", new CookieVer0Service());
final HttpHost target = start();
final CookieStore cookieStore = new BasicCookieStore();
final HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
final HttpGet httpget = new HttpGet("/test/");
final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
final HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
final List<Cookie> cookies = cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
final HttpResponse response2 = this.httpclient.execute(target, httpget, context);
final HttpEntity e2 = response2.getEntity();
EntityUtils.consume(e2);
final HttpRequest reqWrapper = context.getRequest();
final Header cookiesupport = reqWrapper.getFirstHeader("Cookie2");
Assert.assertNotNull(cookiesupport);
Assert.assertEquals("$Version=1", cookiesupport.getValue());
}
private static class CookieVer1Service implements HttpRequestHandler {
@Override
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
response.setStatusLine(httpversion, HttpStatus.SC_OK);
response.addHeader(new BasicHeader("Set-Cookie", "name1=value1; Path=\"/test\"; Version=1"));
response.addHeader(new BasicHeader("Set-Cookie2", "name2=value2; Path=\"/test\"; Version=1"));
final StringEntity entity = new StringEntity("whatever");
response.setEntity(entity);
}
}
@Test
public void testCookieVersionSupportHeader2() throws Exception {
this.serverBootstrap.registerHandler("*", new CookieVer1Service());
final HttpHost target = start();
final CookieStore cookieStore = new BasicCookieStore();
final HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
final HttpGet httpget = new HttpGet("/test/");
final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
final HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
final List<Cookie> cookies = cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(2, cookies.size());
final HttpResponse response2 = this.httpclient.execute(target, httpget, context);
final HttpEntity e2 = response2.getEntity();
EntityUtils.consume(e2);
final HttpRequest reqWrapper = context.getRequest();
final Header cookiesupport = reqWrapper.getFirstHeader(SM.COOKIE2);
Assert.assertNotNull(cookiesupport);
Assert.assertEquals("$Version=1", cookiesupport.getValue());
}
private static class CookieVer2Service implements HttpRequestHandler {
@Override
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
response.setStatusLine(httpversion, HttpStatus.SC_OK);
response.addHeader(new BasicHeader("Set-Cookie2", "name2=value2; Path=\"/test\"; Version=2"));
final StringEntity entity = new StringEntity("whatever");
response.setEntity(entity);
}
}
@Test
public void testCookieVersionSupportHeader3() throws Exception {
this.serverBootstrap.registerHandler("*", new CookieVer2Service());
final HttpHost target = start();
final CookieStore cookieStore = new BasicCookieStore();
final HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
final HttpGet httpget = new HttpGet("/test/");
final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
final HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
final List<Cookie> cookies = cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
final HttpResponse response2 = this.httpclient.execute(target, httpget, context);
final HttpEntity e2 = response2.getEntity();
EntityUtils.consume(e2);
final HttpRequest reqWrapper = context.getRequest();
final Header cookiesupport = reqWrapper.getFirstHeader("Cookie2");
Assert.assertNotNull(cookiesupport);
Assert.assertEquals("$Version=1", cookiesupport.getValue());
}
private static class SetCookieVersionMixService implements HttpRequestHandler {
@Override
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final ProtocolVersion httpversion = request.getRequestLine().getProtocolVersion();
response.setStatusLine(httpversion, HttpStatus.SC_OK);
response.addHeader(new BasicHeader("Set-Cookie", "name=wrong; Path=/test"));
response.addHeader(new BasicHeader("Set-Cookie2", "name=right; Path=\"/test\"; Version=1"));
final StringEntity entity = new StringEntity("whatever");
response.setEntity(entity);
}
}
@Test
public void testSetCookieVersionMix() throws Exception {
this.serverBootstrap.registerHandler("*", new SetCookieVersionMixService());
final HttpHost target = start();
final CookieStore cookieStore = new BasicCookieStore();
final HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
final HttpGet httpget = new HttpGet("/test/");
final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
final HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
final List<Cookie> cookies = cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
Assert.assertEquals("right", cookies.get(0).getValue());
Assert.assertTrue(cookies.get(0) instanceof SetCookie2);
}
}