@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);
    }
  }
 void checkWriteAction(
     boolean autoCreateIndex, TimeValue timeout, ActionRequestBuilder<?, ?, ?, ?> builder) {
   // we clean the metadata when loosing a master, therefore all operations on indices will auto
   // create it, if allowed
   long now = System.currentTimeMillis();
   try {
     builder.get();
     fail("expected ClusterBlockException or MasterNotDiscoveredException");
   } catch (ClusterBlockException | MasterNotDiscoveredException e) {
     if (e instanceof MasterNotDiscoveredException) {
       assertTrue(autoCreateIndex);
     } else {
       assertFalse(autoCreateIndex);
     }
     // verify we waited before giving up...
     assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
     assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
   }
 }