ResponseProcessCookies implements HttpResponseInterceptor { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { if (response == null) { throw new IllegalArgumentException("HTTP request may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } CookieSpec cookieSpec = (CookieSpec) context.getAttribute( ClientContext.COOKIE_SPEC); if (cookieSpec == null) { this.log.debug("Cookie spec not specified in HTTP context"); return; } CookieStore cookieStore = (CookieStore) context.getAttribute( ClientContext.COOKIE_STORE); if (cookieStore == null) { this.log.debug("Cookie store not specified in HTTP context"); return; } CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( ClientContext.COOKIE_ORIGIN); 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); if (cookieSpec.getVersion() > 0) { it = response.headerIterator(SM.SET_COOKIE2); processCookies(it, cookieSpec, cookieOrigin, cookieStore); } } ResponseProcessCookies(); void process(final HttpResponse response, final HttpContext context);  }
@Test(expected=IllegalArgumentException.class) public void testResponseParameterCheck() throws Exception { HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(null, context); }
@Test(expected=IllegalArgumentException.class) public void testContextParameterCheck() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, null); }
@Test public void testParseCookies() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(ClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); 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 { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, null); context.setAttribute(ClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(0, cookies.size()); }
@Test public void testNoCookieSpec() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(ClientContext.COOKIE_SPEC, null); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(0, cookies.size()); }
@Test public void testNoCookieStore() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE, "name1=value1"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(ClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(ClientContext.COOKIE_STORE, null); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(0, cookies.size()); }
@Test public void testSetCookie2OverrideSetCookie() throws Exception { 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"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(ClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); 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 { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE2, "name=value; Version=crap"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(ClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertTrue(cookies.isEmpty()); }
@Test public void testCookieRejected() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.addHeader(SM.SET_COOKIE2, "name=value; Domain=www.somedomain.com; Version=1"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_ORIGIN, this.cookieOrigin); context.setAttribute(ClientContext.COOKIE_SPEC, this.cookieSpec); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); HttpResponseInterceptor interceptor = new ResponseProcessCookies(); interceptor.process(response, context); List<Cookie> cookies = this.cookieStore.getCookies(); Assert.assertNotNull(cookies); Assert.assertTrue(cookies.isEmpty()); }