Skip to content

Commit 04e463e

Browse files
author
Ace Nassri
authored
Fix incorrect region tag on + move HTTP methods sample (GoogleCloudPlatform#2626)
1 parent 8835053 commit 04e463e

3 files changed

Lines changed: 40 additions & 40 deletions

File tree

functions/snippets/helloworld/src/test/java/com/example/functions/helloworld/HelloWorldSnippetsTest.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package com.example.functions.helloworld;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static org.mockito.Mockito.times;
21-
import static org.mockito.Mockito.verify;
2220
import static org.powermock.api.mockito.PowerMockito.mock;
2321
import static org.powermock.api.mockito.PowerMockito.when;
2422

@@ -33,7 +31,6 @@
3331
import java.io.IOException;
3432
import java.io.StringReader;
3533
import java.io.StringWriter;
36-
import java.net.HttpURLConnection;
3734
import java.util.Date;
3835
import java.util.List;
3936
import java.util.Optional;
@@ -162,37 +159,4 @@ public void helloHttp_bodyParamsPost() throws IOException {
162159

163160
assertThat(responseOut.toString()).isEqualTo("Hello Jane!");
164161
}
165-
166-
@Test
167-
public void functionsHelloworldMethod_shouldAcceptGet() throws IOException {
168-
when(request.getMethod()).thenReturn("GET");
169-
170-
new HelloMethod().service(request, response);
171-
172-
writerOut.flush();
173-
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_OK);
174-
assertThat(responseOut.toString()).isEqualTo("Hello world!");
175-
}
176-
177-
@Test
178-
public void functionsHelloworldMethod_shouldForbidPut() throws IOException {
179-
when(request.getMethod()).thenReturn("PUT");
180-
181-
new HelloMethod().service(request, response);
182-
183-
writerOut.flush();
184-
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_FORBIDDEN);
185-
assertThat(responseOut.toString()).isEqualTo("Forbidden!");
186-
}
187-
188-
@Test
189-
public void functionsHelloworldMethod_shouldErrorOnPost() throws IOException {
190-
when(request.getMethod()).thenReturn("POST");
191-
192-
new HelloMethod().service(request, response);
193-
194-
writerOut.flush();
195-
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_METHOD);
196-
assertThat(responseOut.toString()).isEqualTo("Something blew up!");
197-
}
198162
}

functions/snippets/helloworld/src/main/java/com/example/functions/helloworld/HelloMethod.java renamed to functions/snippets/http/src/main/java/com/example/functions/http/HttpMethod.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.functions.helloworld;
17+
package com.example.functions.http;
1818

19-
// [START functions_helloworld_method]
19+
// [START functions_http_method]
2020

2121
import com.google.cloud.functions.HttpFunction;
2222
import com.google.cloud.functions.HttpRequest;
@@ -25,7 +25,7 @@
2525
import java.io.IOException;
2626
import java.net.HttpURLConnection;
2727

28-
public class HelloMethod implements HttpFunction {
28+
public class HttpMethod implements HttpFunction {
2929
@Override
3030
public void service(HttpRequest request, HttpResponse response)
3131
throws IOException {
@@ -48,4 +48,4 @@ public void service(HttpRequest request, HttpResponse response)
4848
}
4949
}
5050
}
51-
// [END functions_helloworld_method]
51+
// [END functions_http_method]

functions/snippets/http/src/test/java/com/example/functions/http/HttpTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.example.functions.http;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.times;
21+
import static org.mockito.Mockito.verify;
2022
import static org.powermock.api.mockito.PowerMockito.mock;
2123
import static org.powermock.api.mockito.PowerMockito.when;
2224

@@ -30,6 +32,7 @@
3032
import java.io.InputStream;
3133
import java.io.StringReader;
3234
import java.io.StringWriter;
35+
import java.net.HttpURLConnection;
3336
import java.nio.charset.StandardCharsets;
3437
import java.util.Base64;
3538
import java.util.Optional;
@@ -157,4 +160,37 @@ public void parseContentTypeTest_form() throws IOException {
157160
writerOut.flush();
158161
assertThat(responseOut.toString()).contains("Hello John!");
159162
}
163+
164+
@Test
165+
public void functionsHelloworldMethod_shouldAcceptGet() throws IOException {
166+
when(request.getMethod()).thenReturn("GET");
167+
168+
new HttpMethod().service(request, response);
169+
170+
writerOut.flush();
171+
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_OK);
172+
assertThat(responseOut.toString()).isEqualTo("Hello world!");
173+
}
174+
175+
@Test
176+
public void functionsHelloworldMethod_shouldForbidPut() throws IOException {
177+
when(request.getMethod()).thenReturn("PUT");
178+
179+
new HttpMethod().service(request, response);
180+
181+
writerOut.flush();
182+
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_FORBIDDEN);
183+
assertThat(responseOut.toString()).isEqualTo("Forbidden!");
184+
}
185+
186+
@Test
187+
public void functionsHelloworldMethod_shouldErrorOnPost() throws IOException {
188+
when(request.getMethod()).thenReturn("POST");
189+
190+
new HttpMethod().service(request, response);
191+
192+
writerOut.flush();
193+
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_METHOD);
194+
assertThat(responseOut.toString()).isEqualTo("Something blew up!");
195+
}
160196
}

0 commit comments

Comments
 (0)