@Override public List<Cookie> parse( final Header header, CookieOrigin origin) throws MalformedCookieException { if (header == null) { throw new IllegalArgumentException("Header may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE2)) { throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'"); } origin = adjustEffectiveHost(origin); HeaderElement[] elems = header.getElements(); return createCookies(elems, origin); }
public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); buffer.append("="); String s = cookie.getValue(); if (s != null) { buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); final String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Override public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); final String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Test public void testParseCookies() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final Cookie cookie = cookies.get(0); Assert.assertEquals(0, cookie.getVersion()); Assert.assertEquals("name1", cookie.getName()); Assert.assertEquals("value1", cookie.getValue()); Assert.assertEquals("localhost", cookie.getDomain()); Assert.assertEquals("/", cookie.getPath()); }
@Test public void testNoCookieOrigin() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, null); context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(0, cookies.size()); }
@Test public void testNoCookieSpec() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(HttpClientContext.COOKIE_SPEC, null); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(0, cookies.size()); }
@Test public void testNoCookieStore() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(HttpClientContext.COOKIE_STORE, null); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(0, cookies.size()); }
@Test public void testSetCookie2OverrideSetCookie() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); response.addHeader(SM.SET_COOKIE2, "name1=value2; Version=1"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final Cookie cookie = cookies.get(0); Assert.assertEquals(1, cookie.getVersion()); Assert.assertEquals("name1", cookie.getName()); Assert.assertEquals("value2", cookie.getValue()); Assert.assertEquals("localhost.local", cookie.getDomain()); Assert.assertEquals("/", cookie.getPath()); }
@Test public void testInvalidHeader() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE2, "name=value; Version=crap"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertTrue(cookies.isEmpty()); }
@Test public void testCookieRejected() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE2, "name=value; Domain=www.somedomain.com; Version=1"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); final HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); final List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertTrue(cookies.isEmpty()); }
@Test public void testCookiesForConnectRequest() throws Exception { final HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com"); 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(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoCookieStore() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); 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, null); 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(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoCookieSpecRegistry() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); 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, null); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoTargetHost() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, null); 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(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoHttpConnection() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpCoreContext.HTTP_CONNECTION, null); 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(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testAddCookiesUsingExplicitCookieSpec() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final RequestConfig config = RequestConfig.custom() .setCookieSpec(CookieSpecs.NETSCAPE).build(); 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.REQUEST_CONFIG, config); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final CookieSpec cookieSpec = context.getCookieSpec(); Assert.assertTrue(cookieSpec instanceof NetscapeDraftSpec); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(1, headers1.length); Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue()); }
private List<Cookie> parseCookies(String url, String cookieHeader) { CookieOrigin origin = getOrigin(url); BasicHeader header = new BasicHeader(SM.SET_COOKIE, cookieHeader); int attrIndex = 0; do { try { CookieSpec cookieSpec = createSpec(); return cookieSpec.parse(header, origin); } catch (MalformedCookieException e) { int indexOfAttrTitle = cookieHeader.indexOf(COOKIE_ATTRS_NOT_STRICT[attrIndex]); if (indexOfAttrTitle != -1) { cookieHeader = cookieHeader.substring(0,indexOfAttrTitle); header = new BasicHeader(SM.SET_COOKIE, cookieHeader); } attrIndex++; } } while (attrIndex <= COOKIE_ATTRS_NOT_STRICT.length); return emtpyCookieList; }
private List<Cookie> parseCookies(URI uri, List<String> cookieHeaders) { ArrayList<Cookie> cookies = new ArrayList<Cookie>(); int port = (uri.getPort() < 0) ? 80 : uri.getPort(); boolean secure = "https".equals(uri.getScheme()); CookieOrigin origin = new CookieOrigin(uri.getHost(), port, uri.getPath(), secure); for (String cookieHeader : cookieHeaders) { BasicHeader header = new BasicHeader(SM.SET_COOKIE, cookieHeader); try { cookies.addAll(cookieSpec.parse(header, origin)); } catch (MalformedCookieException e) { Log.d("RestPixl",e.toString()); } } return cookies; }
@Override public Header getVersionHeader() { CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append(SM.COOKIE2); buffer.append(": "); buffer.append("$Version="); buffer.append(Integer.toString(getVersion())); return new BufferedHeader(buffer); }
public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException { if (header == null) { throw new IllegalArgumentException("Header may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) { throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'"); } HeaderElement[] elems = header.getElements(); return parse(elems, origin); }
@Override public List<Cookie> parse( final Header header, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(header, "Header"); Args.notNull(origin, "Cookie origin"); if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE2)) { throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'"); } final HeaderElement[] elems = header.getElements(); return createCookies(elems, adjustEffectiveHost(origin)); }
@Override public Header getVersionHeader() { final CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append(SM.COOKIE2); buffer.append(": "); buffer.append("$Version="); buffer.append(Integer.toString(getVersion())); return new BufferedHeader(buffer); }
public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(header, "Header"); Args.notNull(origin, "Cookie origin"); if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) { throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'"); } final HeaderElement[] elems = header.getElements(); return parse(elems, origin); }
public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } final String cookieName = cookie.getName(); final String cookieValue = cookie.getValue(); if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) { BasicHeaderValueFormatterHC4.INSTANCE.formatHeaderElement( buffer, new BasicHeaderElement(cookieName, cookieValue), false); } else { // Netscape style cookies do not support quoted values buffer.append(cookieName); buffer.append("="); if (cookieValue != null) { buffer.append(cookieValue); } } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Override public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } final String cookieName = cookie.getName(); final String cookieValue = cookie.getValue(); if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) { BasicHeaderValueFormatter.INSTANCE.formatHeaderElement( buffer, new BasicHeaderElement(cookieName, cookieValue), false); } else { // Netscape style cookies do not support quoted values buffer.append(cookieName); buffer.append("="); if (cookieValue != null) { buffer.append(cookieValue); } } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { Args.notNull(response, "HTTP request"); Args.notNull(context, "HTTP context"); final HttpClientContext clientContext = HttpClientContext.adapt(context); // Obtain actual CookieSpec instance final CookieSpec cookieSpec = clientContext.getCookieSpec(); if (cookieSpec == null) { this.log.debug("Cookie spec not specified in HTTP context"); return; } // Obtain cookie store final CookieStore cookieStore = clientContext.getCookieStore(); if (cookieStore == null) { this.log.debug("Cookie store not specified in HTTP context"); return; } // Obtain actual CookieOrigin instance final CookieOrigin cookieOrigin = clientContext.getCookieOrigin(); if (cookieOrigin == null) { this.log.debug("Cookie origin not specified in HTTP context"); return; } HeaderIterator it = response.headerIterator(SM.SET_COOKIE); processCookies(it, cookieSpec, cookieOrigin, cookieStore); // see if the cookie spec supports cookie versioning. if (cookieSpec.getVersion() > 0) { // process set-cookie2 headers. // Cookie2 will replace equivalent Cookie instances it = response.headerIterator(SM.SET_COOKIE2); processCookies(it, cookieSpec, cookieOrigin, cookieStore); } }
@Override public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(header, "Header"); Args.notNull(origin, "Cookie origin"); if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) { throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'"); } final HeaderElement[] elems = header.getElements(); return parse(elems, origin); }
@Test public void testAddCookies() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); 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(2, headers1.length); Assert.assertEquals("$Version=1; name1=\"value1\"", headers1[0].getValue()); Assert.assertEquals("$Version=1; name2=\"value2\"", headers1[1].getValue()); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); final CookieOrigin cookieOrigin = context.getCookieOrigin(); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(this.target.getPort(), cookieOrigin.getPort()); Assert.assertEquals("/", cookieOrigin.getPath()); Assert.assertFalse(cookieOrigin.isSecure()); }
@Test public void testExcludeExpiredCookies() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final BasicClientCookie2 cookie3 = new BasicClientCookie2("name3", "value3"); cookie3.setVersion(1); cookie3.setDomain("localhost.local"); cookie3.setPath("/"); cookie3.setExpiryDate(new Date(System.currentTimeMillis() + 100)); this.cookieStore.addCookie(cookie3); Assert.assertEquals(3, this.cookieStore.getCookies().size()); this.cookieStore = Mockito.spy(this.cookieStore); 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); // Make sure the third cookie expires Thread.sleep(200); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(2, headers1.length); Assert.assertEquals("$Version=1; name1=\"value1\"", headers1[0].getValue()); Assert.assertEquals("$Version=1; name2=\"value2\"", headers1[1].getValue()); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); Mockito.verify(this.cookieStore, Mockito.times(1)).clearExpired(Mockito.<Date>any()); }
@Test public void testNoMatchingCookies() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); this.cookieStore.clear(); final BasicClientCookie cookie3 = new BasicClientCookie("name3", "value3"); cookie3.setDomain("www.somedomain.com"); cookie3.setPath("/"); this.cookieStore.addCookie(cookie3); 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(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test // Test for ordering adapted from test in Commons HC 3.1 public void testCookieOrder() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/foobar/yada/yada"); this.cookieStore.clear(); cookieStore.addCookie(makeCookie("nomatch", "value", "localhost.local", "/noway")); cookieStore.addCookie(makeCookie("name2", "value", "localhost.local", "/foobar/yada")); cookieStore.addCookie(makeCookie("name3", "value", "localhost.local", "/foobar")); cookieStore.addCookie(makeCookie("name1", "value", "localhost.local", "/foobar/yada/yada")); 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=value; name2=value; name3=value", headers1[0].getValue()); }