### Question:
Cookies { public void set(HttpCookie cookie) { if (getHeader(HttpHeaders.COOKIE).isEmpty()) { request.setHeader(new BasicHeader(HttpHeaders.COOKIE, cookie.toString())); } else { Header cookieHeader = getHeader(HttpHeaders.COOKIE).get(0); List<HttpCookie> cookieList = getAllCookiesFromHeader(cookieHeader); cookieList = cookieList.stream() .filter(c -> !c.getName().equalsIgnoreCase(cookie.getName())) .collect(Collectors.toList()); cookieList.add(cookie); request.setHeader(getCookieHeader(cookieList)); } } private  Cookies(HttpRequest request); static Cookies of(HttpRequest request); List<HttpCookie> getAll(); List<HttpCookie> get(String needle); void set(HttpCookie cookie); void remove(String cookie);  }### Answer:
@Test void givenRequestWithoutCookieHeader_whenSetCookie_thenCreatesCookieHeaderAndCookie() { doReturn(new Header[] {}).when(request).getHeaders(HttpHeaders.COOKIE); ArgumentCaptor<Header> argument = ArgumentCaptor.forClass(Header.class); HttpCookie newCookie = new HttpCookie("cookie1", "cookie1"); newCookie.setVersion(0); underTest.set(newCookie); verify(request).setHeader(argument.capture()); assertThat(argument.getValue().toString(), is("Cookie: cookie1=cookie1")); }
@Test void givenRequestWithCookieHeader_whenSetCookie_thenCreatesOrOverwritesCookie() { doReturn(new Header[] { cookieSingle}).when(request).getHeaders(HttpHeaders.COOKIE); ArgumentCaptor<Header> argument = ArgumentCaptor.forClass(Header.class); HttpCookie newCookie = new HttpCookie("cookie1", "cookie1"); newCookie.setVersion(0); underTest.set(newCookie); verify(request).setHeader(argument.capture()); assertThat(argument.getValue().toString(), is("Cookie: cookie=cookie;cookie1=cookie1")); }