File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 6060 <scope >test</scope >
6161 </dependency >
6262
63+ <!-- Required for mocking env vars -->
64+ <dependency >
65+ <groupId >com.github.stefanbirkner</groupId >
66+ <artifactId >system-rules</artifactId >
67+ <version >1.19.0</version >
68+ <scope >test</scope >
69+ </dependency >
6370 </dependencies >
6471
6572 <!-- Required for Java 8 (Alpha) functions in the inline editor -->
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2019 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ // [START functions_env_vars]
18+ import java .io .IOException ;
19+ import java .io .PrintWriter ;
20+ import javax .servlet .http .HttpServletRequest ;
21+ import javax .servlet .http .HttpServletResponse ;
22+
23+ public class EnvVars {
24+ // Returns the environment variable "foo" set during function deployment.
25+ public void envVar (HttpServletRequest request , HttpServletResponse response )
26+ throws IOException {
27+ PrintWriter writer = response .getWriter ();
28+ String foo = System .getenv ("FOO" );
29+ if (foo == null ) {
30+ foo = "Specified environment variable is not set." ;
31+ }
32+ writer .write (foo );
33+ }
34+ }
35+
36+ // [END functions_env_vars]
Original file line number Diff line number Diff line change 3232import javax .servlet .http .HttpServletResponse ;
3333import org .junit .After ;
3434import org .junit .Before ;
35+ import org .junit .Rule ;
3536import org .junit .Test ;
37+ import org .junit .contrib .java .lang .system .EnvironmentVariables ;
3638import org .junit .runner .RunWith ;
3739import org .junit .runners .JUnit4 ;
3840
@@ -45,6 +47,10 @@ public class SnippetsTests {
4547 private ByteArrayOutputStream stdOut ;
4648 private StringWriter responseOut ;
4749
50+ @ Rule
51+ public final EnvironmentVariables environmentVariables
52+ = new EnvironmentVariables ();
53+
4854 @ Before
4955 public void beforeTest () throws Exception {
5056 // Use a new mock for each test
@@ -167,6 +173,13 @@ public void helloBackgroundTest() throws IOException {
167173 assertThat (responseOut .toString (), containsString ("Hello John!" ));
168174 }
169175
176+ @ Test
177+ public void envTest () throws IOException {
178+ environmentVariables .set ("FOO" , "BAR" );
179+ new EnvVars ().envVar (request , response );
180+ assertThat (responseOut .toString (), containsString ("BAR" ));
181+ }
182+
170183 @ Test
171184 public void helloExecutionCount () throws IOException {
172185 new Concepts ().executionCount (request , response );
You can’t perform that action at this time.
0 commit comments