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");
}
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");
}
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;
}
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;
}
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) {
}
}
Aggregations