@Test
  public void errorResponseTest() {
    EmbeddedChannel channel = createEmbeddedChannel();
    for (RestServiceErrorCode errorCode : RestServiceErrorCode.values()) {
      for (boolean includeExceptionMessageInResponse : new boolean[]{true, false}) {
        HttpHeaders httpHeaders = new DefaultHttpHeaders();
        httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, errorCode);
        if (includeExceptionMessageInResponse) {
          httpHeaders.set(MockNettyMessageProcessor.INCLUDE_EXCEPTION_MESSAGE_IN_RESPONSE_HEADER_NAME, "true");
        }
        channel.writeInbound(
            RestTestUtils.createRequest(HttpMethod.HEAD, TestingUri.OnResponseCompleteWithRestException.toString(),
                httpHeaders));
        HttpResponse response = channel.readOutbound();
        HttpResponseStatus expectedStatus = getExpectedHttpResponseStatus(errorCode);
        assertEquals("Unexpected response status", expectedStatus, response.status());
        boolean containsFailureReasonHeader = response.headers().contains(NettyResponseChannel.FAILURE_REASON_HEADER);
        if (expectedStatus == HttpResponseStatus.BAD_REQUEST
            || expectedStatus == HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE || includeExceptionMessageInResponse) {
          assertTrue("Could not find failure reason header.", containsFailureReasonHeader);
        } else {
          assertFalse("Should not have found failure reason header.", containsFailureReasonHeader);
        }
        if (HttpStatusClass.CLIENT_ERROR.contains(response.status().code())) {
          assertEquals("Wrong error code", errorCode,
              RestServiceErrorCode.valueOf(response.headers().get(NettyResponseChannel.ERROR_CODE_HEADER)));
        } else {
          assertFalse("Should not have found error code header",
              response.headers().contains(NettyResponseChannel.ERROR_CODE_HEADER));
        }
        if (expectedStatus == HttpResponseStatus.METHOD_NOT_ALLOWED) {
          assertEquals("Unexpected value for " + HttpHeaderNames.ALLOW,
              MockNettyMessageProcessor.METHOD_NOT_ALLOWED_ALLOW_HEADER_VALUE,
              response.headers().get(HttpHeaderNames.ALLOW));
        }
        if (response instanceof FullHttpResponse) {
          // assert that there is no content
          assertEquals("The response should not contain content", 0,
              ((FullHttpResponse) response).content().readableBytes());
        } else {
          HttpContent content = channel.readOutbound();
          assertTrue("End marker should be received", content instanceof LastHttpContent);
          content.release();
        }
        assertNull("There should be no more data in the channel", channel.readOutbound());
        boolean shouldBeAlive = !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(expectedStatus);
        assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
        assertEquals("Connection header should be consistent with channel state", shouldBeAlive,
            HttpUtil.isKeepAlive(response));
        if (!shouldBeAlive) {
          channel = createEmbeddedChannel();
        }
      }
    }
    channel.close();
  }
Tests that error responses are correctly formed.
ambry-rest/src/test/java/com/github/ambry/rest/NettyResponseChannelTest.java