use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.
the class TestHC4CookieManager method testCookieOrdering2.
@Test
public void testCookieOrdering2() throws Exception {
URL url = new URL("http://order.now/sub1/moo.html");
man.addCookieFromHeader("test1=moo1;", url);
man.addCookieFromHeader("test2=moo2;path=/sub1", url);
man.addCookieFromHeader("test2=moo3;path=/", url);
assertEquals(3, man.getCookieCount());
// Defaults to caller URL
assertEquals("/sub1", man.get(0).getPath());
assertEquals("/sub1", man.get(1).getPath());
assertEquals("/", man.get(2).getPath());
String s = man.getCookieHeaderForURL(url);
assertNotNull(s);
HC4CookieHandler cookieHandler = (HC4CookieHandler) man.getCookieHandler();
List<org.apache.http.cookie.Cookie> c = cookieHandler.getCookiesForUrl(man.getCookies(), url, CookieManager.ALLOW_VARIABLE_COOKIES);
assertEquals("/sub1", c.get(0).getPath());
assertFalse(((BasicClientCookie) c.get(0)).containsAttribute(ClientCookie.PATH_ATTR));
assertEquals("/sub1", c.get(1).getPath());
assertTrue(((BasicClientCookie) c.get(1)).containsAttribute(ClientCookie.PATH_ATTR));
assertEquals("/", c.get(2).getPath());
assertEquals("test1=moo1; test2=moo2; test2=moo3", s);
}
use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.
the class TestHC4CookieManager method testCookiePolicy2109.
// test of deprecated item
@SuppressWarnings("deprecation")
@Test
public void testCookiePolicy2109() throws Exception {
man.setCookiePolicy(org.apache.http.client.params.CookiePolicy.RFC_2109);
// ensure policy is picked up
man.testStarted();
URL url = new URL("http://order.now/sub1/moo.html");
man.addCookieFromHeader("test1=moo1;", url);
man.addCookieFromHeader("test2=moo2;path=/sub1", url);
man.addCookieFromHeader("test2=moo3;path=/", url);
assertEquals(3, man.getCookieCount());
assertEquals("/sub1", man.get(0).getPath());
assertEquals("/sub1", man.get(1).getPath());
assertEquals("/", man.get(2).getPath());
String s = man.getCookieHeaderForURL(url);
assertNotNull(s);
HC4CookieHandler cookieHandler = (HC4CookieHandler) man.getCookieHandler();
List<org.apache.http.cookie.Cookie> c = cookieHandler.getCookiesForUrl(man.getCookies(), url, CookieManager.ALLOW_VARIABLE_COOKIES);
assertEquals("/sub1", c.get(0).getPath());
assertFalse(((BasicClientCookie) c.get(0)).containsAttribute(ClientCookie.PATH_ATTR));
assertEquals("/sub1", c.get(1).getPath());
assertTrue(((BasicClientCookie) c.get(1)).containsAttribute(ClientCookie.PATH_ATTR));
assertEquals("/", c.get(2).getPath());
assertTrue(((BasicClientCookie) c.get(2)).containsAttribute(ClientCookie.PATH_ATTR));
assertEquals("$Version=0; test1=moo1; test2=moo2; $Path=/sub1; test2=moo3; $Path=/", s);
}
use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.
the class TestHC4CookieManager method testAddCookieFromHeaderWithWildcard.
@Test
public void testAddCookieFromHeaderWithWildcard() throws Exception {
URL url = new URL("https://subdomain.bt.com/page");
String headerLine = "SMTRYNO=1; path=/; domain=.bt.com";
man.addCookieFromHeader(headerLine, url);
Assert.assertEquals(1, man.getCookieCount());
HC4CookieHandler cookieHandler = (HC4CookieHandler) man.getCookieHandler();
List<org.apache.http.cookie.Cookie> cookies = cookieHandler.getCookiesForUrl(man.getCookies(), url, CookieManager.ALLOW_VARIABLE_COOKIES);
for (org.apache.http.cookie.Cookie cookie : cookies) {
// See http://tools.ietf.org/html/rfc6265#section-5.2.3
Assert.assertEquals("bt.com", cookie.getDomain());
Assert.assertTrue(((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR));
}
// we check that CookieManager returns the cookies for the main domain
URL urlMainDomain = new URL("https://www.bt.com/page");
cookies = cookieHandler.getCookiesForUrl(man.getCookies(), urlMainDomain, CookieManager.ALLOW_VARIABLE_COOKIES);
Assert.assertEquals(1, cookies.size());
for (org.apache.http.cookie.Cookie cookie : cookies) {
// See http://tools.ietf.org/html/rfc6265#section-5.2.3
Assert.assertEquals("bt.com", cookie.getDomain());
Assert.assertTrue(((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR));
}
}
use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.
the class HC4CookieHandler method addCookieFromHeader.
@Override
@SuppressWarnings("JdkObsolete")
public void addCookieFromHeader(CookieManager cookieManager, boolean checkCookies, String cookieHeader, URL url) {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("Received Cookie: {} From: {}", cookieHeader, url.toExternalForm());
}
String protocol = url.getProtocol();
String host = url.getHost();
int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort());
String path = url.getPath();
boolean isSecure = HTTPSamplerBase.isSecure(protocol);
List<org.apache.http.cookie.Cookie> cookies = null;
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure);
BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader);
try {
cookies = cookieSpec.parse(basicHeader, cookieOrigin);
} catch (MalformedCookieException e) {
log.error("Unable to add the cookie", e);
}
if (cookies == null) {
return;
}
for (org.apache.http.cookie.Cookie cookie : cookies) {
try {
if (checkCookies) {
try {
cookieSpec.validate(cookie, cookieOrigin);
} catch (MalformedCookieException e) {
// This means the cookie was wrong for the URL
log.info("Not storing invalid cookie: <{}> for URL {} ({})", cookieHeader, url, e.getLocalizedMessage());
continue;
}
}
Date expiryDate = cookie.getExpiryDate();
long exp = 0;
if (expiryDate != null) {
exp = expiryDate.getTime();
}
Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.isSecure(), exp / 1000, ((BasicClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR), ((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR), cookie.getVersion());
// Store session cookies as well as unexpired ones
if (exp == 0 || exp >= System.currentTimeMillis()) {
// Has its own debug log; removes matching cookies
cookieManager.add(newCookie);
} else {
cookieManager.removeMatchingCookies(newCookie);
if (debugEnabled) {
log.info("Dropping expired Cookie: {}", newCookie);
}
}
} catch (IllegalArgumentException e) {
log.warn(cookieHeader + e.getLocalizedMessage());
}
}
}
use of org.apache.http.impl.cookie.BasicClientCookie in project wildfly by wildfly.
the class DefaultCookieVersionTestCase method commonSendCookieVersion.
private void commonSendCookieVersion(int cookieVersion) throws IOException, URISyntaxException {
configureDefaultCookieVersion(cookieVersion);
BasicCookieStore basicCookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("testCookie", "testCookieValue");
cookie.setVersion(cookieVersion);
cookie.setDomain(cookieURL.getHost());
basicCookieStore.addCookie(cookie);
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultCookieStore(basicCookieStore).build()) {
HttpResponse response = httpclient.execute(new HttpGet(cookieURL.toURI() + "CookieEchoServlet"));
if (response.getEntity() != null) {
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(cookieVersion + "", EntityUtils.toString(response.getEntity()));
}
}
}
Aggregations