Search in sources :

Example 6 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainValidate2.

@Test
public void testDomainValidate2() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain(".somedomain.com");
    h.validate(cookie, origin);
    cookie.setDomain(".otherdomain.com");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
    // expected
    }
    cookie.setDomain("www.otherdomain.com");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
    // expected
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 7 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainValidate3.

@Test
public void testDomainValidate3() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain(".a.com");
    h.validate(cookie, origin);
    cookie.setDomain(".com");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
    // expected
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 8 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project spring-framework by spring-projects.

the class MockWebResponseBuilder method createCookie.

static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) {
    Date expires = null;
    if (cookie.getMaxAge() > -1) {
        expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
    }
    BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
    result.setDomain(cookie.getDomain());
    result.setComment(cookie.getComment());
    result.setExpiryDate(expires);
    result.setPath(cookie.getPath());
    result.setSecure(cookie.getSecure());
    if (cookie.isHttpOnly()) {
        result.setAttribute("httponly", "true");
    }
    return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Cookie(javax.servlet.http.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Date(java.util.Date)

Example 9 with BasicClientCookie

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

the class ITMDCFilter method testWihCustomData.

@Test
public void testWihCustomData() throws Exception {
    RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
    //Create test config via servlet
    executor.execute(rb.buildGetRequest("/mdc", "createTestConfig", "true"));
    TimeUnit.SECONDS.sleep(1);
    //Pass custom cookie
    BasicClientCookie cookie = new BasicClientCookie("mdc-test-cookie", "foo-test-cookie");
    cookie.setPath("/");
    cookie.setDomain("localhost");
    httpClient.getCookieStore().addCookie(cookie);
    //Execute request
    RequestExecutor result = executor.execute(rb.buildGetRequest("/mdc", "mdc-test-param", "foo-test-param", "ignored-param", "ignored-value").withHeader("X-Forwarded-For", "foo-forwarded-for").withHeader("mdc-test-header", "foo-test-header"));
    JsonObject jb = Json.createReader(new StringReader(result.getContent())).readObject();
    log.info("Response  {}", result.getContent());
    assertEquals("/mdc", jb.getString("req.requestURI"));
    assertEquals(ServerConfiguration.getServerUrl() + "/mdc", jb.getString("req.requestURL"));
    assertEquals("foo-forwarded-for", jb.getString("req.xForwardedFor"));
    assertEquals("foo-test-header", jb.getString("mdc-test-header"));
    assertEquals("foo-test-param", jb.getString("mdc-test-param"));
    assertEquals("foo-test-cookie", jb.getString("mdc-test-cookie"));
    //Only configured params must be returned
    assertFalse(jb.containsKey("ignored-param"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Example 10 with BasicClientCookie

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

the class HTTPEngine4Impl method addCookie.

public static void addCookie(CookieStore cookieStore, String domain, String name, String value, String path, String charset) {
    if (ReqRspUtil.needEncoding(name, false))
        name = ReqRspUtil.encode(name, charset);
    if (ReqRspUtil.needEncoding(value, false))
        value = ReqRspUtil.encode(value, charset);
    BasicClientCookie cookie = new BasicClientCookie(name, value);
    if (!StringUtil.isEmpty(domain, true))
        cookie.setDomain(domain);
    if (!StringUtil.isEmpty(path, true))
        cookie.setPath(path);
    cookieStore.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