CookieConverter implements Serializable { Cookie[] convertCookies(HttpEntity<?> httpEntity) { final List<Cookie> cookies = new LinkedList<>(); List<String> inboundCookies = httpEntity.getHeaders().get(HttpHeaders.SET_COOKIE); if (inboundCookies != null) { for (String cookieString : inboundCookies) { Cookie cookie = convertCookieString(cookieString); cookies.add(cookie); } } return cookies.toArray(new Cookie[0]); }    }
@Test public void testCookiesAreParsedCorrectly(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals("foo", cookies[0].getName()); assertEquals("bar", cookies[0].getValue()); }
@Test public void testAdditionalCookieDirectivesAreDiscarded(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;HttpOnly")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals("foo", cookies[0].getName()); assertEquals("bar", cookies[0].getValue()); }
@Test public void testCookieCommentIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;Comment=wtf")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals("wtf", cookies[0].getComment()); }
@Test public void testCookieDomainIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;Domain=whatever")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals("whatever", cookies[0].getDomain()); }
@Test public void testCookieMaxAgeIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;Max-Age=42")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals(42, cookies[0].getMaxAge()); }
@Test public void testCookiePathIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;Path=foobar")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals("foobar", cookies[0].getPath()); }
@Test public void testCookieSecureIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;Secure")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertTrue(cookies[0].getSecure()); }
@Test public void testCookieVersionIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;Version=1")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertEquals(1, cookies[0].getVersion()); }
@Test public void testCookieHttpOnlyIsPreserved(){ cookieHeaders.put("Set-Cookie", Collections.singletonList("foo=bar;HttpOnly")); final ResponseEntity<?> responseEntity = new ResponseEntity<>(cookieHeaders, HttpStatus.OK); final Cookie[] cookies = cookieConverter.convertCookies(responseEntity); assertTrue(cookies[0].isHttpOnly()); }