Search in sources :

Example 26 with BasicClientCookie

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);
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) ClientCookie(org.apache.http.cookie.ClientCookie) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 27 with BasicClientCookie

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);
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) ClientCookie(org.apache.http.cookie.ClientCookie) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 28 with BasicClientCookie

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));
    }
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) ClientCookie(org.apache.http.cookie.ClientCookie) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 29 with BasicClientCookie

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());
        }
    }
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) Date(java.util.Date) CookieOrigin(org.apache.http.cookie.CookieOrigin) BasicHeader(org.apache.http.message.BasicHeader)

Example 30 with BasicClientCookie

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()));
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Aggregations

BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)88 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)36 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)19 Cookie (org.apache.http.cookie.Cookie)17 HttpClient (org.apache.http.client.HttpClient)15 HttpGet (org.apache.http.client.methods.HttpGet)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)15 Map (java.util.Map)14 RequestConfig (org.apache.http.client.config.RequestConfig)13 HttpPost (org.apache.http.client.methods.HttpPost)9 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)9 URL (java.net.URL)8 HttpEntity (org.apache.http.HttpEntity)8 CookieStore (org.apache.http.client.CookieStore)8 IOException (java.io.IOException)7 Date (java.util.Date)7 ClientCookie (org.apache.http.cookie.ClientCookie)7 HashMap (java.util.HashMap)6