Search in sources :

Example 71 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.

the class TestHC4CookieManager method testCookiePolicyNetscape.

@Test
public void testCookiePolicyNetscape() throws Exception {
    man.setCookiePolicy(CookieSpecs.NETSCAPE);
    // ensure policy is picked up
    man.testStarted();
    URL url = new URL("http://www.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("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 72 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.

the class TestHC4CookieManager method testAddCookieFromHeaderWithNoWildcard.

@Test
public void testAddCookieFromHeaderWithNoWildcard() throws Exception {
    URL url = new URL("https://subdomain.bt.com/page");
    String headerLine = "SMTRYNO=1; path=/";
    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);
    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("subdomain.bt.com", cookie.getDomain());
        Assert.assertFalse(((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(0, cookies.size());
}
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 73 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project jmeter by apache.

the class HC4CookieHandler method makeCookie.

/**
 * Create an HttpClient cookie from a JMeter cookie
 */
@SuppressWarnings("JdkObsolete")
private org.apache.http.cookie.Cookie makeCookie(Cookie jmc) {
    long exp = jmc.getExpiresMillis();
    BasicClientCookie ret = new BasicClientCookie(jmc.getName(), jmc.getValue());
    ret.setDomain(jmc.getDomain());
    ret.setPath(jmc.getPath());
    // use null for no expiry
    ret.setExpiryDate(exp > 0 ? new Date(exp) : null);
    ret.setSecure(jmc.getSecure());
    ret.setVersion(jmc.getVersion());
    if (jmc.isDomainSpecified()) {
        ret.setAttribute(ClientCookie.DOMAIN_ATTR, jmc.getDomain());
    }
    if (jmc.isPathSpecified()) {
        ret.setAttribute(ClientCookie.PATH_ATTR, jmc.getPath());
    }
    return ret;
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Date(java.util.Date)

Example 74 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class HttpClientUtil method newHttpState.

public static BasicCookieStore newHttpState(ZAuthToken authToken, String host, boolean isAdmin) {
    BasicCookieStore cookieStore = new BasicCookieStore();
    if (authToken != null) {
        Map<String, String> cookieMap = authToken.cookieMap(isAdmin);
        if (cookieMap != null) {
            for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
                BasicClientCookie cookie = new BasicClientCookie(ck.getKey(), ck.getValue());
                cookie.setDomain(host);
                cookie.setPath("/");
                cookie.setSecure(false);
                cookieStore.addCookie(cookie);
            }
        }
    }
    return cookieStore;
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Map(java.util.Map)

Example 75 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class ZimbraAuthToken method encode.

@Override
public void encode(BasicCookieStore state, boolean isAdminReq, String cookieDomain) throws ServiceException {
    String origAuthData = AuthTokenUtil.getOrigAuthData(this);
    BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(isAdminReq), origAuthData);
    cookie.setDomain(cookieDomain);
    cookie.setPath("/");
    cookie.setSecure(false);
    state.addCookie(cookie);
}
Also used : 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