public static Storage.Builder newStorageClient(GcsOptions options) {
    String applicationName =
        String.format(
            "%sapache-beam/%s (GPN:Beam)",
            isNullOrEmpty(options.getAppName()) ? "" : options.getAppName() + " ",
            ReleaseInfo.getReleaseInfo().getSdkVersion());

    String servicePath = options.getGcsEndpoint();

    Storage.Builder storageBuilder =
        new Storage.Builder(
                getTransport(), getJsonFactory(), httpRequestInitializerFromOptions(options))
            .setApplicationName(applicationName)
            .setGoogleClientRequestInitializer(options.getGoogleApiTrace());
    if (servicePath != null) {
      ApiComponents components = apiComponentsFromUrl(servicePath);
      storageBuilder.setRootUrl(components.rootUrl);
      storageBuilder.setServicePath(components.servicePath);
      storageBuilder.setBatchPath(Paths.get("batch/", components.servicePath).toString());
    }
    return storageBuilder;
  }
@Test
  public void testUserAgentAndCustomAuditInGcsRequestHeaders() throws IOException {
    GcsOptions gcsOptions = PipelineOptionsFactory.as(GcsOptions.class);
    gcsOptions.setGcpCredential(new TestCredential());
    gcsOptions.setJobName("test-job");
    gcsOptions.setAppName("test-app");

    Storage storageClient = Transport.newStorageClient(gcsOptions).build();
    Storage.Objects.Get getObject = storageClient.objects().get("test-bucket", "test-object");
    HttpRequest request = getObject.buildHttpRequest();

    // An example of user agent string will be like
    // "test-app apache-beam/2.57.0.dev (GPN:Beam) Google-API-Java-Client/2.0.0"
    // For a valid user-agent string, a comment like "(GPN:Beam)" cannot be the first token.
    // https://www.rfc-editor.org/rfc/rfc7231#section-5.5.3
    assertThat(
        Arrays.asList(request.getHeaders().getUserAgent().split(" ")).indexOf("test-app"),
        greaterThanOrEqualTo(0));

    assertThat(
        Arrays.asList(request.getHeaders().getUserAgent().split(" "))
            .indexOf(String.format("apache-beam/%s", ReleaseInfo.getReleaseInfo().getSdkVersion())),
        greaterThan(0));

    assertThat(
        Arrays.asList(request.getHeaders().getUserAgent().split(" ")).indexOf("(GPN:Beam)"),
        greaterThan(0));

    // there should be one and only one custom audit entry for job name
    assertEquals(
        request.getHeaders().getHeaderStringValues("x-goog-custom-audit-job"),
        Collections.singletonList("test-job"));
  }