@@ -0,0 +1,3056 @@
+/*%0A * Copyright 2020 Google LLC%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.example.functions.helloworld;%0A%0A// %5BSTART functions_http_unit_test%5D%0Aimport static com.google.common.truth.Truth.assertThat;%0Aimport static org.powermock.api.mockito.PowerMockito.mock;%0Aimport static org.powermock.api.mockito.PowerMockito.when;%0A%0Aimport com.google.cloud.functions.HttpRequest;%0Aimport com.google.cloud.functions.HttpResponse;%0Aimport com.google.common.testing.TestLogHandler;%0Aimport java.io.BufferedReader;%0Aimport java.io.BufferedWriter;%0Aimport java.io.IOException;%0Aimport java.io.StringReader;%0Aimport java.io.StringWriter;%0Aimport java.util.Optional;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.JUnit4;%0Aimport org.mockito.Mock;%0Aimport org.mockito.Mockito;%0A%0A@RunWith(JUnit4.class)%0Apublic class HelloHttpTest %7B%0A  @Mock private HttpRequest request;%0A  @Mock private HttpResponse response;%0A%0A  private BufferedWriter writerOut;%0A  private StringWriter responseOut;%0A%0A  private static final TestLogHandler logHandler = new TestLogHandler();%0A%0A  @Before%0A  public void beforeTest() throws IOException %7B%0A    Mockito.mockitoSession().initMocks(this);%0A%0A    request = mock(HttpRequest.class);%0A    response = mock(HttpResponse.class);%0A%0A    BufferedReader reader = new BufferedReader(new StringReader(%22%7B%7D%22));%0A    when(request.getReader()).thenReturn(reader);%0A%0A    responseOut = new StringWriter();%0A    writerOut = new BufferedWriter(responseOut);%0A    when(response.getWriter()).thenReturn(writerOut);%0A%0A    logHandler.clear();%0A  %7D%0A%0A  @Test%0A  public void helloHttp_noParamsGet() throws IOException %7B%0A    new HelloHttp().service(request, response);%0A%0A    writerOut.flush();%0A    assertThat(responseOut.toString()).isEqualTo(%22Hello world!%22);%0A  %7D%0A  // %5BEND functions_http_unit_test%5D%0A%0A  @Test%0A  public void helloHttp_urlParamsGet() throws IOException %7B%0A    when(request.getFirstQueryParameter(%22name%22)).thenReturn(Optional.of(%22Tom%22));%0A%0A    new HelloHttp().service(request, response);%0A%0A    writerOut.flush();%0A    assertThat(responseOut.toString()).isEqualTo(%22Hello Tom!%22);%0A  %7D%0A%0A  @Test%0A  public void helloHttp_bodyParamsPost() throws IOException %7B%0A    BufferedReader jsonReader = new BufferedReader(new StringReader(%22%7B'name': 'Jane'%7D%22));%0A%0A    when(request.getReader()).thenReturn(jsonReader);%0A%0A    new HelloHttp().service(request, response);%0A    writerOut.flush();%0A%0A    assertThat(responseOut.toString()).isEqualTo(%22Hello Jane!%22);%0A  %7D%0A  // %5BSTART functions_http_unit_test%5D%0A%7D%0A// %5BEND functions_http_unit_test%5D%0A

Add HTTP unit test sample (#2629)