@Test
  public void testCopyHeadersRequestBuilder() {
    Map<String, String> transportHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> restHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> copiedHeaders = randomHeadersFrom(restHeaders);
    Set<String> usefulRestHeaders = new HashSet<>(copiedHeaders.keySet());
    usefulRestHeaders.addAll(randomMap(randomIntBetween(0, 10), "useful-").keySet());
    Map<String, String> restContext = randomContext(randomIntBetween(0, 10));
    Map<String, String> transportContext =
        Maps.difference(randomContext(randomIntBetween(0, 10)), restContext).entriesOnlyOnLeft();

    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.putAll(transportHeaders);
    expectedHeaders.putAll(copiedHeaders);

    Map<String, String> expectedContext = new HashMap<>();
    expectedContext.putAll(transportContext);
    expectedContext.putAll(restContext);

    Client client =
        client(new NoOpClient(), new FakeRestRequest(restHeaders, restContext), usefulRestHeaders);

    ActionRequestBuilder requestBuilders[] =
        new ActionRequestBuilder[] {
          client.prepareIndex("index", "type"),
          client.prepareGet("index", "type", "id"),
          client.prepareBulk(),
          client.prepareDelete(),
          client.prepareIndex(),
          client.prepareClearScroll(),
          client.prepareMultiGet(),
          client.prepareBenchStatus()
        };

    for (ActionRequestBuilder requestBuilder : requestBuilders) {
      putHeaders(requestBuilder.request(), transportHeaders);
      putContext(requestBuilder.request(), transportContext);
      assertHeaders(requestBuilder.request(), transportHeaders);
      requestBuilder.get();
      assertHeaders(requestBuilder.request(), expectedHeaders);
      assertContext(requestBuilder.request(), expectedContext);
    }
  }
  @Test
  public void testCopyHeadersIndicesAdminRequestBuilder() {
    Map<String, String> transportHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> restHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> copiedHeaders = randomHeadersFrom(restHeaders);
    Set<String> usefulRestHeaders = new HashSet<>(copiedHeaders.keySet());
    usefulRestHeaders.addAll(randomMap(randomIntBetween(0, 10), "useful-").keySet());
    Map<String, String> restContext = randomContext(randomIntBetween(0, 10));
    Map<String, String> transportContext =
        Maps.difference(randomContext(randomIntBetween(0, 10)), restContext).entriesOnlyOnLeft();

    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.putAll(transportHeaders);
    expectedHeaders.putAll(copiedHeaders);

    Map<String, String> expectedContext = new HashMap<>();
    expectedContext.putAll(transportContext);
    expectedContext.putAll(restContext);

    Client client =
        client(new NoOpClient(), new FakeRestRequest(restHeaders, restContext), usefulRestHeaders);

    ActionRequestBuilder requestBuilders[] =
        new ActionRequestBuilder[] {
          client.admin().indices().prepareValidateQuery(),
          client.admin().indices().prepareCreate("test"),
          client.admin().indices().prepareAliases(),
          client.admin().indices().prepareAnalyze("text"),
          client.admin().indices().prepareDeleteWarmer(),
          client.admin().indices().prepareTypesExists("type"),
          client.admin().indices().prepareClose()
        };

    for (ActionRequestBuilder requestBuilder : requestBuilders) {
      putHeaders(requestBuilder.request(), transportHeaders);
      putContext(requestBuilder.request(), transportContext);
      assertHeaders(requestBuilder.request(), transportHeaders);
      requestBuilder.get();
      assertHeaders(requestBuilder.request(), expectedHeaders);
      assertContext(requestBuilder.request(), expectedContext);
    }
  }
  @Test
  public void testCopyHeadersClusterAdminRequestBuilder() {
    Map<String, String> transportHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> restHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> copiedHeaders = randomHeadersFrom(restHeaders);
    Set<String> usefulRestHeaders = new HashSet<>(copiedHeaders.keySet());
    usefulRestHeaders.addAll(randomMap(randomIntBetween(0, 10), "useful-").keySet());
    Map<String, String> restContext = randomContext(randomIntBetween(0, 10));
    Map<String, String> transportContext =
        Maps.difference(randomContext(randomIntBetween(0, 10)), restContext).entriesOnlyOnLeft();

    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.putAll(transportHeaders);
    expectedHeaders.putAll(copiedHeaders);

    Map<String, String> expectedContext = new HashMap<>();
    expectedContext.putAll(transportContext);
    expectedContext.putAll(restContext);

    Client client =
        client(new NoOpClient(), new FakeRestRequest(restHeaders, restContext), usefulRestHeaders);

    ActionRequestBuilder requestBuilders[] =
        new ActionRequestBuilder[] {
          client.admin().cluster().prepareNodesInfo(),
          client.admin().cluster().prepareClusterStats(),
          client.admin().cluster().prepareState(),
          client.admin().cluster().prepareCreateSnapshot("repo", "name"),
          client.admin().cluster().prepareHealth(),
          client.admin().cluster().prepareReroute()
        };

    for (ActionRequestBuilder requestBuilder : requestBuilders) {
      putHeaders(requestBuilder.request(), transportHeaders);
      putContext(requestBuilder.request(), transportContext);
      assertHeaders(requestBuilder.request(), transportHeaders);
      requestBuilder.get();
      assertHeaders(requestBuilder.request(), expectedHeaders);
      assertContext(requestBuilder.request(), expectedContext);
    }
  }