@Test public void testInvalidInput() throws Exception { CookieSpec cookiespec = new BrowserCompatSpec(); try { cookiespec.parse(null, null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { cookiespec.parse(new BasicHeader("Set-Cookie", "name=value"), null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { cookiespec.validate(null, null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { cookiespec.validate(new BasicClientCookie("name", null), null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { cookiespec.match(null, null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { cookiespec.match(new BasicClientCookie("name", null), null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { cookiespec.formatCookies(null); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } try { List<Cookie> cookies = new ArrayList<Cookie>(); cookiespec.formatCookies(cookies); Assert.fail("IllegalArgumentException must have been thrown"); } catch (IllegalArgumentException ex) { // expected } }
/** Tests generic cookie formatting. */ @Test public void testGenericCookieFormatting() throws Exception { Header header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.mydomain.com"); CookieSpec cookiespec = new BrowserCompatSpec(); CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false); List<Cookie> cookies = cookiespec.parse(header, origin); cookiespec.validate(cookies.get(0), origin); List<Header> headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("name=value", headers.get(0).getValue()); }
/** Tests generic cookie formatting. */ @Test public void testFormatSeveralCookies() throws Exception { Header header = new BasicHeader( "Set-Cookie", "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com; version=0"); CookieSpec cookiespec = new BrowserCompatSpec(); CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false); List<Cookie> cookies = cookiespec.parse(header, origin); List<Header> headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("name1=value1; name2=value2", headers.get(0).getValue()); }
/** Tests if null cookie values are handled correctly. */ @Test public void testNullCookieValueFormatting() { BasicClientCookie cookie = new BasicClientCookie("name", null); cookie.setDomain(".whatever.com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain()); cookie.setPath("/"); cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath()); CookieSpec cookiespec = new BrowserCompatSpec(); List<Cookie> cookies = new ArrayList<Cookie>(1); cookies.add(cookie); List<Header> headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("name=", headers.get(0).getValue()); }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue); cookie.setVersion(0); cookie.setPath("/"); cookie.setDomain(jetty.getBaseUrl().getHost()); CookieStore cookieStore = new BasicCookieStore(); CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute(ClientContext.COOKIESPEC_REGISTRY); String policy = HttpClientParams.getCookiePolicy(request.getParams()); CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams()); // Add the cookies to the request List<Header> headers = cookieSpec.formatCookies(Collections.singletonList(cookie)); for (Header header : headers) { request.addHeader(header); } context.setAttribute(ClientContext.COOKIE_STORE, cookieStore); context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec); }