use of com.linkedin.r2.message.stream.entitystream.ByteStringWriter in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToHttp2HeadersBlacklist.
@Test
public void testStreamToHttp2HeadersBlacklist() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
HEADER_BLACKLIST.forEach(header -> streamRequestBuilder.addHeaderValue(header, ANY_HEADER));
StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
Assert.assertNotNull(headers);
HEADER_BLACKLIST.forEach(header -> Assert.assertFalse(headers.contains(header), header));
}
use of com.linkedin.r2.message.stream.entitystream.ByteStringWriter in project rest.li by linkedin.
the class TestParseqTraceDebugRequestHandler method testErrorStreamingAbsorbRequestAbortResponse.
@Test
public void testErrorStreamingAbsorbRequestAbortResponse() throws Exception {
//This test verifies that in the face of an error, the ParseqTraceDebugRequestHandler aborts any potential outgoing
//response attachments and absorbs and drops on the ground any incoming request attachments.
final URI uri = URI.create("http://host/abc/12/__debug/parseqtrace/raw");
ParseqTraceDebugRequestHandler requestHandler = new ParseqTraceDebugRequestHandler();
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
RestRequest request = requestBuilder.build();
RequestContext requestContext = new RequestContext();
final RequestExecutionCallback<RestResponse> callback = new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
//Even though we call callback.onError() below to simulate a failed rest.li request execution, the
//ParseqTraceDebugRequestHandler eventually treats this as a success.
Assert.fail("Request execution passed unexpectedly.");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
}
};
//Create request and response attachments. This is not the wire protocol for rest.li streaming, but
//makes this test easier to write and understand.
//Response attachment.
final RestLiTestAttachmentDataSource responseDataSource = RestLiTestAttachmentDataSource.createWithRandomPayload("1");
final RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().appendSingleAttachment(responseDataSource).build();
//Request attachment. We need to create a reader here.
final RestLiTestAttachmentDataSource requestAttachment = RestLiTestAttachmentDataSource.createWithRandomPayload("2");
final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
AttachmentUtils.appendSingleAttachmentToBuilder(builder, requestAttachment);
final ByteStringWriter byteStringWriter = new ByteStringWriter(ByteString.copyString("Normal rest.li request payload", Charset.defaultCharset()));
final MultiPartMIMEWriter writer = AttachmentUtils.createMultiPartMIMEWriter(byteStringWriter, "application/json", builder);
final StreamRequest streamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(new URI(""), "related", writer);
final MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
final RestLiAttachmentReader attachmentReader = new RestLiAttachmentReader(reader);
//Absorb the first part as rest.li server does and then give the beginning of the next part to the
//ParseqTraceDebugRequestHandler.
final MultiPartMIMEReaderCallback multiPartMIMEReaderCallback = new MultiPartMIMEReaderCallback() {
int partCounter = 0;
@Override
public void onNewPart(MultiPartMIMEReader.SinglePartMIMEReader singlePartMIMEReader) {
if (partCounter == 0) {
singlePartMIMEReader.drainPart();
partCounter++;
} else {
//When the first part is read in, at the beginning of the 2nd part, we move to the debug request handler.
requestHandler.handleRequest(request, requestContext, new RestLiDebugRequestHandler.ResourceDebugRequestHandler() {
@Override
public void handleRequest(RestRequest request, RequestContext requestContext, RequestExecutionCallback<RestResponse> callback) {
callback.onError(RestException.forError(500, "An error has occurred"), new RequestExecutionReportBuilder().build(), attachmentReader, responseAttachments);
}
}, attachmentReader, callback);
}
}
@Override
public void onFinished() {
Assert.fail();
}
@Override
public void onDrainComplete() {
//Eventually this should occur.
}
@Override
public void onStreamError(Throwable throwable) {
Assert.fail();
}
};
reader.registerReaderCallback(multiPartMIMEReaderCallback);
//The response data source should have been aborted.
Assert.assertTrue(responseDataSource.dataSourceAborted());
//The request attachment should have been absorbed and finished.
Assert.assertTrue(requestAttachment.finished());
}
use of com.linkedin.r2.message.stream.entitystream.ByteStringWriter in project rest.li by linkedin.
the class TestMessages method testToStreamTransportCallbackStreamException.
@Test
public void testToStreamTransportCallbackStreamException() {
TransportCallback<RestResponse> restCallback = response -> {
Assert.assertTrue(response.hasError());
Assert.assertNotNull(response.getError());
Assert.assertTrue(response.getError() instanceof RestException);
Assert.assertNotNull(response.getWireAttributes());
Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
Assert.assertEquals(response.getError().getStackTrace().length, 0);
};
TransportCallback<StreamResponse> streamCallback = Messages.toStreamTransportCallback(restCallback);
StreamResponseBuilder builder = new StreamResponseBuilder();
StreamResponse streamResponse = builder.build(EntityStreams.newEntityStream(new ByteStringWriter(DATA)));
streamCallback.onResponse(TransportResponseImpl.error(new StreamException(streamResponse, new IllegalStateException()), WIRE_ATTR));
}
use of com.linkedin.r2.message.stream.entitystream.ByteStringWriter in project rest.li by linkedin.
the class TestBuilders method testChainBuildStreamRequestFromStreamRequestBuilder.
@Test
public void testChainBuildStreamRequestFromStreamRequestBuilder() {
final StreamRequest req = new StreamRequestBuilder(URI.create("test")).setHeader("k1", "v1").setMethod(RestMethod.PUT).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 1, 2, 3, 4 })))).builder().setHeader("k2", "v2").setMethod(RestMethod.POST).setURI(URI.create("anotherURI")).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 5, 6, 7, 8 }))));
Messages.toRestRequest(req, new Callback<RestRequest>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestRequest result) {
Assert.assertEquals(new byte[] { 5, 6, 7, 8 }, result.getEntity().copyBytes());
Assert.assertEquals("v1", req.getHeader("k1"));
Assert.assertEquals("v2", req.getHeader("k2"));
Assert.assertEquals(RestMethod.POST, req.getMethod());
Assert.assertEquals(URI.create("anotherURI"), req.getURI());
}
});
}
use of com.linkedin.r2.message.stream.entitystream.ByteStringWriter in project rest.li by linkedin.
the class TestBuilders method testChainBuildStreamResponseFromStreamResponseBuilder.
@Test
public void testChainBuildStreamResponseFromStreamResponseBuilder() {
final StreamResponse res = new StreamResponseBuilder().setHeader("k1", "v1").setStatus(300).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 1, 2, 3, 4 })))).builder().setHeader("k2", "v2").setStatus(400).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 5, 6, 7, 8 }))));
Messages.toRestResponse(res, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
Assert.assertEquals(new byte[] { 5, 6, 7, 8 }, result.getEntity().copyBytes());
Assert.assertEquals("v1", res.getHeader("k1"));
Assert.assertEquals("v2", res.getHeader("k2"));
Assert.assertEquals(400, res.getStatus());
}
});
}
Aggregations