use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testPostProcessingException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testPostProcessingException(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
// request for nested projection within string field will generate error
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
replay(statusResource);
Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof RestException);
RestException restException = (RestException) e;
RestResponse restResponse = restException.getResponse();
try {
assertEquals(restResponse.getStatus(), 500);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
} catch (Exception e2) {
fail(e2.toString());
}
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
fail();
}
@Override
public void onError(Throwable e) {
Messages.toRestException((StreamException) e, new Callback<RestException>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestException result) {
restResponseCallback.onError(result);
}
});
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testValidRequest.
private void testValidRequest(RestLiServer restLiServer, final ProtocolVersion clientProtocolVersion, boolean filters, final String headerConstant, final RestOrStream restOrStream) throws URISyntaxException {
RestRequest request = null;
StreamRequest streamRequest = null;
if (clientProtocolVersion != null) {
if (restOrStream == RestOrStream.REST) {
request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build();
} else {
streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
}
} else {
if (restOrStream == RestOrStream.REST) {
request = new RestRequestBuilder(new URI("/statuses/1")).build();
} else {
streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).build(EntityStreams.emptyStream());
}
}
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
if (filters) {
_mockFilter.onRequest(anyObject(FilterRequestContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
_mockFilter.onResponse(anyObject(FilterRequestContext.class), anyObject(FilterResponseContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
replay(_mockFilter);
}
replay(statusResource);
final Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
assertEquals(restResponse.getStatus(), 200);
assertTrue(restResponse.getEntity().length() > 0);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
if (clientProtocolVersion != null) {
assertEquals(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, headerConstant, "Rest.li protocol header name is unexpected.");
}
}
@Override
public void onError(Throwable e) {
fail();
}
};
if (restOrStream == RestOrStream.REST) {
restLiServer.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
Callback<StreamResponse> streamResponseCallback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
restResponseCallback.onSuccess(result);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
restLiServer.handleRequest(streamRequest, new RequestContext(), streamResponseCallback);
}
if (filters) {
EasyMock.verify(_mockFilter, _mockFilter);
}
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testHandleRequestWithRestLiResponseSuccess.
@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseSuccess(final RestOrStream restOrStream) throws Exception {
Status status = new Status();
status.data().put("test", "this is a test");
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(status).once();
replay(statusResource);
Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {
@Override
public void onSuccess(RestLiResponse restLiResponse) {
assertEquals(restLiResponse.getDataMap(), status.data());
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
@Override
public void onError(Throwable e) {
fail("We should not get an error here. The server should have returned a 200!");
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
_server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
}
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder 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());
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testStreamRequestMultiplexedRequestMultiPartAcceptType.
@Test
public void testStreamRequestMultiplexedRequestMultiPartAcceptType() throws Exception {
// This test verifies that a StreamRequest sent to the RestLiServer throws an exception if the accept type contains
// multipart/related.
StreamRequest streamRequestMux = new StreamRequestBuilder(new URI("/mux")).setHeader(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof StreamException);
StreamException streamException = (StreamException) e;
StreamResponse streamResponse = streamException.getResponse();
assertEquals(streamResponse.getStatus(), 406);
final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(ByteString result) {
// We have the body so assert
assertTrue(result.length() > 0);
assertEquals(result.asString(Charset.defaultCharset()), "This server cannot handle multiplexed requests that have an accept type of multipart/related");
}
});
streamResponse.getEntityStream().setReader(fullEntityReader);
}
};
_server.handleRequest(streamRequestMux, new RequestContext(), callback);
}
Aggregations