Example usage for org.apache.commons.httpclient Cookie Cookie

List of usage examples for org.apache.commons.httpclient Cookie Cookie

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Cookie Cookie.

Prototype

public Cookie(String paramString1, String paramString2, String paramString3) 

Source Link

Usage

From source file:org.mule.module.jersey.JerseyCookiePropagationTestCase.java

@Test
public void testJerseyCookiePropagation() throws Exception {
    HelloWorldCookieResource jerseyResource = muleContext.getRegistry().get("jerseyComponent");

    Map<String, Object> props = new HashMap<String, Object>();

    MuleMessage result;/*from   w  w w . j  a v  a2s . com*/
    if (useOldTransport) {
        Cookie[] cookiesObject = new Cookie[] { new Cookie(null, TEST_COOKIE_NAME, TEST_COOKIE_VALUE) };
        props.put(HttpConnector.HTTP_COOKIES_PROPERTY, cookiesObject);
        props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_POST);

        result = muleContext.getClient().send("http://localhost:" + httpPort.getNumber() + "/helloworld",
                getTestMessage(props));
    } else {
        props.put("Cookie", String.format("%s=%s", TEST_COOKIE_NAME, TEST_COOKIE_VALUE));
        final HttpRequestOptions httpRequestOptions = newOptions()
                .method(org.mule.module.http.api.HttpConstants.Methods.POST.name()).build();
        result = muleContext.getClient().send("http://localhost:" + httpPort.getNumber() + "/helloworld",
                getTestMessage(props), httpRequestOptions);
    }

    assertThat(HttpStatus.SC_OK, is(equalTo(result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0))));

    Map<String, javax.ws.rs.core.Cookie> cookies = jerseyResource.getCookies();
    javax.ws.rs.core.Cookie jerseyCookie = jerseyResource.getCookies().get(TEST_COOKIE_NAME);

    assertThat(1, is(equalTo(cookies.size())));
    assertThat(TEST_COOKIE_NAME, is(equalTo(jerseyCookie.getName())));
    assertThat(TEST_COOKIE_VALUE, is(equalTo(jerseyCookie.getValue())));
    assertThat(TEST_COOKIE_VALUE, is(equalTo(jerseyResource.getTestCookie())));
}

From source file:org.mule.transport.http.components.HttpResponseBuilderTestCase.java

@Test
public void testHttpResponseCopyOutboundProperties() throws Exception {
    HttpResponseBuilder httpResponseBuilder = createHttpResponseBuilder();
    Map<String, Object> outboundProperties = new HashMap<String, Object>();
    outboundProperties.put(HttpConstants.HEADER_AGE, "12");
    outboundProperties.put(HttpConstants.HEADER_CACHE_CONTROL, "max-age=3600");
    outboundProperties.put(MuleProperties.MULE_ENCODING_PROPERTY, "UTF-8");
    Cookie[] cookies = new Cookie[2];
    cookies[0] = new Cookie(null, "clientId", "2");
    cookies[1] = new Cookie(null, "category", "premium");
    outboundProperties.put(HttpConstants.HEADER_COOKIE_SET, cookies);

    Set<String> propertyNames = outboundProperties.keySet();
    when(mockMuleMessage.getOutboundPropertyNames()).thenReturn(propertyNames);
    for (String propertyName : propertyNames) {
        when(mockMuleMessage.getOutboundProperty(propertyName))
                .thenReturn(outboundProperties.get(propertyName));
    }/*  w  w  w  .  j  a v  a2s .  c  om*/

    HttpResponse response = new HttpResponse();
    httpResponseBuilder.copyOutboundProperties(response, mockMuleMessage);

    Header[] headers = response.getHeaders();
    for (Header header : headers) {
        if (HttpConstants.HEADER_COOKIE_SET.equals(header.getName())) {
            if (header.getValue().startsWith(cookies[0].getName())) {
                assertEquals(cookies[0].toString(), header.getValue());
            } else {
                assertEquals(cookies[1].toString(), header.getValue());
            }

        } else if (header.getName().startsWith(HttpConstants.CUSTOM_HEADER_PREFIX)) {
            assertEquals(
                    outboundProperties
                            .get(header.getName().substring(HttpConstants.CUSTOM_HEADER_PREFIX.length())),
                    header.getValue());
        } else {
            assertEquals(outboundProperties.get(header.getName()), header.getValue());
        }
    }
}

