File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 60
60
<scope >test</scope >
61
61
</dependency >
62
62
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 >
63
70
</dependencies >
64
71
65
72
<!-- 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 32
32
import javax .servlet .http .HttpServletResponse ;
33
33
import org .junit .After ;
34
34
import org .junit .Before ;
35
+ import org .junit .Rule ;
35
36
import org .junit .Test ;
37
+ import org .junit .contrib .java .lang .system .EnvironmentVariables ;
36
38
import org .junit .runner .RunWith ;
37
39
import org .junit .runners .JUnit4 ;
38
40
@@ -45,6 +47,10 @@ public class SnippetsTests {
45
47
private ByteArrayOutputStream stdOut ;
46
48
private StringWriter responseOut ;
47
49
50
+ @ Rule
51
+ public final EnvironmentVariables environmentVariables
52
+ = new EnvironmentVariables ();
53
+
48
54
@ Before
49
55
public void beforeTest () throws Exception {
50
56
// Use a new mock for each test
@@ -167,6 +173,13 @@ public void helloBackgroundTest() throws IOException {
167
173
assertThat (responseOut .toString (), containsString ("Hello John!" ));
168
174
}
169
175
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
+
170
183
@ Test
171
184
public void helloExecutionCount () throws IOException {
172
185
new Concepts ().executionCount (request , response );
You can’t perform that action at this time.
0 commit comments