2323import java .io .IOException ;
2424import java .net .URI ;
2525import java .net .http .HttpClient ;
26+ import java .net .http .HttpRequest ;
2627import java .net .http .HttpResponse ;
2728import org .junit .Test ;
2829import org .junit .runner .RunWith ;
@@ -34,6 +35,12 @@ public class ExampleSystemIT {
3435 // TODO<developer>: set this value, as an environment variable or within your test code
3536 private static final String BASE_URL = System .getenv ("FUNCTIONS_BASE_URL" );
3637
38+ // Access token used to send requests to authenticated-only functions
39+ // TODO<developer>: Set this value if your function requires authentication.
40+ // See the documentation for more info:
41+ // https://cloud.google.com/functions/docs/securing/authenticating
42+ private static final String ACCESS_TOKEN = System .getenv ("FUNCTIONS_ACCESS_TOKEN" );
43+
3744 private static HttpClient client = HttpClient .newHttpClient ();
3845
3946 @ Test
@@ -48,8 +55,16 @@ public void helloHttp_shouldRunWithFunctionsFramework() throws IOException, Inte
4855 }
4956
5057 // [START functions_http_system_test]
51- java .net .http .HttpRequest getRequest =
52- java .net .http .HttpRequest .newBuilder ().uri (URI .create (functionUrl )).GET ().build ();
58+ HttpRequest .Builder getRequestBuilder = java .net .http .HttpRequest .newBuilder ()
59+ .uri (URI .create (functionUrl ))
60+ .GET ();
61+
62+ // Used to test functions that require authenticated invokers
63+ if (ACCESS_TOKEN != null ) {
64+ getRequestBuilder .header ("Authorization" , "Bearer " + ACCESS_TOKEN );
65+ }
66+
67+ java .net .http .HttpRequest getRequest = getRequestBuilder .build ();
5368
5469 HttpResponse response = client .send (getRequest , HttpResponse .BodyHandlers .ofString ());
5570 assertThat (response .body ().toString ()).isEqualTo ("Hello world!" );
0 commit comments