GetCookieFromRequestRule implements IResolveDependencyStrategy { @Override public <T> T resolve(final Object... args) { String cookieString = ((FullHttpRequest) args[0]).headers().get(HttpHeaders.Names.COOKIE); if (cookieString == null) { return null; } String[] cookie = Stream.of(cookieString.split(";")) .map(x -> x.split("=")) .filter(x -> x[0].trim().equals(((String) args[1]))) .findFirst().orElse(null); if (cookie == null || cookie.length == 1) { return null; } return (T) cookie[1]; }  @Override T resolve(final Object... args);  }
@Test public void shouldExtractRightCookie() { GetCookieFromRequestRule rule = new GetCookieFromRequestRule(); FullHttpRequest request = mock(FullHttpRequest.class); HttpHeaders headers = mock(HttpHeaders.class); when(request.headers()).thenReturn(headers); when(headers.get(eq("Cookie"))).thenReturn("key1=val1;key2=val2"); assertEquals("val2", rule.resolve(request, "key2")); }
@Test public void shouldReturnNullIfCookieIsNotFound() { GetCookieFromRequestRule rule = new GetCookieFromRequestRule(); FullHttpRequest request = mock(FullHttpRequest.class); HttpHeaders headers = mock(HttpHeaders.class); when(request.headers()).thenReturn(headers); when(headers.get(eq("Cookie"))).thenReturn("key1=val1;key2=val2"); assertNull(rule.resolve(request, "key3")); }
@Test public void shouldReturnNullIfCookiesIsEmpty() { GetCookieFromRequestRule rule = new GetCookieFromRequestRule(); FullHttpRequest request = mock(FullHttpRequest.class); HttpHeaders headers = mock(HttpHeaders.class); when(request.headers()).thenReturn(headers); when(headers.get(eq("Cookie"))).thenReturn(null); assertNull(rule.resolve(request, "key3")); }