From source file:org.mule.transport.http.CookieHelperTestCase.java

@Test
public void testPutAndMergeCookieObjectMapOfStringString_CookiesInArray_NewCookiesInMap() {
    Cookie[] cookiesObject = new Cookie[] { new Cookie(null, COOKIE_1_NAME, COOKIE_1_ORIGINAL_VALUE) };

    Map<String, String> newCookiesMap = new HashMap<String, String>();
    newCookiesMap.put(COOKIE_1_NAME, COOKIE_1_NEW_VALUE);
    newCookiesMap.put(COOKIE_2_NAME, COOKIE_2_VALUE);

    cookiesObject = (Cookie[]) CookieHelper.putAndMergeCookie(cookiesObject, newCookiesMap);

    assertEquals(2, cookiesObject.length);

    assertEquals(COOKIE_1_NAME, cookiesObject[0].getName());
    assertEquals(COOKIE_1_NEW_VALUE, cookiesObject[0].getValue());

    assertEquals(COOKIE_2_NAME, cookiesObject[1].getName());
    assertEquals(COOKIE_2_VALUE, cookiesObject[1].getValue());

    Cookie[] unModifiedCookiesObject = (Cookie[]) CookieHelper.putAndMergeCookie(cookiesObject,
            (Map<String, String>) null);
    assertSame(cookiesObject, unModifiedCookiesObject);
    assertEquals(2, cookiesObject.length);
}

From source file:org.mule.transport.http.CookieHelperTestCase.java

@Test
@SuppressWarnings("unchecked")
public void testPutAndMergeCookieObjectCookieArray_CookiesInMap_NewCookiesInArray() {
    Map<String, String> cookiesObject = new HashMap<String, String>();
    cookiesObject.put(COOKIE_1_NAME, COOKIE_1_ORIGINAL_VALUE);

    assertEquals(1, cookiesObject.size());

    Cookie[] newCookiesArray = new Cookie[] { new Cookie(null, COOKIE_1_NAME, COOKIE_1_NEW_VALUE),
            new Cookie(null, COOKIE_2_NAME, COOKIE_2_VALUE) };

    cookiesObject = (Map<String, String>) CookieHelper.putAndMergeCookie(cookiesObject, newCookiesArray);

    assertEquals(2, cookiesObject.size());
    assertEquals(COOKIE_1_NEW_VALUE, cookiesObject.get(COOKIE_1_NAME));
    assertEquals(COOKIE_2_VALUE, cookiesObject.get(COOKIE_2_NAME));

    Map<String, String> unModifiedCookiesObject = (Map<String, String>) CookieHelper
            .putAndMergeCookie(cookiesObject, (Cookie[]) null);
    assertSame(cookiesObject, unModifiedCookiesObject);
    assertEquals(2, cookiesObject.size());
}

From source file:org.mule.transport.http.CookieHelperTestCase.java

