Skip to content

Commit 31a721b

Browse files
author
Ace Nassri
authored
chore(functions): fix dep. on allow-unauthenticated (GoogleCloudPlatform#6672)
* chore(functions): fix dep. on allow-unauthenticated * Optimize imports * Fix misleading comment * Add link to docs
1 parent f14dfc4 commit 31a721b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

functions/helloworld/hello-http/src/test/java/functions/ExampleSystemIT.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.net.URI;
2525
import java.net.http.HttpClient;
26+
import java.net.http.HttpRequest;
2627
import java.net.http.HttpResponse;
2728
import org.junit.Test;
2829
import 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

Comments
 (0)