use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestQueryTunnel method getResponse.
private RestResponse getResponse(String query, RequestContext requestContext) throws Exception {
URI uri = URI.create("http://localhost:" + _port + "/checkQuery?" + query);
RestRequestBuilder builder = new RestRequestBuilder(uri);
return _client.restRequest(builder.build(), requestContext).get(5000, TimeUnit.MILLISECONDS);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestRestCompressionEcho method testResponseCompression.
@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
RestRequestBuilder builder = new RestRequestBuilder((Bootstrap.createHttpURI(PORT, ECHO_URI)));
byte[] content = new byte[(int) bytes];
for (int i = 0; i < bytes; i++) {
content[i] = (byte) (i % 256);
}
RestRequest request = builder.setEntity(content).build();
final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
RequestContext requestContext = new RequestContext();
// OPERATION is required to enabled response compression
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
client.restRequest(request, requestContext, callback);
final RestResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
Assert.assertEquals(response.getEntity().copyBytes(), content);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestHttpClient method testSimpleURI.
@Test(dataProvider = "configs")
public void testSimpleURI(Server server, Client client) throws Exception {
try {
server.start();
// Note no trailing slash; the point of the test is to ensure this URI will
// send a Request-URI of "/".
URI uri = URI.create("http://localhost:" + PORT);
RestRequestBuilder rb = new RestRequestBuilder(uri);
rb.setMethod("GET");
RestRequest request = rb.build();
Future<RestResponse> f = client.restRequest(request);
// This will block
RestResponse response = f.get();
assertEquals(response.getStatus(), 200);
final FutureCallback<None> callback = new FutureCallback<None>();
client.shutdown(callback);
callback.get();
} finally {
server.stop();
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class RestClient method buildRestRequest.
// This throws Exception to remind the caller to deal with arbitrary exceptions including RuntimeException
// in a way appropriate for the public method that was originally invoked.
private RestRequest buildRestRequest(URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, ProtocolVersion protocolVersion, ContentType contentType, List<AcceptType> acceptTypes, boolean acceptResponseAttachments) throws Exception {
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod(method.getHttpMethod().toString());
requestBuilder.setHeaders(headers);
requestBuilder.setCookies(cookies);
addAcceptHeaders(requestBuilder, acceptTypes, acceptResponseAttachments);
final ContentType type = resolveContentType(requestBuilder, dataMap, contentType);
if (type != null) {
requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey());
switch(type) {
case PSON:
requestBuilder.setEntity(PSON_DATA_CODEC.mapToBytes(dataMap));
break;
case JSON:
requestBuilder.setEntity(JACKSON_DATA_CODEC.mapToBytes(dataMap));
break;
default:
throw new IllegalStateException("Unknown ContentType:" + type);
}
}
addProtocolVersionHeader(requestBuilder, protocolVersion);
if (method.getHttpMethod() == HttpMethod.POST) {
requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString());
}
return requestBuilder.build();
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project incubator-gobblin by apache.
the class R2RestRequestBuilderTest method testBuildWriteRequest.
/**
* Build a {@link RestRequest} from a {@link GenericRecord}
*/
public void testBuildWriteRequest() throws URISyntaxException, IOException {
String urlTemplate = "http://www.test.com/a/part1:${part1}/a/part2:${part2}";
String verb = "update";
String protocolVersion = "2.0.0";
R2RestRequestBuilder builder = spy(new R2RestRequestBuilder(urlTemplate, verb, protocolVersion));
ArgumentCaptor<RestRequestBuilder> requestBuilderArgument = ArgumentCaptor.forClass(RestRequestBuilder.class);
Queue<BufferedRecord<GenericRecord>> queue = HttpTestUtils.createQueue(1, false);
AsyncRequest<GenericRecord, RestRequest> request = builder.buildRequest(queue);
verify(builder).build(requestBuilderArgument.capture());
RestRequestBuilder expected = new RestRequestBuilder(new URI("http://www.test.com/a/part1:01/a/part2:02?param1=01"));
expected.setMethod("PUT");
expected.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion);
expected.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, verb.toLowerCase());
expected.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON);
DataMap data = new DataMap();
data.put("id", "id0");
expected.setEntity(JACKSON_DATA_CODEC.mapToBytes(data));
HttpTestUtils.assertEqual(requestBuilderArgument.getValue(), expected);
Assert.assertEquals(request.getRecordCount(), 1);
Assert.assertEquals(queue.size(), 0);
}
Aggregations