@Test
public void testPutAndMergeCookieObjectCookieArray_CookiesInArray_NewCookiesInArray() {
    Cookie[] cookiesObject = new Cookie[] { new Cookie(null, COOKIE_1_NAME, COOKIE_1_ORIGINAL_VALUE) };

    assertEquals(1, cookiesObject.length);

    Cookie[] newCookiesArray = new Cookie[] { new Cookie(null, COOKIE_1_NAME, COOKIE_1_NEW_VALUE),
            new Cookie(null, COOKIE_2_NAME, COOKIE_2_VALUE) };

    cookiesObject = (Cookie[]) CookieHelper.putAndMergeCookie(cookiesObject, newCookiesArray);

    assertEquals(2, cookiesObject.length);

    assertEquals(COOKIE_1_NAME, cookiesObject[0].getName());
    assertEquals(COOKIE_1_NEW_VALUE, cookiesObject[0].getValue());

    assertEquals(COOKIE_2_NAME, cookiesObject[1].getName());
    assertEquals(COOKIE_2_VALUE, cookiesObject[1].getValue());

    Cookie[] unModifiedCookiesObject = (Cookie[]) CookieHelper.putAndMergeCookie(cookiesObject,
            (Cookie[]) null);//from   w w  w.  ja va 2 s. co  m
    assertSame(cookiesObject, unModifiedCookiesObject);
    assertEquals(2, cookiesObject.length);
}

From source file:org.mule.transport.http.functional.HttpCookieTestCase.java

private Cookie[] getCookieArray() {
    Cookie[] cookieArray = new Cookie[1];
    cookieArray[0] = new Cookie("localhost", "customCookie", "yes");
    return cookieArray;
}

From source file:org.mule.transport.http.transformers.MuleMessageToHttpResponseTestCase.java

@Test
public void testSetCookieOnOutbound() throws Exception {
    MuleMessageToHttpResponse transformer = new MuleMessageToHttpResponse();
    MuleMessage msg = mock(MuleMessage.class);

    Cookie[] cookiesOutbound = new Cookie[2];
    cookiesOutbound[0] = new Cookie("domain", "name-out-1", "value-out-1");
    cookiesOutbound[1] = new Cookie("domain", "name-out-2", "value-out-2");

    when(msg.getOutboundProperty("Set-Cookie")).thenReturn(cookiesOutbound);
    Set props = new HashSet();
    props.add("Set-Cookie");
    when(msg.getOutboundPropertyNames()).thenReturn(props);

    HttpResponse response = transformer.createResponse(null, "UTF-8", msg);
    Header[] headers = response.getHeaders();
    int cookiesSet = 0;
    for (Header header : headers) {
        if ("Set-Cookie".equals(header.getName())) {
            cookiesSet++;/*from  w w  w .ja va 2  s  .  c om*/
        }
    }
    Assert.assertEquals(cookiesOutbound.length, cookiesSet);
}

From source file:org.zaproxy.zap.network.ZapCookieSpecUnitTest.java

@Test(expected = NullPointerException.class)
public void shouldThrowWhenValidatingWithNullCookieDomain() throws MalformedCookieException {
    // Given//w ww . j a va  2 s .c om
    CookieSpec cookieSpec = createCookieSpec();
    Cookie cookie = new Cookie(null, "name", "value");
    // When
    cookieSpec.validate(HOST, PORT, PATH, SECURE, cookie);
    // Then = NullPointerException.
}

From source file:org.zaproxy.zap.network.ZapCookieSpecUnitTest.java

@Test(expected = MalformedCookieException.class)
public void shouldBeMalformedWhenValidatingWithNegativeCookieVersion() throws MalformedCookieException {
    // Given//w  w  w .jav a  2 s.  c o m
    CookieSpec cookieSpec = createCookieSpec();
    Cookie cookie = new Cookie(HOST, "name", "value");
    cookie.setVersion(-1);
    // When
    cookieSpec.validate(HOST, PORT, PATH, SECURE, cookie);
    // Then = MalformedCookieException.
}

From source file:org.zaproxy.zap.network.ZapCookieSpecUnitTest.java

@Test
public void shouldBeValidEvenIfCookiePathIsDifferentThanOrigin() throws MalformedCookieException {
    // Given//from  w w  w .  j a  v a2s.  c o m
    CookieSpec cookieSpec = createCookieSpec();
    Cookie cookie = new Cookie(HOST, "name", "value");
    cookie.setPath("/other/path/");
    // When
    cookieSpec.validate(HOST, PORT, PATH, SECURE, cookie);
    // Then = No exception.
}