Skip to content

Commit c13df28

Browse files
authored
feat: implement http_unit_test example (#183)
1 parent 9797d88 commit c13df28

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ if (BUILD_TESTING)
5757
find_package(GTest CONFIG REQUIRED)
5858
set(googleapis_functions_framework_examples_unit_tests
5959
# cmake-format: sort
60-
cloud_event_examples_test.cc http_examples_test.cc site_test.cc)
60+
cloud_event_examples_test.cc http_examples_test.cc http_unit_test.cc
61+
site_test.cc)
6162

6263
foreach (fname ${googleapis_functions_framework_examples_unit_tests})
6364
string(REPLACE "/" "_" target "${fname}")

examples/http_unit_test.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START functions_http_unit_test]
16+
#include "google/cloud/functions/http_request.h"
17+
#include "google/cloud/functions/http_response.h"
18+
#include <gtest/gtest.h>
19+
20+
namespace gcf = ::google::cloud::functions;
21+
extern gcf::HttpResponse hello_world_http(gcf::HttpRequest request);
22+
23+
namespace {
24+
25+
TEST(HttpUnitTest, Success) {
26+
auto actual = hello_world_http(
27+
gcf::HttpRequest{}.set_payload(R"js({ "name": "Foo" })js"));
28+
EXPECT_EQ(actual.payload(), "Hello Foo!");
29+
30+
actual = hello_world_http(
31+
gcf::HttpRequest{}.set_payload(R"js({ "unused": 7 })js"));
32+
EXPECT_EQ(actual.payload(), "Hello World!");
33+
34+
actual = hello_world_http(gcf::HttpRequest{}.set_payload("Bar"));
35+
EXPECT_EQ(actual.payload(), "Hello World!");
36+
}
37+
38+
} // namespace
39+
// [END functions_http_unit_test]

0 commit comments

Comments
 (0)