Search in sources :

Example 91 with HttpCookie

use of java.net.HttpCookie in project rest.li by linkedin.

the class TestMockResponseBuilder method testBuild.

@Test
public void testBuild() {
    MockResponseBuilder<Long, Greeting> mockResponseBuilder = new MockResponseBuilder<Long, Greeting>();
    Greeting greeting = new Greeting().setId(1L).setMessage("message");
    Map<String, String> headers = Collections.singletonMap("foo", "bar");
    RestLiResponseException restLiResponseException = EasyMock.createMock(RestLiResponseException.class);
    EasyMock.expect(restLiResponseException.getErrorSource()).andReturn("foo").once();
    EasyMock.replay(restLiResponseException);
    // build a response object using all the setters. This response does not make sense logically but the goal here
    // is to test that the builder is working correctly.
    mockResponseBuilder.setEntity(greeting).setHeaders(headers).setCookies(Collections.singletonList(new HttpCookie("cookie", "value"))).setStatus(200).setRestLiResponseException(restLiResponseException);
    Response<Greeting> response = mockResponseBuilder.build();
    // when we build the Response the ID is put into the headers
    Map<String, String> builtHeaders = new HashMap<String, String>(headers);
    builtHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString());
    Assert.assertEquals(response.getEntity(), greeting);
    Assert.assertEquals(response.getHeaders(), builtHeaders);
    Assert.assertEquals(response.getCookies(), Collections.singletonList(new HttpCookie("cookie", "value")));
    Assert.assertEquals(response.getStatus(), 200);
    Assert.assertEquals(response.getError().getErrorSource(), "foo");
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) HttpCookie(java.net.HttpCookie) MockResponseBuilder(com.linkedin.restli.client.testutils.MockResponseBuilder) Test(org.testng.annotations.Test)

Example 92 with HttpCookie

use of java.net.HttpCookie in project rest.li by linkedin.

the class TestMockRestliResponseExceptionBuilder method testAddCookiesAndHeaders.

@Test
public void testAddCookiesAndHeaders() {
    Map.Entry<String, String> expectedEntry = new AbstractMap.SimpleEntry<>("foo", "bar");
    HttpCookie expectedCookie = new HttpCookie("bar", "foo");
    Map<String, String> headers = new HashMap<>();
    headers.put(expectedEntry.getKey(), expectedEntry.getValue());
    List<HttpCookie> cookies = new ArrayList<>();
    cookies.add(expectedCookie);
    RestLiResponseException exception = new MockRestliResponseExceptionBuilder().setHeaders(headers).setCookies(cookies).build();
    RestResponse errorResponse = exception.getResponse();
    assertEquals(errorResponse.getHeader(expectedEntry.getKey()), expectedEntry.getValue());
    assertEquals(errorResponse.getCookies().get(0), "bar=foo");
}
Also used : HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) MockRestliResponseExceptionBuilder(com.linkedin.restli.client.testutils.MockRestliResponseExceptionBuilder) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HttpCookie(java.net.HttpCookie) Test(org.testng.annotations.Test)

Example 93 with HttpCookie

use of java.net.HttpCookie in project rest.li by linkedin.

the class CookieUtil method decodeCookies.

/**
   * Cutomized cookie decoding function, for unknown client communication with potentially multiple cookies in one cookie string.
   * @param cookieStrs
   * @return list of cookie objects
   */
public static List<HttpCookie> decodeCookies(List<String> cookieStrs) {
    List<HttpCookie> cookies = new ArrayList<HttpCookie>();
    if (cookieStrs == null) {
        return cookies;
    }
    for (String cookieStr : cookieStrs) {
        if (cookieStr == null) {
            continue;
        }
        StringTokenizer tokenizer = new StringTokenizer(cookieStr, ";");
        String nameValuePair;
        HttpCookie cookieToBeAdd = null;
        while (tokenizer.hasMoreTokens()) {
            nameValuePair = tokenizer.nextToken();
            int index = nameValuePair.indexOf('=');
            if (index != -1) {
                String name = nameValuePair.substring(0, index).trim();
                String value = stripOffSurrounding(nameValuePair.substring(index + 1).trim());
                if (name.charAt(0) != '$') {
                    if (cookieToBeAdd != null) {
                        cookies.add(cookieToBeAdd);
                    }
                    cookieToBeAdd = new HttpCookie(name, value);
                } else if (cookieToBeAdd != null) {
                    if (name.equals("$Path")) {
                        cookieToBeAdd.setPath(value);
                    } else if (name.equals("$Domain")) {
                        cookieToBeAdd.setDomain(value);
                    } else if (name.equals("$Port")) {
                        cookieToBeAdd.setPortlist(value);
                    }
                }
            } else {
                throw new IllegalArgumentException("Invalid cookie name-value pair");
            }
        }
        if (cookieToBeAdd != null) {
            cookies.add(cookieToBeAdd);
        }
    }
    return cookies;
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) HttpCookie(java.net.HttpCookie)

Example 94 with HttpCookie

use of java.net.HttpCookie in project rest.li by linkedin.

the class MultiplexedRequestBuilder method toIndividualRequest.

private static IndividualRequest toIndividualRequest(Request<?> request, IndividualRequestMap dependantRequests) throws RestLiEncodingException {
    //TODO: Hardcoding RESTLI_PROTOCOL_2_0_0 for now. We need to refactor this code later to get protocol version using the mechanism similar to
    // RestClient.getProtocolVersionForService()
    ProtocolVersion protocolVersion = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    String relativeUrl = getRelativeUrl(request, protocolVersion);
    IndividualRequest individualRequest = new IndividualRequest();
    individualRequest.setRelativeUrl(relativeUrl);
    individualRequest.setMethod(request.getMethod().getHttpMethod().name());
    individualRequest.setHeaders(new StringMap(request.getHeaders()));
    List<HttpCookie> cookies = request.getCookies();
    if (cookies != null && !cookies.isEmpty()) {
        throw new IllegalArgumentException(String.format("Cookies for individual request '%s' MUST be added at the envelope request level", relativeUrl));
    }
    individualRequest.setBody(getBody(request, protocolVersion), SetMode.IGNORE_NULL);
    individualRequest.setDependentRequests(dependantRequests);
    return individualRequest;
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) StringMap(com.linkedin.data.template.StringMap) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) HttpCookie(java.net.HttpCookie)

Example 95 with HttpCookie

use of java.net.HttpCookie in project rest.li by linkedin.

the class TestAbstractRequestBuilder method testCookiesAreReadOnly.

@Test
public void testCookiesAreReadOnly() {
    final AbstractRequestBuilder<Object, ?, ?> builder = new DummyAbstractRequestBuilder();
    builder.setCookies(Arrays.asList(new HttpCookie("a", "b"), new HttpCookie("c", "d")));
    List<HttpCookie> cookies = builder.buildReadOnlyCookies();
    try {
        cookies.add(new HttpCookie("ac", "bb"));
        Assert.fail("The generated cookies should be read-only.");
    } catch (Exception e) {
    }
}
Also used : HttpCookie(java.net.HttpCookie) Test(org.testng.annotations.Test)

Aggregations

HttpCookie (java.net.HttpCookie)151 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)32 Test (org.junit.Test)31 IOException (java.io.IOException)16 Test (org.testng.annotations.Test)13 MockResponse (com.google.mockwebserver.MockResponse)11 MockWebServer (com.google.mockwebserver.MockWebServer)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Project (com.kickstarter.models.Project)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 Cookie (javax.servlet.http.Cookie)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 ByteString (com.linkedin.data.ByteString)4