/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package functions;

// [START functions_http_system_test]

import static com.google.common.truth.Truth.assertThat;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ExampleSystemIT {
  // Root URL pointing to your Cloud Functions deployment
  // TODO<developer>: set this value, as an environment variable or within your test code
  private static final String BASE_URL = System.getenv("FUNCTIONS_BASE_URL");

  private static HttpClient client = HttpClient.newHttpClient();

  @Test
  public void helloHttp_shouldRunWithFunctionsFramework() throws IOException, InterruptedException {
    String functionUrl = BASE_URL + "/HelloHttp";

    // [END functions_http_system_test]
    // Skip this test if FUNCTIONS_BASE_URL is not set
    if (BASE_URL == null) {
      System.out.println("FUNCTIONS_BASE_URL is not set; skipping ExampleSystemIT.");
      return;
    }

    // [START functions_http_system_test]
    java.net.http.HttpRequest getRequest =
        java.net.http.HttpRequest.newBuilder().uri(URI.create(functionUrl)).GET().build();

    HttpResponse response = client.send(getRequest, HttpResponse.BodyHandlers.ofString());
    assertThat(response.body().toString()).isEqualTo("Hello world!");
  }
}
// [END functions_http_system_test]

@@ -782,16 +782,50 @@
 Client;%0A
+import java.net.http.HttpRequest;%0A
 import j
@@ -851,16 +851,16 @@
 sponse;%0A
-
 import o
@@ -1222,16 +1222,382 @@
 URL%22);%0A%0A
+  // Access token used to send requests to authenticated-only functions%0A  // TODO%3Cdeveloper%3E: Set this value if your function requires authentication.%0A  //                  See the documentation for more info:%0A  // https://cloud.google.com/functions/docs/securing/authenticating%0A  private static final String ACCESS_TOKEN = System.getenv(%22FUNCTIONS_ACCESS_TOKEN%22);%0A%0A
   privat
@@ -2086,38 +2086,24 @@
 m_test%5D%0A    
-java.net.http.
 HttpRequest 
@@ -2101,16 +2101,24 @@
 pRequest
+.Builder
  getRequ
@@ -2124,18 +2124,17 @@
 uest
- =%0A       
+Builder =
  jav
@@ -2168,16 +2168,25 @@
 uilder()
+%0A        
 .uri(URI
@@ -2210,14 +2210,266 @@
 rl))
-.GET()
+%0A        .GET();%0A%0A    // Used to test functions that require authenticated invokers%0A    if (ACCESS_TOKEN != null) %7B%0A      getRequestBuilder.header(%22Authorization%22, %22Bearer %22 + ACCESS_TOKEN);%0A    %7D%0A%0A    java.net.http.HttpRequest getRequest = getRequestBuilder
 .bui

fix dep. on allow-unauthenticated (#6672)