use of io.servicetalk.http.api.DefaultHttpCookiePair in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method headerCookieRemovalAndIteration.
private void headerCookieRemovalAndIteration(HttpHeaders headers) {
// Single COOKIE header entry with duplicate cookie names.
headers.add(COOKIE, "name1=value1; name2=value2; name1=value3");
assertEquals(new DefaultHttpCookiePair("name1", "value1"), headers.getCookie("name1"));
assertEquals(new DefaultHttpCookiePair("name2", "value2"), headers.getCookie("name2"));
assertIteratorHasItems(headers.getCookiesIterator(), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name2", "value2"), new DefaultHttpCookiePair("name1", "value3"));
assertIteratorHasItems(headers.getCookiesIterator("name1"), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name1", "value3"));
assertIteratorHasItems(headers.getCookiesIterator("name2"), new DefaultHttpCookiePair("name2", "value2"));
assertTrue(headers.removeCookies("name1"));
assertEmptyIterator(headers.getCookiesIterator("name1"));
assertIteratorHasItems(headers.getCookiesIterator("name2"), new DefaultHttpCookiePair("name2", "value2"));
assertTrue(headers.removeCookies("name2"));
assertEmptyIterator(headers.getCookiesIterator("name1"));
assertEmptyIterator(headers.getCookiesIterator("name2"));
assertEmptyIterator(headers.valuesIterator(COOKIE));
// Simulate the same behavior as above, but with addCookie
headers.addCookie("name1", "value1");
headers.addCookie("name2", "value2");
headers.addCookie("name1", "value3");
// data structure and will not be fixed for 4.1.
if (h2PriorKnowledge) {
assertEquals(new DefaultHttpCookiePair("name1", "value3"), headers.getCookie("name1"));
} else {
assertEquals(new DefaultHttpCookiePair("name1", "value1"), headers.getCookie("name1"));
}
assertEquals(new DefaultHttpCookiePair("name2", "value2"), headers.getCookie("name2"));
assertIteratorHasItems(headers.getCookiesIterator(), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name2", "value2"), new DefaultHttpCookiePair("name1", "value3"));
assertIteratorHasItems(headers.getCookiesIterator("name1"), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name1", "value3"));
assertIteratorHasItems(headers.getCookiesIterator("name2"), new DefaultHttpCookiePair("name2", "value2"));
assertTrue(headers.removeCookies("name1"));
assertEmptyIterator(headers.getCookiesIterator("name1"));
assertIteratorHasItems(headers.getCookiesIterator("name2"), new DefaultHttpCookiePair("name2", "value2"));
assertTrue(headers.removeCookies("name2"));
assertEmptyIterator(headers.getCookiesIterator("name1"));
assertEmptyIterator(headers.getCookiesIterator("name2"));
assertEmptyIterator(headers.valuesIterator(COOKIE));
// Split headers across 2 header entries, with duplicate cookie names.
headers.add(COOKIE, "name1=value1; name2=value2; name1=value3");
headers.add(COOKIE, "name2=value4; name1=value5; name3=value6");
if (h2PriorKnowledge) {
assertEquals(new DefaultHttpCookiePair("name1", "value5"), headers.getCookie("name1"));
assertEquals(new DefaultHttpCookiePair("name2", "value4"), headers.getCookie("name2"));
assertEquals(new DefaultHttpCookiePair("name3", "value6"), headers.getCookie("name3"));
} else {
assertEquals(new DefaultHttpCookiePair("name1", "value1"), headers.getCookie("name1"));
assertEquals(new DefaultHttpCookiePair("name2", "value2"), headers.getCookie("name2"));
assertEquals(new DefaultHttpCookiePair("name3", "value6"), headers.getCookie("name3"));
}
assertIteratorHasItems(headers.getCookiesIterator(), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name2", "value2"), new DefaultHttpCookiePair("name1", "value3"), new DefaultHttpCookiePair("name2", "value4"), new DefaultHttpCookiePair("name1", "value5"), new DefaultHttpCookiePair("name3", "value6"));
assertIteratorHasItems(headers.getCookiesIterator("name1"), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name1", "value3"), new DefaultHttpCookiePair("name1", "value5"));
assertIteratorHasItems(headers.getCookiesIterator("name2"), new DefaultHttpCookiePair("name2", "value2"), new DefaultHttpCookiePair("name2", "value4"));
assertIteratorHasItems(headers.getCookiesIterator("name3"), new DefaultHttpCookiePair("name3", "value6"));
assertTrue(headers.removeCookies("name2"));
assertIteratorHasItems(headers.getCookiesIterator("name1"), new DefaultHttpCookiePair("name1", "value1"), new DefaultHttpCookiePair("name1", "value3"), new DefaultHttpCookiePair("name1", "value5"));
assertEmptyIterator(headers.getCookiesIterator("name2"));
assertIteratorHasItems(headers.getCookiesIterator("name3"), new DefaultHttpCookiePair("name3", "value6"));
assertTrue(headers.removeCookies("name1"));
assertEmptyIterator(headers.getCookiesIterator("name1"));
assertEmptyIterator(headers.getCookiesIterator("name2"));
assertIteratorHasItems(headers.getCookiesIterator("name3"), new DefaultHttpCookiePair("name3", "value6"));
assertTrue(headers.removeCookies("name3"));
assertEmptyIterator(headers.getCookiesIterator("name1"));
assertEmptyIterator(headers.getCookiesIterator("name2"));
assertEmptyIterator(headers.getCookiesIterator("name3"));
assertEmptyIterator(headers.valuesIterator(COOKIE));
// Test partial name matches don't inadvertently match.
headers.add(COOKIE, "foo=bar");
assertEquals(new DefaultHttpCookiePair("foo", "bar"), headers.getCookie("foo"));
assertNull(headers.getCookie("baz"));
assertNull(headers.getCookie("foo="));
assertNull(headers.getCookie("fo"));
assertNull(headers.getCookie("f"));
assertFalse(headers.removeCookies("foo="));
assertFalse(headers.removeCookies("fo"));
assertFalse(headers.removeCookies("f"));
assertEquals(new DefaultHttpCookiePair("foo", "bar"), headers.getCookie("foo"));
assertEmptyIterator(headers.getCookiesIterator("foo="));
assertEmptyIterator(headers.getCookiesIterator("fo"));
assertEmptyIterator(headers.getCookiesIterator("f"));
assertTrue(headers.removeCookies("foo"));
assertNull(headers.getCookie("foo"));
assertEmptyIterator(headers.getCookiesIterator("foo"));
assertEmptyIterator(headers.valuesIterator(COOKIE));
}
use of io.servicetalk.http.api.DefaultHttpCookiePair in project servicetalk by apple.
the class H2ToStH1UtilsTest method testH1HeadersSplitCookieCrumbs.
void testH1HeadersSplitCookieCrumbs(HttpHeadersFactory headersFactory, CharSequence secondHeaderName) {
HttpHeaders headers = headersFactory.newHeaders();
// Add two headers which will be saved in the same entries[index]:
headers.add(COOKIE, "a=b; c=d; e=f");
String secondHeaderValue = "some-value";
headers.add(secondHeaderName, secondHeaderValue);
h1HeadersSplitCookieCrumbs(headers);
List<HttpCookiePair> cookies = new ArrayList<>();
for (HttpCookiePair pair : headers.getCookies()) {
cookies.add(pair);
}
assertThat(cookies, hasSize(3));
assertThat(cookies, containsInAnyOrder(new DefaultHttpCookiePair("a", "b"), new DefaultHttpCookiePair("c", "d"), new DefaultHttpCookiePair("e", "f")));
assertThat(headers.get(secondHeaderName), equalTo(secondHeaderValue));
}
Aggregations