io.netty.channel.embedded.EmbeddedChannel#writeOneOutbound ( )源码实例Demo

下面列出了io.netty.channel.embedded.EmbeddedChannel#writeOneOutbound ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bazel   文件: HttpUploadHandlerTest.java
private void uploadsShouldWork(boolean casUpload, EmbeddedChannel ch, HttpResponseStatus status)
    throws Exception {
  ByteArrayInputStream data = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(new UploadCommand(CACHE_URI, casUpload, "abcdef", data, 5), writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.method()).isEqualTo(HttpMethod.PUT);
  assertThat(request.headers().get(HttpHeaders.CONNECTION))
      .isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());

  HttpChunkedInput content = ch.readOutbound();
  assertThat(content.readChunk(ByteBufAllocator.DEFAULT).content().readableBytes()).isEqualTo(5);

  FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
  response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

  ch.writeInbound(response);

  assertThat(writePromise.isDone()).isTrue();
  assertThat(ch.isOpen()).isTrue();
}
 
源代码2 项目: bazel   文件: HttpUploadHandlerTest.java
/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test
public void httpErrorsAreSupported() {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpUploadHandler(null, ImmutableList.of()));
  ByteArrayInputStream data = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(new UploadCommand(CACHE_URI, true, "abcdef", data, 5), writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request).isInstanceOf(HttpRequest.class);
  HttpChunkedInput content = ch.readOutbound();
  assertThat(content).isInstanceOf(HttpChunkedInput.class);

  FullHttpResponse response =
      new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);

  ch.writeInbound(response);

  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).response().status())
      .isEqualTo(HttpResponseStatus.FORBIDDEN);
  assertThat(ch.isOpen()).isFalse();
}
 
源代码3 项目: bazel   文件: HttpUploadHandlerTest.java
/**
 * Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND) with a
 * Content-Length header.
 */
@Test
public void httpErrorsWithContentAreSupported() {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpUploadHandler(null, ImmutableList.of()));
  ByteArrayInputStream data = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(new UploadCommand(CACHE_URI, true, "abcdef", data, 5), writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request).isInstanceOf(HttpRequest.class);
  HttpChunkedInput content = ch.readOutbound();
  assertThat(content).isInstanceOf(HttpChunkedInput.class);

  ByteBuf errorMsg = ByteBufUtil.writeAscii(ch.alloc(), "error message");
  FullHttpResponse response =
      new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, errorMsg);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

  ch.writeInbound(response);

  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).response().status())
      .isEqualTo(HttpResponseStatus.NOT_FOUND);
  assertThat(ch.isOpen()).isTrue();
}
 
源代码4 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void extraHeadersAreIncluded() throws Exception {
  URI uri = new URI("http://does.not.exist:8080/foo");
  ImmutableList<Entry<String, String>> remoteHeaders =
      ImmutableList.of(
          Maps.immutableEntry("key1", "value1"), Maps.immutableEntry("key2", "value2"));

  EmbeddedChannel ch =
      new EmbeddedChannel(new HttpDownloadHandler(/* credentials= */ null, remoteHeaders));
  DownloadCommand cmd =
      new DownloadCommand(uri, /* casDownload= */ true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().get("key1")).isEqualTo("value1");
  assertThat(request.headers().get("key2")).isEqualTo("value2");
}
 
源代码5 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void multipleExtraHeadersAreSupported() throws Exception {
  URI uri = new URI("http://does.not.exist:8080/foo");
  ImmutableList<Entry<String, String>> remoteHeaders =
      ImmutableList.of(
          Maps.immutableEntry("key", "value1"), Maps.immutableEntry("key", "value2"));

  EmbeddedChannel ch =
      new EmbeddedChannel(new HttpDownloadHandler(/* credentials= */ null, remoteHeaders));
  DownloadCommand cmd =
      new DownloadCommand(uri, /* casDownload= */ true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().getAll("key")).isEqualTo(Arrays.asList("value1", "value2"));
}
 
源代码6 项目: bazel   文件: HttpDownloadHandlerTest.java
/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test
public void httpErrorsAreSupported() throws IOException {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
  DownloadCommand cmd = new DownloadCommand(CACHE_URI, true, DIGEST, out);
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpResponse response =
      new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
  response.headers().set(HttpHeaders.HOST, "localhost");
  response.headers().set(HttpHeaders.CONTENT_LENGTH, 0);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  ch.writeInbound(response);
  ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).response().status())
      .isEqualTo(HttpResponseStatus.NOT_FOUND);
  // No data should have been written to the OutputStream and it should have been closed.
  assertThat(out.size()).isEqualTo(0);
  // The caller is responsible for closing the stream.
  verify(out, never()).close();
  assertThat(ch.isOpen()).isTrue();
}
 
