use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestMIMEReaderStateTransitions method testRegisterCallbackMultiPartMIMEReader.
// MultiPartMIMEReader exceptions:
@Test
public void testRegisterCallbackMultiPartMIMEReader() {
final EntityStream entityStream = mock(EntityStream.class);
final StreamRequest streamRequest = mock(StreamRequest.class);
when(streamRequest.getEntityStream()).thenReturn(entityStream);
when(streamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER)).thenReturn("multipart/mixed; boundary=\"--123\"");
MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
// Test each possible exception:
reader.setState(MultiPartMIMEReader.MultiPartReaderState.FINISHED);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.READING_EPILOGUE);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.CALLBACK_BOUND_AND_READING_PREAMBLE);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
reader.setState(MultiPartMIMEReader.MultiPartReaderState.DRAINING);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
// This is a desired top level reader state
reader.setState(MultiPartMIMEReader.MultiPartReaderState.READING_PARTS);
final MultiPartMIMEReader.SinglePartMIMEReader singlePartMIMEReader = reader.new SinglePartMIMEReader(Collections.<String, String>emptyMap());
// This is a undesired single part state
singlePartMIMEReader.setState(MultiPartMIMEReader.SingleReaderState.REQUESTED_DATA);
reader.setCurrentSinglePartMIMEReader(singlePartMIMEReader);
try {
reader.registerReaderCallback(EMPTY_MULTI_PART_MIME_READER_CALLBACK);
Assert.fail();
} catch (StreamBusyException streamBusyException) {
}
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestMIMEIntegrationReader method executeRequestAndAssert.
// /////////////////////////////////////////////////////////////////////////////////////
private void executeRequestAndAssert(final ByteString requestPayload, final int chunkSize, final MimeMultipart mimeMultipart) throws Exception {
final VariableByteStringWriter variableByteStringWriter = new VariableByteStringWriter(requestPayload, chunkSize);
final EntityStream entityStream = EntityStreams.newEntityStream(variableByteStringWriter);
final StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, SERVER_URI));
// We add additional parameters since MIME supports this and we want to make sure we can still extract boundary
// properly.
final String contentTypeHeader = mimeMultipart.getContentType() + ";somecustomparameter=somecustomvalue" + ";anothercustomparameter=anothercustomvalue";
StreamRequest request = builder.setMethod("POST").setHeader(HEADER_CONTENT_TYPE, contentTypeHeader).build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectSuccessCallback(latch, status, new HashMap<>());
_client.streamRequest(request, callback);
latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
List<SinglePartMIMEReaderCallbackImpl> singlePartMIMEReaderCallbacks = _mimeServerRequestHandler._testMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks;
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), mimeMultipart.getCount());
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
// Actual
final SinglePartMIMEReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
// Expected
final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
// Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback._headers, expectedHeaders);
// Verify the body matches
if (currentExpectedPart.getContent() instanceof byte[]) {
Assert.assertEquals(currentCallback._finishedData.copyBytes(), currentExpectedPart.getContent());
} else {
// Default is String
Assert.assertEquals(new String(currentCallback._finishedData.copyBytes()), currentExpectedPart.getContent());
}
}
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestMIMEIntegrationReaderDrain method executeRequestWithDrainStrategy.
// /////////////////////////////////////////////////////////////////////////////////////
private MimeMultipart executeRequestWithDrainStrategy(final int chunkSize, final List<MimeBodyPart> bodyPartList, final String drainStrategy, final String serverHeaderPrefix) throws Exception {
MimeMultipart multiPartMimeBody = new MimeMultipart();
// Add your body parts
for (final MimeBodyPart bodyPart : bodyPartList) {
multiPartMimeBody.addBodyPart(bodyPart);
}
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
multiPartMimeBody.writeTo(byteArrayOutputStream);
final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
final VariableByteStringWriter variableByteStringWriter = new VariableByteStringWriter(requestPayload, chunkSize);
final EntityStream entityStream = EntityStreams.newEntityStream(variableByteStringWriter);
final StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, SERVER_URI));
StreamRequest request = builder.setMethod("POST").setHeader(HEADER_CONTENT_TYPE, multiPartMimeBody.getContentType()).setHeader(DRAIN_HEADER, drainStrategy).build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
final Map<String, String> responseHeaders = new HashMap<>();
Callback<StreamResponse> callback = expectSuccessCallback(latch, status, responseHeaders);
_client.streamRequest(request, callback);
latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
Assert.assertEquals(responseHeaders.get(DRAIN_HEADER), serverHeaderPrefix + drainStrategy);
return multiPartMimeBody;
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestUnstructuredDataClient method testClientWithStreamResponse.
@Test(dataProvider = "goodURLs")
public void testClientWithStreamResponse(String resourceURL) throws Throwable {
ByteString expectedContent = ByteString.copy(GreetingUnstructuredDataUtils.UNSTRUCTURED_DATA_BYTES);
URI testURI = URI.create(RestLiIntegrationTest.URI_PREFIX + resourceURL);
EntityStream emptyStream = EntityStreams.emptyStream();
StreamRequest streamRequest = new StreamRequestBuilder(testURI).build(emptyStream);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean();
_client.streamRequest(streamRequest, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
fail("failed to get response", e);
}
@Override
public void onSuccess(StreamResponse result) {
assertEquals(result.getHeader(RestConstants.HEADER_CONTENT_TYPE), GreetingUnstructuredDataUtils.MIME_TYPE);
FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
success.set(false);
latch.countDown();
}
@Override
public void onSuccess(ByteString result) {
// Won't fail the test, only use to print out error
assertEquals(result, expectedContent);
// Will fail the test if content is not identical
success.set(result.equals(expectedContent));
latch.countDown();
}
});
result.getEntityStream().setReader(fullEntityReader);
}
});
latch.await(10, TimeUnit.SECONDS);
if (!success.get())
fail("Failed to read response data from stream!");
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestRestLiServer method testUnstructuredDataGetWithBody.
@Test
public void testUnstructuredDataGetWithBody() throws Exception {
EntityStream streams = EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 1, 2, 3, 4 })));
Observer observer = new TestObserver();
streams.addObserver(observer);
final StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveGreetingCollectionUnstructuredData/hello")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(streams);
final Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
}
@Override
public void onError(Throwable e) {
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
assertTrue(((TestObserver) observer).isDone());
}
Aggregations