List of usage examples for io.netty.buffer Unpooled copiedBuffer
public static ByteBuf copiedBuffer(ByteBuffer... buffers)
From source file:com.linkedin.flashback.netty.builder.RecordedHttpMessageBuilder.java
License:Open Source License
/** * Append http content to temporary byte buffer. * @param chunk netty http content chunk * *//*ww w .j ava 2 s . c o m*/ public void appendHttpContent(HttpContent chunk) throws IOException { _bodyByteBuf.addComponent(Unpooled.copiedBuffer(chunk.content())); _bodyByteBuf.writerIndex(_bodyByteBuf.writerIndex() + chunk.content().readableBytes()); }
From source file:com.linkedin.flashback.netty.builder.RecordedHttpRequestBuilderTest.java
License:Open Source License
@Test public void testBuildContent() throws Exception { HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "www.google.com"); RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest); String charset = "UTF-8"; String str1 = "first content"; HttpContent httpContent1 = new DefaultHttpContent(Unpooled.copiedBuffer(str1.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(httpContent1); String str2 = "second content"; HttpContent httpContent2 = new DefaultHttpContent(Unpooled.copiedBuffer(str2.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(httpContent2); String lastStr = "Last chunk"; HttpContent lastContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastStr.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(lastContent); RecordedHttpRequest recordedHttpRequest = recordedHttpRequestBuilder.build(); Assert.assertEquals((str1 + str2 + lastStr).getBytes(charset), recordedHttpRequest.getHttpBody().getContent(charset)); }
From source file:com.linkedin.flashback.netty.builder.RecordedHttpResponseBuilderTest.java
License:Open Source License
@Test public void testBuild() throws IOException { HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.GATEWAY_TIMEOUT); RecordedHttpResponseBuilder recordedHttpResponseBuilder = new RecordedHttpResponseBuilder(httpResponse); String charset = "UTF-8"; String str1 = "Hello world"; HttpContent httpContent1 = new DefaultHttpContent(Unpooled.copiedBuffer(str1.getBytes(charset))); recordedHttpResponseBuilder.appendHttpContent(httpContent1); String str2 = "second content"; HttpContent httpContent2 = new DefaultHttpContent(Unpooled.copiedBuffer(str2.getBytes(charset))); recordedHttpResponseBuilder.appendHttpContent(httpContent2); String lastStr = "Last chunk"; HttpContent lastContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastStr.getBytes(charset))); recordedHttpResponseBuilder.appendHttpContent(lastContent); RecordedHttpResponse recordedHttpResponse = recordedHttpResponseBuilder.build(); Assert.assertEquals(recordedHttpResponse.getStatus(), HttpResponseStatus.GATEWAY_TIMEOUT.code()); Assert.assertEquals((str1 + str2 + lastStr).getBytes(charset), recordedHttpResponse.getHttpBody().getContent(charset)); }
From source file:com.mesosphere.mesos.rx.java.recordio.RecordIOOperatorTest.java
License:Apache License
@Test public void readEvents_multipleEventsInOneChunk() throws Exception { final List<Event> subHbOffer = newArrayList(TestingProtos.SUBSCRIBED, TestingProtos.HEARTBEAT, TestingProtos.OFFER);//from ww w . j av a 2s . c o m final List<byte[]> eventChunks = subHbOffer.stream().map(AbstractMessageLite::toByteArray) .map(RecordIOUtils::createChunk).collect(Collectors.toList()); final List<ByteBuf> singleChunk = newArrayList(Unpooled.copiedBuffer(concatAllChunks(eventChunks))); final List<Event> events = runTestOnChunks(singleChunk); assertThat(events).isEqualTo(subHbOffer); }
From source file:com.mesosphere.mesos.rx.java.ResponseUtilsTest.java
License:Apache License
@Test public void attemptToReadErrorResponse_plainString() throws Exception { final String errMsg = "some response"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> { headers.add("Content-Type", "text/plain;charset=utf-8"); headers.add("Content-Length", bytes.length); });//from w ww. j a v a2s.c o m final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo(errMsg); }
From source file:com.mesosphere.mesos.rx.java.ResponseUtilsTest.java
License:Apache License
@Test public void attemptToReadErrorResponse_noContentLength() throws Exception { final String errMsg = "some response"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> headers.add("Content-Type", "text/plain;charset=utf-8")); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo(""); }
From source file:com.mesosphere.mesos.rx.java.ResponseUtilsTest.java
License:Apache License
@Test public void attemptToReadErrorResponse_noContentType() throws Exception { final String errMsg = "some response"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> headers.add("Content-Length", bytes.length)); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo("Not attempting to decode error response with unspecified Content-Type"); }
From source file:com.mesosphere.mesos.rx.java.ResponseUtilsTest.java
License:Apache License
@Test public void attemptToReadErrorResponse_contentLengthIsZero() throws Exception { final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(new byte[] {}), (headers) -> headers.add("Content-Length", "0")); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo(""); }
From source file:com.mesosphere.mesos.rx.java.ResponseUtilsTest.java
License:Apache License
@Test public void attemptToReadErrorResponse_nonTextPlain() throws Exception { final String errMsg = "{\"some\":\"json\"}"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> { headers.add("Content-Type", "application/json;charset=utf-8"); headers.add("Content-Length", bytes.length); });//from ww w .j av a 2s . co m final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo( "Not attempting to decode error response of type 'application/json;charset=utf-8' as string"); }
From source file:com.mesosphere.mesos.rx.java.ResponseUtilsTest.java
License:Apache License
@Test public void attemptToReadErrorResponse_responseContentIgnoredByDefaultWhenNotString() throws Exception { final String errMsg = "lies"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> { headers.add("Content-Type", "application/json;charset=utf-8"); headers.add("Content-Length", bytes.length); });//ww w. j a v a2s.c om final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isNotEqualTo("lies"); try { resp.getContent().toBlocking().first(); } catch (IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Content stream is already disposed."); } }