源代码7 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void basicAuthShouldWork() throws Exception {
  URI uri = new URI("http://user:[email protected]/foo");
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  DownloadCommand cmd = new DownloadCommand(uri, true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().get(HttpHeaderNames.AUTHORIZATION))
      .isEqualTo("Basic dXNlcjpwYXNzd29yZA==");
}
 
源代码8 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void basicAuthShouldNotEnabled() throws Exception {
  URI uri = new URI("http://does.not.exist/foo");
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  DownloadCommand cmd = new DownloadCommand(uri, true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().contains(HttpHeaderNames.AUTHORIZATION)).isFalse();
}
 
源代码9 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void hostDoesntIncludePortHttp() throws Exception {
  URI uri = new URI("http://does.not.exist/foo");
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  DownloadCommand cmd = new DownloadCommand(uri, true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().get(HttpHeaderNames.HOST)).isEqualTo("does.not.exist");
}
 
源代码10 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void hostDoesntIncludePortHttps() throws Exception {
  URI uri = new URI("https://does.not.exist/foo");
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  DownloadCommand cmd = new DownloadCommand(uri, true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().get(HttpHeaderNames.HOST)).isEqualTo("does.not.exist");
}
 
源代码11 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void hostDoesIncludePort() throws Exception {
  URI uri = new URI("http://does.not.exist:8080/foo");
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  DownloadCommand cmd = new DownloadCommand(uri, true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().get(HttpHeaderNames.HOST)).isEqualTo("does.not.exist:8080");
}
 
源代码12 项目: bazel   文件: AbstractHttpHandlerTest.java
@Test
public void headersDoIncludeUserAgent() throws Exception {
  URI uri = new URI("http://does.not.exist:8080/foo");
  EmbeddedChannel ch =
      new EmbeddedChannel(new HttpDownloadHandler(/* credentials= */ null, ImmutableList.of()));
  DownloadCommand cmd =
      new DownloadCommand(uri, /* casDownload= */ true, DIGEST, new ByteArrayOutputStream());
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.headers().get(HttpHeaderNames.USER_AGENT)).isEqualTo("bazel/");
}
 
源代码13 项目: bazel   文件: HttpDownloadHandlerTest.java
private void downloadShouldWork(boolean casDownload, EmbeddedChannel ch) throws IOException {
  ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
  DownloadCommand cmd = new DownloadCommand(CACHE_URI, casDownload, DIGEST, out);
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.method()).isEqualTo(HttpMethod.GET);
  assertThat(request.headers().get(HttpHeaderNames.HOST)).isEqualTo(CACHE_URI.getHost());
  if (casDownload) {
    assertThat(request.uri()).isEqualTo("/cache-bucket/cas/" + DIGEST.getHash());
  } else {
    assertThat(request.uri()).isEqualTo("/cache-bucket/ac/" + DIGEST.getHash());
  }

  assertThat(writePromise.isDone()).isFalse();

  HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
  response.headers().set(HttpHeaders.CONTENT_LENGTH, 5);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  ch.writeInbound(response);
  ByteBuf content = Unpooled.buffer();
  content.writeBytes(new byte[] {1, 2, 3, 4, 5});
  ch.writeInbound(new DefaultLastHttpContent(content));

  assertThat(writePromise.isDone()).isTrue();
  assertThat(out.toByteArray()).isEqualTo(new byte[] {1, 2, 3, 4, 5});
  verify(out, never()).close();
  assertThat(ch.isActive()).isTrue();
}
 
源代码14 项目: bazel   文件: HttpDownloadHandlerTest.java
/**
 * Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND) with a
 * Content-Length header.
 */
@Test
public void httpErrorsWithContentAreSupported() throws IOException {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
  DownloadCommand cmd = new DownloadCommand(CACHE_URI, true, DIGEST, out);
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpResponse response =
      new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
  ByteBuf errorMessage = ByteBufUtil.writeAscii(ch.alloc(), "Error message");
  response.headers().set(HttpHeaders.HOST, "localhost");
  response
      .headers()
      .set(HttpHeaders.CONTENT_LENGTH, String.valueOf(errorMessage.readableBytes()));
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);

  ch.writeInbound(response);
  // The promise must not be done because we haven't received the error message yet.
  assertThat(writePromise.isDone()).isFalse();

  ch.writeInbound(new DefaultHttpContent(errorMessage));
  ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).response().status())
      .isEqualTo(HttpResponseStatus.NOT_FOUND);
  // No data should have been written to the OutputStream and it should have been closed.
  assertThat(out.size()).isEqualTo(0);
  // The caller is responsible for closing the stream.
  verify(out, never()).close();
  assertThat(ch.isOpen()).isFalse();
}