@Override
  public HttpResponse sendAsIs(HttpRequest httpRequest) throws IOException {
    HttpURLConnection connection = connectionFactory.openConnection(httpRequest.url());
    connection.setRequestMethod(httpRequest.method().toString());
    httpRequest.headers().names().stream()
        .filter(headerName -> !Ascii.equalsIgnoreCase(headerName, USER_AGENT))
        .forEach(
            headerName ->
                httpRequest
                    .headers()
                    .getAll(headerName)
                    .forEach(
                        headerValue -> connection.setRequestProperty(headerName, headerValue)));
    connection.setRequestProperty(USER_AGENT, this.userAgent);

    if (ImmutableSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)
        .contains(httpRequest.method())) {
      connection.setDoOutput(true);
      ByteSource.wrap(httpRequest.requestBody().orElse(ByteString.EMPTY).toByteArray())
          .copyTo(connection.getOutputStream());
    }

    int responseCode = connection.getResponseCode();
    HttpHeaders.Builder responseHeadersBuilder = HttpHeaders.builder();
    for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
      String headerName = headerEntry.getKey();
      if (!isNullOrEmpty(headerName)) {
        for (String headerValue : headerEntry.getValue()) {
          if (!isNullOrEmpty(headerValue)) {
            responseHeadersBuilder.addHeader(headerName, headerValue);
          }
        }
      }
    }
    return HttpResponse.builder()
        .setStatus(HttpStatus.fromCode(responseCode))
        .setHeaders(responseHeadersBuilder.build())
        .setBodyBytes(ByteString.readFrom(connection.getInputStream()))
        .build();
  }
@Test
  public void sendAsIs_withPostRequest_returnsExpectedHttpResponse()
      throws IOException, InterruptedException {
    mockWebServer.setDispatcher(new SendAsIsTestDispatcher());
    mockWebServer.start();
    String requestBody = "POST BODY";
    String expectedResponseBody = SendAsIsTestDispatcher.buildBody("POST", requestBody);

    HttpUrl baseUrl = mockWebServer.url("/");
    String requestUrl =
        new URL(baseUrl.scheme(), baseUrl.host(), baseUrl.port(), "/send-as-is/%2e%2e/%2e%2e/path")
            .toString();

    HttpResponse response =
        httpClient.sendAsIs(
            post(requestUrl)
                .setRequestBody(ByteString.copyFrom(requestBody, UTF_8))
                .withEmptyHeaders()
                .build());

    assertThat(mockWebServer.takeRequest().getPath()).isEqualTo("/send-as-is/%2e%2e/%2e%2e/path");
    assertThat(response)
        .isEqualTo(
            HttpResponse.builder()
                .setStatus(HttpStatus.OK)
                .setHeaders(
                    HttpHeaders.builder()
                        .addHeader(CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString())
                        // MockWebServer always adds this response header.
                        .addHeader(CONTENT_LENGTH, String.valueOf(expectedResponseBody.length()))
                        .build())
                .setBodyBytes(ByteString.copyFrom(expectedResponseBody, UTF_8))
                .build());
  }