Search in sources :

Example 16 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestHttpBridge method testHttpToRestErrorMessage.

@Test
public void testHttpToRestErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
    FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
    TransportCallback<RestResponse> bridgeCallback = HttpBridge.httpToRestCallback(callback);
    RestResponse restResponse = new RestResponseBuilder().build();
    // Note: FutureCallback will fail if called twice. An exception would be raised on the current
    // thread because we begin the callback sequence here in onResponse.
    // (test originally added due to bug with double callback invocation)
    bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new RestException(restResponse)));
    RestResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
    // should have unpacked restResponse from the RestException that we passed in without
    // propagating the actual exception
    Assert.assertSame(resp, restResponse);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 17 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestRestBuilders method testSetHeaderNameWithControlChar.

@Test
public void testSetHeaderNameWithControlChar() {
    final String headerBaseName = "invalidName";
    final String headerValue = "testValue";
    for (char i = 0; i <= 31; i++) {
        final RestResponseBuilder builder = new RestResponseBuilder();
        try {
            builder.setHeader(headerBaseName + i, headerValue);
            Assert.fail("Should have thrown exception for invalid char (control char): " + i);
        } catch (IllegalArgumentException e) {
        // expected
        }
    }
    for (char i = 127; i <= 255; i++) {
        final RestResponseBuilder builder = new RestResponseBuilder();
        try {
            builder.setHeader(headerBaseName + i, headerValue);
            Assert.fail("Should have thrown exception for invalid char (control char): " + i);
        } catch (IllegalArgumentException e) {
        // expected
        }
    }
}
Also used : RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 18 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestRestBuilders method testSetMultiValueHeader.

@Test
public void testSetMultiValueHeader() {
    final String headerName = "key";
    final String headerVal1 = "value1";
    final String headerVal2 = "value2";
    final String headerValue = headerVal1 + ',' + headerVal2;
    final MessageHeaders msg = new RestResponseBuilder().setHeader(headerName, headerValue).build();
    Assert.assertEquals(headerValue, msg.getHeader(headerName));
    Assert.assertEquals(Arrays.asList(headerVal1, headerVal2), msg.getHeaderValues(headerName));
}
Also used : RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) MessageHeaders(com.linkedin.r2.message.MessageHeaders) Test(org.testng.annotations.Test)

Example 19 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestRestBuilders method testSetHeaderWithCookieFails.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testSetHeaderWithCookieFails() {
    final String headerName = "set-cookie";
    final String headerValue = "value";
    final RestResponse res = new RestResponseBuilder().setHeader(headerName, headerValue).build();
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 20 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestRestBuilders method testSetCookiesMultipleValues.

@Test
public void testSetCookiesMultipleValues() {
    final String cookie1 = "cookie1";
    final String cookie2 = "cookie2";
    final String cookie3 = "cookie3";
    List<String> cookies = new ArrayList<String>();
    cookies.add(cookie2);
    cookies.add(cookie3);
    final RestResponse res = new RestResponseBuilder().addCookie(cookie1).setCookies(cookies).build();
    Assert.assertNotNull(res.getHeaders());
    Assert.assertNotNull(res.getCookies());
    Assert.assertTrue(res.getHeaders().isEmpty());
    Assert.assertEquals(res.getCookies().size(), 2);
    Assert.assertEquals(res.getCookies().get(0), cookie2);
    Assert.assertEquals(res.getCookies().get(1), cookie3);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Aggregations

RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)67 Test (org.testng.annotations.Test)45 RestResponse (com.linkedin.r2.message.rest.RestResponse)41 RestException (com.linkedin.r2.message.rest.RestException)23 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)14 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)14 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)14 BeforeTest (org.testng.annotations.BeforeTest)14 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)13 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)11 RoutingException (com.linkedin.restli.server.RoutingException)10 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)10 EmptyRecord (com.linkedin.restli.common.EmptyRecord)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 HashMap (java.util.HashMap)8 RequestContext (com.linkedin.r2.message.RequestContext)7 ByteString (com.linkedin.data.ByteString)6 RecordTemplate (com.linkedin.data.template.RecordTemplate)6 Map (java.util.Map)6