Java Code Examples for io.netty.channel.embedded.EmbeddedChannel#writeOneOutbound()

The following examples show how to use io.netty.channel.embedded.EmbeddedChannel#writeOneOutbound() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUploadHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: HttpUploadHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/** 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();
}
 
Example 3
Source File: HttpUploadHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 5
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example 6
Source File: HttpDownloadHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/** 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();
}
 
Example 7
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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==");
}
 
Example 8
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 9
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 10
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 11
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 12
Source File: AbstractHttpHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@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/");
}
 
Example 13
Source File: HttpDownloadHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: HttpDownloadHandlerTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}