Search in sources :

Example 21 with ByteStringWriter

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));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 22 with ByteStringWriter

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());
}
Also used : RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) MultiPartMIMEReaderCallback(com.linkedin.multipart.MultiPartMIMEReaderCallback) RequestContext(com.linkedin.r2.message.RequestContext) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) RestResponse(com.linkedin.r2.message.rest.RestResponse) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 23 with ByteStringWriter

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));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestException(com.linkedin.r2.message.rest.RestException) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 24 with ByteStringWriter

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());
        }
    });
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 25 with ByteStringWriter

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());
        }
    });
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Aggregations

ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)38 Test (org.testng.annotations.Test)34 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)32 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)31 URI (java.net.URI)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)17 RequestContext (com.linkedin.r2.message.RequestContext)15 ByteString (com.linkedin.data.ByteString)10 Callback (com.linkedin.common.callback.Callback)8 Http2Headers (io.netty.handler.codec.http2.Http2Headers)8 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)7 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Server (org.eclipse.jetty.server.Server)6 AfterTest (org.testng.annotations.AfterTest)6 BeforeTest (org.testng.annotations.BeforeTest)6 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5