use of org.chromium.net.UrlResponseInfo in project grpc-java by grpc.
the class CronetClientStreamTest method read.
@Test
public void read() {
ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
BidirectionalStream.Callback callback = callbackCaptor.getValue();
// Read is not called until we receive the response header.
verify(cronetStream, times(0)).read(isA(ByteBuffer.class));
UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
callback.onResponseHeadersReceived(cronetStream, info);
verify(cronetStream, times(1)).read(isA(ByteBuffer.class));
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
verify(clientListener).headersRead(metadataCaptor.capture());
// Verify recevied headers.
Metadata metadata = metadataCaptor.getValue();
assertEquals("application/grpc", metadata.get(Metadata.Key.of("content-type", Metadata.ASCII_STRING_MARSHALLER)));
assertEquals("test-value", metadata.get(Metadata.Key.of("test-key", Metadata.ASCII_STRING_MARSHALLER)));
callback.onReadCompleted(cronetStream, info, createMessageFrame(new String("response1").getBytes(Charset.forName("UTF-8"))), false);
// Haven't request any message, so no callback is called here.
verify(clientListener, times(0)).messagesAvailable(isA(MessageProducer.class));
verify(cronetStream, times(1)).read(isA(ByteBuffer.class));
// Request one message
clientStream.request(1);
verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
// BidirectionalStream.read will not be called again after receiving endOfStream(empty buffer).
clientStream.request(1);
callback.onReadCompleted(cronetStream, info, ByteBuffer.allocate(0), true);
verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
}
use of org.chromium.net.UrlResponseInfo in project grpc-java by grpc.
the class CronetClientStreamTest method streamSucceeded.
@Test
public void streamSucceeded() {
ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
BidirectionalStream.Callback callback = callbackCaptor.getValue();
callback.onStreamReady(cronetStream);
verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class));
// Send the first data frame.
CronetWritableBufferAllocator allocator = new CronetWritableBufferAllocator();
String request = new String("request");
WritableBuffer writableBuffer = allocator.allocate(request.length());
writableBuffer.write(request.getBytes(Charset.forName("UTF-8")), 0, request.length());
clientStream.abstractClientStreamSink().writeFrame(writableBuffer, false, true, 1);
ArgumentCaptor<ByteBuffer> bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
verify(cronetStream, times(1)).write(bufferCaptor.capture(), isA(Boolean.class));
ByteBuffer buffer = bufferCaptor.getValue();
((Buffer) buffer).position(request.length());
verify(cronetStream, times(1)).flush();
// Receive response header
clientStream.request(2);
UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
callback.onResponseHeadersReceived(cronetStream, info);
verify(cronetStream, times(1)).read(isA(ByteBuffer.class));
// Receive one message
callback.onReadCompleted(cronetStream, info, createMessageFrame(new String("response").getBytes(Charset.forName("UTF-8"))), false);
verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
// Send endOfStream
callback.onWriteCompleted(cronetStream, null, buffer, false);
clientStream.abstractClientStreamSink().writeFrame(null, true, true, 1);
verify(cronetStream, times(2)).write(isA(ByteBuffer.class), isA(Boolean.class));
verify(cronetStream, times(2)).flush();
// Receive trailer
((CronetClientStream.BidirectionalStreamCallback) callback).processTrailers(trailers(0));
callback.onSucceeded(cronetStream, info);
// Verify trailer
ArgumentCaptor<Metadata> trailerCaptor = ArgumentCaptor.forClass(Metadata.class);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(clientListener).closed(statusCaptor.capture(), isA(RpcProgress.class), trailerCaptor.capture());
// Verify recevied headers.
Metadata trailers = trailerCaptor.getValue();
Status status = statusCaptor.getValue();
assertEquals("test-trailer-value", trailers.get(Metadata.Key.of("test-trailer-key", Metadata.ASCII_STRING_MARSHALLER)));
assertEquals("application/grpc", trailers.get(Metadata.Key.of("content-type", Metadata.ASCII_STRING_MARSHALLER)));
assertTrue(status.isOk());
}
use of org.chromium.net.UrlResponseInfo in project grpc-java by grpc.
the class CronetClientStreamTest method reportTrailersWhenTrailersReceivedBeforeReadClosed.
@Test
public void reportTrailersWhenTrailersReceivedBeforeReadClosed() {
ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
BidirectionalStream.Callback callback = callbackCaptor.getValue();
callback.onStreamReady(cronetStream);
UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
callback.onResponseHeadersReceived(cronetStream, info);
// Receive trailer first
((CronetClientStream.BidirectionalStreamCallback) callback).processTrailers(trailers(Status.UNAUTHENTICATED.getCode().value()));
verify(clientListener, times(0)).closed(isA(Status.class), isA(RpcProgress.class), isA(Metadata.class));
// Receive cronet's endOfStream
callback.onReadCompleted(cronetStream, null, ByteBuffer.allocate(0), true);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(clientListener, times(1)).closed(statusCaptor.capture(), isA(RpcProgress.class), isA(Metadata.class));
Status status = statusCaptor.getValue();
assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode());
}
use of org.chromium.net.UrlResponseInfo in project grpc-java by grpc.
the class CronetClientStreamTest method streamSucceededWithGrpcError.
@Test
public void streamSucceededWithGrpcError() {
ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
BidirectionalStream.Callback callback = callbackCaptor.getValue();
callback.onStreamReady(cronetStream);
verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class));
clientStream.abstractClientStreamSink().writeFrame(null, true, true, 1);
verify(cronetStream, times(1)).write(isA(ByteBuffer.class), isA(Boolean.class));
verify(cronetStream, times(1)).flush();
// Receive response header
clientStream.request(2);
UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
callback.onResponseHeadersReceived(cronetStream, info);
verify(cronetStream, times(1)).read(isA(ByteBuffer.class));
// Receive trailer
callback.onReadCompleted(cronetStream, null, ByteBuffer.allocate(0), true);
((CronetClientStream.BidirectionalStreamCallback) callback).processTrailers(trailers(Status.PERMISSION_DENIED.getCode().value()));
callback.onSucceeded(cronetStream, info);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(clientListener).closed(statusCaptor.capture(), isA(RpcProgress.class), isA(Metadata.class));
// Verify error status.
Status status = statusCaptor.getValue();
assertFalse(status.isOk());
assertEquals(Status.PERMISSION_DENIED.getCode(), status.getCode());
}
Aggregations