From 9d96d7e98b7f9e3da892677a6a6cf15905374cd2 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 10:32:45 -0800 Subject: [PATCH 1/7] Fixed incorrectly resolved conflicts --- script/release-binary | 3 --- 1 file changed, 3 deletions(-) diff --git a/script/release-binary b/script/release-binary index 4c7d6b5882d..0ff4dd63f91 100755 --- a/script/release-binary +++ b/script/release-binary @@ -44,7 +44,6 @@ sha256sum "${BINARY_NAME}" > "${SHA256_NAME}" # Copy it to the bucket. echo "Copying ${BINARY_NAME} ${SHA256_NAME} to ${DST}/" gsutil cp "${BINARY_NAME}" "${SHA256_NAME}" "${DST}/" -<<<<<<< HEAD # Build the debug binary BINARY_NAME="envoy-debug-${SHA}.tar.gz" @@ -57,5 +56,3 @@ sha256sum "${BINARY_NAME}" > "${SHA256_NAME}" # Copy it to the bucket. echo "Copying ${BINARY_NAME} ${SHA256_NAME} to ${DST}/" gsutil cp "${BINARY_NAME}" "${SHA256_NAME}" "${DST}/" -======= ->>>>>>> origin/rate_limiting From a7098adcda005359967d75c3ebec88461231e3b3 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 14:16:10 -0800 Subject: [PATCH 2/7] Added unit test cases for rate limiting --- .../src/api_manager/service_control/BUILD | 13 ++ .../service_control/aggregated_test.cc | 154 ++++++++++++++++ .../allocate_quota_response_test.cc | 165 ++++++++++++++++++ .../api_manager/service_control/proto_test.cc | 76 ++++++++ .../testdata/allocate_quota_request.golden | 36 ++++ ...locate_quota_request_no_method_name.golden | 35 ++++ ...ocate_quota_request_no_metrics_rule.golden | 24 +++ .../api_manager/service_control/url_test.cc | 4 + 8 files changed, 507 insertions(+) create mode 100644 contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc create mode 100644 contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request.golden create mode 100644 contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_method_name.golden create mode 100644 contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden diff --git a/contrib/endpoints/src/api_manager/service_control/BUILD b/contrib/endpoints/src/api_manager/service_control/BUILD index 73b722b4808..c0e35adca07 100644 --- a/contrib/endpoints/src/api_manager/service_control/BUILD +++ b/contrib/endpoints/src/api_manager/service_control/BUILD @@ -122,3 +122,16 @@ cc_test( "//external:googletest_main", ], ) + +cc_test( + name = "allocate_quota_response_test", + size = "small", + srcs = [ + "allocate_quota_response_test.cc", + ], + linkstatic = 1, + deps = [ + ":service_control", + "//external:googletest_main", + ], +) diff --git a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc index 5e9ca38a55d..5a4109f22aa 100644 --- a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc @@ -19,10 +19,13 @@ #include "contrib/endpoints/src/api_manager/mock_api_manager_environment.h" #include "contrib/endpoints/src/api_manager/service_control/proto.h" #include "gmock/gmock.h" +#include "google/protobuf/text_format.h" #include "gtest/gtest.h" using ::google::api::servicecontrol::v1::CheckRequest; using ::google::api::servicecontrol::v1::CheckResponse; +using ::google::api::servicecontrol::v1::AllocateQuotaRequest; +using ::google::api::servicecontrol::v1::AllocateQuotaResponse; using ::google::api::servicecontrol::v1::ReportRequest; using ::google::api::servicecontrol::v1::ReportResponse; using ::google::api_manager::utils::Status; @@ -39,6 +42,39 @@ namespace api_manager { namespace service_control { namespace { + +const char kAllocateQuotaResponse[] = R"( +operation_id: "test_service" +quota_metrics { + metric_name: "serviceruntime.googleapis.com/api/consumer/quota_used_count" + metric_values { + labels { + key: "/quota_name" + value: "metric_first" + } + int64_value: 2 + } + metric_values { + labels { + key: "/quota_name" + value: "metric" + } + int64_value: 1 + } +}service_config_id: "2017-02-08r9" + +)"; + +const char kAllocateQuotaResponseErrorExhausted[] = R"( +operation_id: "test_service" +allocate_errors { + code: RESOURCE_EXHAUSTED + description: "Insufficient tokens for quota group and limit \'apiWriteQpsPerProject_LOW\' of service \'jaebonginternal.sandbox.google.com\', using the limit by ID \'container:1002409420961\'." +} +service_config_id: "2017-02-08r9" + +)"; + void FillOperationInfo(OperationInfo* op) { op->operation_id = "operation_id"; op->operation_name = "operation_name"; @@ -195,6 +231,124 @@ TEST_F(AggregatedTestWithRealClient, CheckOKTest) { EXPECT_EQ(stat.send_report_operations, 0); } +class QuotaAllocationFailedTestWithRealClient : public ::testing::Test { + public: + void SetUp() { + service_.set_name("test_service"); + service_.mutable_control()->set_environment( + "servicecontrol.googleapis.com"); + env_.reset(new ::testing::NiceMock); + sc_lib_.reset(Aggregated::Create(service_, nullptr, env_.get(), nullptr)); + ASSERT_TRUE((bool)(sc_lib_)); + // This is the call actually creating the client. + sc_lib_->Init(); + } + + void DoRunHTTPRequest(HTTPRequest* request) { + std::map headers; + AllocateQuotaResponse allocate_quota_response_; + + ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( + kAllocateQuotaResponseErrorExhausted, &allocate_quota_response_)); + + std::string body = allocate_quota_response_.SerializeAsString(); + + request->OnComplete(Status::OK, std::move(headers), std::move(body)); + } + + ::google::api::Service service_; + std::unique_ptr env_; + std::unique_ptr sc_lib_; +}; + +TEST_F(QuotaAllocationFailedTestWithRealClient, AllocateQuotaTest) { + EXPECT_CALL(*env_, DoRunHTTPRequest(_)) + .WillOnce(Invoke( + this, &QuotaAllocationFailedTestWithRealClient::DoRunHTTPRequest)); + + std::vector> metric_cost_vector; + metric_cost_vector.push_back( + std::make_pair("metric_first", 1)); + metric_cost_vector.push_back( + std::make_pair("metric_second", 2)); + + QuotaRequestInfo info; + + info.metric_cost_vector = &metric_cost_vector; + + FillOperationInfo(&info); + sc_lib_->Quota(info, nullptr, [](Status status) { + ASSERT_TRUE(status.code() == Code::RESOURCE_EXHAUSTED); + }); +} + +class QuotaAllocationTestWithRealClient : public ::testing::Test { + public: + void SetUp() { + service_.set_name("test_service"); + service_.mutable_control()->set_environment( + "servicecontrol.googleapis.com"); + env_.reset(new ::testing::NiceMock); + sc_lib_.reset(Aggregated::Create(service_, nullptr, env_.get(), nullptr)); + ASSERT_TRUE((bool)(sc_lib_)); + // This is the call actually creating the client. + sc_lib_->Init(); + } + + void DoRunHTTPRequest(HTTPRequest* request) { + std::map headers; + AllocateQuotaRequest allocate_quota_request_; + AllocateQuotaResponse allocate_quota_response_; + + ASSERT_TRUE(allocate_quota_request_.ParseFromString(request->body())); + ASSERT_EQ(allocate_quota_request_.allocate_operation().quota_metrics_size(), + 2); + + std::unordered_map metric_rules; + for (auto rule : + allocate_quota_request_.allocate_operation().quota_metrics()) { + metric_rules[rule.metric_name()] = rule.metric_values(0).int64_value(); + } + ASSERT_EQ(metric_rules.size(), 2); + + ASSERT_NE(metric_rules.find("metric_first"), metric_rules.end()); + ASSERT_NE(metric_rules.find("metric_second"), metric_rules.end()); + ASSERT_EQ(metric_rules["metric_first"], 1); + ASSERT_EQ(metric_rules["metric_second"], 2); + + ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( + kAllocateQuotaResponse, &allocate_quota_response_)); + + std::string body = allocate_quota_response_.SerializeAsString(); + + request->OnComplete(Status::OK, std::move(headers), std::move(body)); + } + + ::google::api::Service service_; + std::unique_ptr env_; + std::unique_ptr sc_lib_; +}; + +TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { + EXPECT_CALL(*env_, DoRunHTTPRequest(_)) + .WillOnce( + Invoke(this, &QuotaAllocationTestWithRealClient::DoRunHTTPRequest)); + + std::vector> metric_cost_vector; + metric_cost_vector.push_back( + std::make_pair("metric_first", 1)); + metric_cost_vector.push_back( + std::make_pair("metric_second", 2)); + + QuotaRequestInfo info; + + info.metric_cost_vector = &metric_cost_vector; + + FillOperationInfo(&info); + sc_lib_->Quota(info, nullptr, + [](Status status) { ASSERT_TRUE(status.ok()); }); +} + TEST(AggregatedServiceControlTest, Create) { // Verify that invalid service config yields nullptr. ::google::api::Service diff --git a/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc b/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc new file mode 100644 index 00000000000..34b4bcc917e --- /dev/null +++ b/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc @@ -0,0 +1,165 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +// +#include "contrib/endpoints/include/api_manager/utils/status.h" +#include "contrib/endpoints/src/api_manager/service_control/proto.h" +#include "gtest/gtest.h" + +namespace gasv1 = ::google::api::servicecontrol::v1; + +using ::google::api::servicecontrol::v1::QuotaError; +using ::google::api_manager::utils::Status; +using ::google::protobuf::util::error::Code; + +namespace google { +namespace api_manager { +namespace service_control { + +namespace { + +Status ConvertAllocateQuotaErrorToStatus(gasv1::QuotaError::Code code, + const char* error_detail, + const char* service_name) { + gasv1::AllocateQuotaResponse response; + gasv1::QuotaError* quota_error = response.add_allocate_errors(); + QuotaRequestInfo info; + quota_error->set_code(code); + quota_error->set_description(error_detail); + return Proto::ConvertAllocateQuotaResponse(response, service_name); +} + +Status ConvertAllocateQuotaErrorToStatus(gasv1::QuotaError::Code code) { + gasv1::AllocateQuotaResponse response; + std::string service_name; + response.add_allocate_errors()->set_code(code); + return Proto::ConvertAllocateQuotaResponse(response, service_name); +} + + +} // namespace + +TEST(AllocateQuotaResponseTest, AbortedWithInvalidArgumentWhenRespIsKeyInvalid) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_INVALID); + EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); +} + +TEST(AllocateQuotaResponseTest, AbortedWithInvalidArgumentWhenRespIsKeyExpired) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_EXPIRED); + EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithInvalidArgumentWhenRespIsBlockedWithResourceExausted) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::RESOURCE_EXHAUSTED); + EXPECT_EQ(Code::RESOURCE_EXHAUSTED, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithInvalidArgumentWhenRespIsBlockedWithProjectSuspended) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_SUSPENDED); + EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithServiceNotEnabled) { + Status result = ConvertAllocateQuotaErrorToStatus( + QuotaError::SERVICE_NOT_ENABLED, "API api_xxxx is not enabled for the project.", "api_xxxx"); + EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); + EXPECT_EQ(result.message(), "API api_xxxx is not enabled for the project."); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithBillingNotActivated) { + Status result = ConvertAllocateQuotaErrorToStatus( + QuotaError::BILLING_NOT_ACTIVE, "API api_xxxx has billing disabled. Please enable it..", "api_xxxx"); + EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); + EXPECT_EQ(result.message(), "API api_xxxx has billing disabled. Please enable it."); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithIpAddressBlocked) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::IP_ADDRESS_BLOCKED); + EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithRefererBlocked) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::REFERER_BLOCKED); + EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithClientAppBlocked) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::CLIENT_APP_BLOCKED); + EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenResponseIsBlockedWithProjectInvalid) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_INVALID); + EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithProjectDeleted) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_DELETED); + EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithApiKeyInvalid) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_INVALID); + EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AbortedWithPermissionDeniedWhenRespIsBlockedWithApiKeyExpiread) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_EXPIRED); + EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AcceptOKWhenRespIsBlockedWithProjectStatusUnavailable) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_STATUS_UNVAILABLE); + EXPECT_EQ(Code::OK, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AcceptOKWhenRespIsBlockedWithServiceStatusUnavailable) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::SERVICE_STATUS_UNAVAILABLE); + EXPECT_EQ(Code::OK, result.code()); +} + +TEST(AllocateQuotaResponseTest, + AcceptOKWhenRespIsBlockedWithBillingStatusUnavailable) { + Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::BILLING_STATUS_UNAVAILABLE); + EXPECT_EQ(Code::OK, result.code()); +} + + +TEST(AllocateQuotaResponseTest, FailOpenWhenResponseIsUnknownBillingStatus) { + EXPECT_TRUE( + ConvertAllocateQuotaErrorToStatus(QuotaError::BILLING_STATUS_UNAVAILABLE).ok()); +} + +TEST(AllocateQuotaResponseTest, FailOpenWhenResponseIsUnknownServiceStatus) { + EXPECT_TRUE( + ConvertAllocateQuotaErrorToStatus(QuotaError::SERVICE_STATUS_UNAVAILABLE).ok()); +} + +} // namespace service_control +} // namespace api_manager +} // namespace google diff --git a/contrib/endpoints/src/api_manager/service_control/proto_test.cc b/contrib/endpoints/src/api_manager/service_control/proto_test.cc index 609cb54abe7..b21efaba1bb 100644 --- a/contrib/endpoints/src/api_manager/service_control/proto_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/proto_test.cc @@ -76,6 +76,12 @@ void FillCheckRequestInfo(CheckRequestInfo* request) { request->referer = "referer"; } +void FillAllocateQuotaRequestInfo(QuotaRequestInfo* request) { + request->client_ip = "1.2.3.4"; + request->referer = "referer"; + request->method_name = "operation_name"; +} + void FillReportRequestInfo(ReportRequestInfo* request) { request->referer = "referer"; request->response_code = 200; @@ -122,6 +128,12 @@ std::string CheckRequestToString(gasv1::CheckRequest* request) { return text; } +std::string AllocateQuotaRequestToString(gasv1::AllocateQuotaRequest* request) { + std::string text; + google::protobuf::TextFormat::PrintToString(*request, &text); + return text; +} + std::string ReportRequestToString(gasv1::ReportRequest* request) { gasv1::Operation* op = request->mutable_operations(0); SetFixTimeStamps(op); @@ -161,6 +173,70 @@ TEST_F(ProtoTest, FillGoodCheckRequestTest) { ASSERT_EQ(expected_text, text); } +TEST_F(ProtoTest, FillGoodAllocateQuotaRequestTest) { + std::vector> metric_cost_vector; + metric_cost_vector.push_back( + std::make_pair("metric_first", 1)); + metric_cost_vector.push_back( + std::make_pair("metric_second", 2)); + + google::api_manager::service_control::QuotaRequestInfo info; + + info.metric_cost_vector = &metric_cost_vector; + + FillOperationInfo(&info); + FillAllocateQuotaRequestInfo(&info); + + gasv1::AllocateQuotaRequest request; + ASSERT_TRUE(scp_.FillAllocateQuotaRequest(info, &request).ok()); + + std::string text = AllocateQuotaRequestToString(&request); + std::string expected_text = ReadTestBaseline("allocate_quota_request.golden"); + ASSERT_EQ(expected_text, text); +} + +TEST_F(ProtoTest, FillAllocateQuotaRequestNoMethodNameTest) { + std::vector> metric_cost_vector; + metric_cost_vector.push_back( + std::make_pair("metric_first", 1)); + metric_cost_vector.push_back( + std::make_pair("metric_second", 2)); + + google::api_manager::service_control::QuotaRequestInfo info; + + FillOperationInfo(&info); + info.metric_cost_vector = &metric_cost_vector; + info.client_ip = "1.2.3.4"; + info.referer = "referer"; + info.method_name = ""; + + gasv1::AllocateQuotaRequest request; + ASSERT_TRUE(scp_.FillAllocateQuotaRequest(info, &request).ok()); + + std::string text = AllocateQuotaRequestToString(&request); + std::string expected_text = + ReadTestBaseline("allocate_quota_request_no_method_name.golden"); + ASSERT_EQ(expected_text, text); +} + +TEST_F(ProtoTest, FillNoMetricRulesAllocateQuotaRequestTest) { + google::api_manager::service_control::QuotaRequestInfo info; + + info.metric_cost_vector = nullptr; + + FillOperationInfo(&info); + FillAllocateQuotaRequestInfo(&info); + + gasv1::AllocateQuotaRequest request; + ASSERT_TRUE(scp_.FillAllocateQuotaRequest(info, &request).ok()); + + std::string text = AllocateQuotaRequestToString(&request); + std::string expected_text = + ReadTestBaseline("allocate_quota_request_no_metrics_rule.golden"); + + ASSERT_EQ(expected_text, text); +} + TEST_F(ProtoTest, FillNoApiKeyCheckRequestTest) { CheckRequestInfo info; info.operation_id = "operation_id"; diff --git a/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request.golden b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request.golden new file mode 100644 index 00000000000..73a26239238 --- /dev/null +++ b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request.golden @@ -0,0 +1,36 @@ +service_name: "test_service" +allocate_operation { + operation_id: "operation_id" + method_name: "operation_name" + consumer_id: "api_key:api_key_x" + labels { + key: "servicecontrol.googleapis.com/caller_ip" + value: "1.2.3.4" + } + labels { + key: "servicecontrol.googleapis.com/referer" + value: "referer" + } + labels { + key: "servicecontrol.googleapis.com/service_agent" + value: "ESP/{{service_agent_version}}" + } + labels { + key: "servicecontrol.googleapis.com/user_agent" + value: "ESP" + } + quota_metrics { + metric_name: "metric_first" + metric_values { + int64_value: 1 + } + } + quota_metrics { + metric_name: "metric_second" + metric_values { + int64_value: 2 + } + } + quota_mode: NORMAL +} +service_config_id: "2016-09-19r0" diff --git a/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_method_name.golden b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_method_name.golden new file mode 100644 index 00000000000..34a59f47652 --- /dev/null +++ b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_method_name.golden @@ -0,0 +1,35 @@ +service_name: "test_service" +allocate_operation { + operation_id: "operation_id" + consumer_id: "api_key:api_key_x" + labels { + key: "servicecontrol.googleapis.com/caller_ip" + value: "1.2.3.4" + } + labels { + key: "servicecontrol.googleapis.com/referer" + value: "referer" + } + labels { + key: "servicecontrol.googleapis.com/service_agent" + value: "ESP/{{service_agent_version}}" + } + labels { + key: "servicecontrol.googleapis.com/user_agent" + value: "ESP" + } + quota_metrics { + metric_name: "metric_first" + metric_values { + int64_value: 1 + } + } + quota_metrics { + metric_name: "metric_second" + metric_values { + int64_value: 2 + } + } + quota_mode: NORMAL +} +service_config_id: "2016-09-19r0" diff --git a/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden new file mode 100644 index 00000000000..2e718fccb8e --- /dev/null +++ b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden @@ -0,0 +1,24 @@ +service_name: "test_service" +allocate_operation { + operation_id: "operation_id" + method_name: "operation_name" + consumer_id: "api_key:api_key_x" + labels { + key: "servicecontrol.googleapis.com/caller_ip" + value: "1.2.3.4" + } + labels { + key: "servicecontrol.googleapis.com/referer" + value: "referer" + } + labels { + key: "servicecontrol.googleapis.com/service_agent" + value: "ESP/{{service_agent_version}}" + } + labels { + key: "servicecontrol.googleapis.com/user_agent" + value: "ESP" + } + quota_mode: NORMAL +} +service_config_id: "2016-09-19r0" diff --git a/contrib/endpoints/src/api_manager/service_control/url_test.cc b/contrib/endpoints/src/api_manager/service_control/url_test.cc index afbf48bd154..8e0bb2d188a 100644 --- a/contrib/endpoints/src/api_manager/service_control/url_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/url_test.cc @@ -57,6 +57,10 @@ TEST(UrlTest, PrependHttps) { ASSERT_EQ( "https://servicecontrol.googleapis.com/v1/services/https-config:report", url.report_url()); + ASSERT_EQ( + "https://servicecontrol.googleapis.com/v1/services/" + "https-config:allocateQuota", + url.quota_url()); } TEST(UrlTest, ServerControlOverride) { From 9dc18d71ed539eb912b909cac323dda6289a6af1 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 14:21:11 -0800 Subject: [PATCH 3/7] Added unit test cases for rate limiting --- .../allocate_quota_response_test.cc | 69 ++++++++++++------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc b/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc index 34b4bcc917e..729a7302527 100644 --- a/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/allocate_quota_response_test.cc @@ -31,8 +31,8 @@ namespace service_control { namespace { Status ConvertAllocateQuotaErrorToStatus(gasv1::QuotaError::Code code, - const char* error_detail, - const char* service_name) { + const char* error_detail, + const char* service_name) { gasv1::AllocateQuotaResponse response; gasv1::QuotaError* quota_error = response.add_allocate_errors(); QuotaRequestInfo info; @@ -48,35 +48,41 @@ Status ConvertAllocateQuotaErrorToStatus(gasv1::QuotaError::Code code) { return Proto::ConvertAllocateQuotaResponse(response, service_name); } - } // namespace -TEST(AllocateQuotaResponseTest, AbortedWithInvalidArgumentWhenRespIsKeyInvalid) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_INVALID); +TEST(AllocateQuotaResponseTest, + AbortedWithInvalidArgumentWhenRespIsKeyInvalid) { + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_INVALID); EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); } -TEST(AllocateQuotaResponseTest, AbortedWithInvalidArgumentWhenRespIsKeyExpired) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_EXPIRED); +TEST(AllocateQuotaResponseTest, + AbortedWithInvalidArgumentWhenRespIsKeyExpired) { + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_EXPIRED); EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithInvalidArgumentWhenRespIsBlockedWithResourceExausted) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::RESOURCE_EXHAUSTED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::RESOURCE_EXHAUSTED); EXPECT_EQ(Code::RESOURCE_EXHAUSTED, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithInvalidArgumentWhenRespIsBlockedWithProjectSuspended) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_SUSPENDED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_SUSPENDED); EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithServiceNotEnabled) { Status result = ConvertAllocateQuotaErrorToStatus( - QuotaError::SERVICE_NOT_ENABLED, "API api_xxxx is not enabled for the project.", "api_xxxx"); + QuotaError::SERVICE_NOT_ENABLED, + "API api_xxxx is not enabled for the project.", "api_xxxx"); EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); EXPECT_EQ(result.message(), "API api_xxxx is not enabled for the project."); } @@ -84,80 +90,93 @@ TEST(AllocateQuotaResponseTest, TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithBillingNotActivated) { Status result = ConvertAllocateQuotaErrorToStatus( - QuotaError::BILLING_NOT_ACTIVE, "API api_xxxx has billing disabled. Please enable it..", "api_xxxx"); + QuotaError::BILLING_NOT_ACTIVE, + "API api_xxxx has billing disabled. Please enable it..", "api_xxxx"); EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); - EXPECT_EQ(result.message(), "API api_xxxx has billing disabled. Please enable it."); + EXPECT_EQ(result.message(), + "API api_xxxx has billing disabled. Please enable it."); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithIpAddressBlocked) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::IP_ADDRESS_BLOCKED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::IP_ADDRESS_BLOCKED); EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithRefererBlocked) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::REFERER_BLOCKED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::REFERER_BLOCKED); EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithClientAppBlocked) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::CLIENT_APP_BLOCKED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::CLIENT_APP_BLOCKED); EXPECT_EQ(Code::PERMISSION_DENIED, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenResponseIsBlockedWithProjectInvalid) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_INVALID); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_INVALID); EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithProjectDeleted) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_DELETED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_DELETED); EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithApiKeyInvalid) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_INVALID); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_INVALID); EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); } TEST(AllocateQuotaResponseTest, AbortedWithPermissionDeniedWhenRespIsBlockedWithApiKeyExpiread) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_EXPIRED); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::API_KEY_EXPIRED); EXPECT_EQ(Code::INVALID_ARGUMENT, result.code()); } TEST(AllocateQuotaResponseTest, AcceptOKWhenRespIsBlockedWithProjectStatusUnavailable) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_STATUS_UNVAILABLE); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::PROJECT_STATUS_UNVAILABLE); EXPECT_EQ(Code::OK, result.code()); } TEST(AllocateQuotaResponseTest, AcceptOKWhenRespIsBlockedWithServiceStatusUnavailable) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::SERVICE_STATUS_UNAVAILABLE); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::SERVICE_STATUS_UNAVAILABLE); EXPECT_EQ(Code::OK, result.code()); } TEST(AllocateQuotaResponseTest, AcceptOKWhenRespIsBlockedWithBillingStatusUnavailable) { - Status result = ConvertAllocateQuotaErrorToStatus(QuotaError::BILLING_STATUS_UNAVAILABLE); + Status result = + ConvertAllocateQuotaErrorToStatus(QuotaError::BILLING_STATUS_UNAVAILABLE); EXPECT_EQ(Code::OK, result.code()); } - TEST(AllocateQuotaResponseTest, FailOpenWhenResponseIsUnknownBillingStatus) { EXPECT_TRUE( - ConvertAllocateQuotaErrorToStatus(QuotaError::BILLING_STATUS_UNAVAILABLE).ok()); + ConvertAllocateQuotaErrorToStatus(QuotaError::BILLING_STATUS_UNAVAILABLE) + .ok()); } TEST(AllocateQuotaResponseTest, FailOpenWhenResponseIsUnknownServiceStatus) { EXPECT_TRUE( - ConvertAllocateQuotaErrorToStatus(QuotaError::SERVICE_STATUS_UNAVAILABLE).ok()); + ConvertAllocateQuotaErrorToStatus(QuotaError::SERVICE_STATUS_UNAVAILABLE) + .ok()); } } // namespace service_control From 267d0bb10a4946f1e7c1e3c24abef808289d59b1 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 16:44:39 -0800 Subject: [PATCH 4/7] Added unit test cases for rate limiting --- .../service_control/aggregated_test.cc | 104 +++++++----------- .../api_manager/service_control/proto_test.cc | 18 --- 2 files changed, 41 insertions(+), 81 deletions(-) diff --git a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc index 5a4109f22aa..bee5a315e7b 100644 --- a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc @@ -231,7 +231,7 @@ TEST_F(AggregatedTestWithRealClient, CheckOKTest) { EXPECT_EQ(stat.send_report_operations, 0); } -class QuotaAllocationFailedTestWithRealClient : public ::testing::Test { +class QuotaAllocationTestWithRealClient : public ::testing::Test { public: void SetUp() { service_.set_name("test_service"); @@ -242,84 +242,46 @@ class QuotaAllocationFailedTestWithRealClient : public ::testing::Test { ASSERT_TRUE((bool)(sc_lib_)); // This is the call actually creating the client. sc_lib_->Init(); + + metric_cost_vector_ = {{"metric_first", 1}, {"metric_second", 2}}; } void DoRunHTTPRequest(HTTPRequest* request) { std::map headers; - AllocateQuotaResponse allocate_quota_response_; - ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( - kAllocateQuotaResponseErrorExhausted, &allocate_quota_response_)); + AllocateQuotaRequest quota_request; + AllocateQuotaResponse quota_response; - std::string body = allocate_quota_response_.SerializeAsString(); + ASSERT_TRUE(quota_request.ParseFromString(request->body())); + ASSERT_EQ(quota_request.allocate_operation().quota_metrics_size(), 2); - request->OnComplete(Status::OK, std::move(headers), std::move(body)); - } + std::set> expected_costs = { + {"metric_first", 1}, {"metric_second", 2}}; + std::set> actual_costs; - ::google::api::Service service_; - std::unique_ptr env_; - std::unique_ptr sc_lib_; -}; - -TEST_F(QuotaAllocationFailedTestWithRealClient, AllocateQuotaTest) { - EXPECT_CALL(*env_, DoRunHTTPRequest(_)) - .WillOnce(Invoke( - this, &QuotaAllocationFailedTestWithRealClient::DoRunHTTPRequest)); - - std::vector> metric_cost_vector; - metric_cost_vector.push_back( - std::make_pair("metric_first", 1)); - metric_cost_vector.push_back( - std::make_pair("metric_second", 2)); + for (auto rule : quota_request.allocate_operation().quota_metrics()) { + actual_costs.insert(std::make_pair(rule.metric_name(), + rule.metric_values(0).int64_value())); + } - QuotaRequestInfo info; + ASSERT_EQ(actual_costs, expected_costs); - info.metric_cost_vector = &metric_cost_vector; + ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( + kAllocateQuotaResponse, "a_response)); - FillOperationInfo(&info); - sc_lib_->Quota(info, nullptr, [](Status status) { - ASSERT_TRUE(status.code() == Code::RESOURCE_EXHAUSTED); - }); -} + std::string body = quota_response.SerializeAsString(); -class QuotaAllocationTestWithRealClient : public ::testing::Test { - public: - void SetUp() { - service_.set_name("test_service"); - service_.mutable_control()->set_environment( - "servicecontrol.googleapis.com"); - env_.reset(new ::testing::NiceMock); - sc_lib_.reset(Aggregated::Create(service_, nullptr, env_.get(), nullptr)); - ASSERT_TRUE((bool)(sc_lib_)); - // This is the call actually creating the client. - sc_lib_->Init(); + request->OnComplete(Status::OK, std::move(headers), std::move(body)); } - void DoRunHTTPRequest(HTTPRequest* request) { + void DoRunHTTPRequestAllocationFailed(HTTPRequest* request) { std::map headers; - AllocateQuotaRequest allocate_quota_request_; - AllocateQuotaResponse allocate_quota_response_; - - ASSERT_TRUE(allocate_quota_request_.ParseFromString(request->body())); - ASSERT_EQ(allocate_quota_request_.allocate_operation().quota_metrics_size(), - 2); - - std::unordered_map metric_rules; - for (auto rule : - allocate_quota_request_.allocate_operation().quota_metrics()) { - metric_rules[rule.metric_name()] = rule.metric_values(0).int64_value(); - } - ASSERT_EQ(metric_rules.size(), 2); - - ASSERT_NE(metric_rules.find("metric_first"), metric_rules.end()); - ASSERT_NE(metric_rules.find("metric_second"), metric_rules.end()); - ASSERT_EQ(metric_rules["metric_first"], 1); - ASSERT_EQ(metric_rules["metric_second"], 2); + AllocateQuotaResponse quota_response; ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( - kAllocateQuotaResponse, &allocate_quota_response_)); + kAllocateQuotaResponseErrorExhausted, "a_response)); - std::string body = allocate_quota_response_.SerializeAsString(); + std::string body = quota_response.SerializeAsString(); request->OnComplete(Status::OK, std::move(headers), std::move(body)); } @@ -327,6 +289,7 @@ class QuotaAllocationTestWithRealClient : public ::testing::Test { ::google::api::Service service_; std::unique_ptr env_; std::unique_ptr sc_lib_; + std::vector> metric_cost_vector_; }; TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { @@ -334,6 +297,20 @@ TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { .WillOnce( Invoke(this, &QuotaAllocationTestWithRealClient::DoRunHTTPRequest)); + QuotaRequestInfo info; + + info.metric_cost_vector = &metric_cost_vector_; + + FillOperationInfo(&info); + sc_lib_->Quota(info, nullptr, + [](Status status) { ASSERT_TRUE(status.ok()); }); +} + +TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaFailedTest) { + EXPECT_CALL(*env_, DoRunHTTPRequest(_)) + .WillOnce(Invoke(this, &QuotaAllocationTestWithRealClient:: + DoRunHTTPRequestAllocationFailed)); + std::vector> metric_cost_vector; metric_cost_vector.push_back( std::make_pair("metric_first", 1)); @@ -345,8 +322,9 @@ TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { info.metric_cost_vector = &metric_cost_vector; FillOperationInfo(&info); - sc_lib_->Quota(info, nullptr, - [](Status status) { ASSERT_TRUE(status.ok()); }); + sc_lib_->Quota(info, nullptr, [](Status status) { + ASSERT_TRUE(status.code() == Code::RESOURCE_EXHAUSTED); + }); } TEST(AggregatedServiceControlTest, Create) { diff --git a/contrib/endpoints/src/api_manager/service_control/proto_test.cc b/contrib/endpoints/src/api_manager/service_control/proto_test.cc index b21efaba1bb..1e966b2a02e 100644 --- a/contrib/endpoints/src/api_manager/service_control/proto_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/proto_test.cc @@ -219,24 +219,6 @@ TEST_F(ProtoTest, FillAllocateQuotaRequestNoMethodNameTest) { ASSERT_EQ(expected_text, text); } -TEST_F(ProtoTest, FillNoMetricRulesAllocateQuotaRequestTest) { - google::api_manager::service_control::QuotaRequestInfo info; - - info.metric_cost_vector = nullptr; - - FillOperationInfo(&info); - FillAllocateQuotaRequestInfo(&info); - - gasv1::AllocateQuotaRequest request; - ASSERT_TRUE(scp_.FillAllocateQuotaRequest(info, &request).ok()); - - std::string text = AllocateQuotaRequestToString(&request); - std::string expected_text = - ReadTestBaseline("allocate_quota_request_no_metrics_rule.golden"); - - ASSERT_EQ(expected_text, text); -} - TEST_F(ProtoTest, FillNoApiKeyCheckRequestTest) { CheckRequestInfo info; info.operation_id = "operation_id"; From 021c9425487482772d3cbc67805053f7f220bb14 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 17:20:15 -0800 Subject: [PATCH 5/7] Added unit test cases for rate limiting --- .../src/api_manager/service_control/aggregated_test.cc | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc index bee5a315e7b..2f029c10185 100644 --- a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc @@ -298,7 +298,6 @@ TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { Invoke(this, &QuotaAllocationTestWithRealClient::DoRunHTTPRequest)); QuotaRequestInfo info; - info.metric_cost_vector = &metric_cost_vector_; FillOperationInfo(&info); @@ -311,15 +310,8 @@ TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaFailedTest) { .WillOnce(Invoke(this, &QuotaAllocationTestWithRealClient:: DoRunHTTPRequestAllocationFailed)); - std::vector> metric_cost_vector; - metric_cost_vector.push_back( - std::make_pair("metric_first", 1)); - metric_cost_vector.push_back( - std::make_pair("metric_second", 2)); - QuotaRequestInfo info; - - info.metric_cost_vector = &metric_cost_vector; + info.metric_cost_vector = &metric_cost_vector_; FillOperationInfo(&info); sc_lib_->Quota(info, nullptr, [](Status status) { From f84d7d86fa1be21e32f3915d72a5a5a23e27d81e Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 17:21:05 -0800 Subject: [PATCH 6/7] Added unit test cases for rate limiting --- ...ocate_quota_request_no_metrics_rule.golden | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden diff --git a/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden b/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden deleted file mode 100644 index 2e718fccb8e..00000000000 --- a/contrib/endpoints/src/api_manager/service_control/testdata/allocate_quota_request_no_metrics_rule.golden +++ /dev/null @@ -1,24 +0,0 @@ -service_name: "test_service" -allocate_operation { - operation_id: "operation_id" - method_name: "operation_name" - consumer_id: "api_key:api_key_x" - labels { - key: "servicecontrol.googleapis.com/caller_ip" - value: "1.2.3.4" - } - labels { - key: "servicecontrol.googleapis.com/referer" - value: "referer" - } - labels { - key: "servicecontrol.googleapis.com/service_agent" - value: "ESP/{{service_agent_version}}" - } - labels { - key: "servicecontrol.googleapis.com/user_agent" - value: "ESP" - } - quota_mode: NORMAL -} -service_config_id: "2016-09-19r0" From 4c9dbd340265a76ab265749cdb16ea3a58d73a10 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 23 Feb 2017 17:53:06 -0800 Subject: [PATCH 7/7] Added unit test cases for rate limiting --- .../service_control/aggregated_test.cc | 25 ++++++++----------- .../api_manager/service_control/proto_test.cc | 16 +++--------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc index 2f029c10185..cc88308b8ba 100644 --- a/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/aggregated_test.cc @@ -246,11 +246,16 @@ class QuotaAllocationTestWithRealClient : public ::testing::Test { metric_cost_vector_ = {{"metric_first", 1}, {"metric_second", 2}}; } + std::string getResponseBody(const char* response) { + AllocateQuotaResponse quota_response; + ::google::protobuf::TextFormat::ParseFromString(response, "a_response); + return quota_response.SerializeAsString(); + } + void DoRunHTTPRequest(HTTPRequest* request) { std::map headers; AllocateQuotaRequest quota_request; - AllocateQuotaResponse quota_response; ASSERT_TRUE(quota_request.ParseFromString(request->body())); ASSERT_EQ(quota_request.allocate_operation().quota_metrics_size(), 2); @@ -266,24 +271,16 @@ class QuotaAllocationTestWithRealClient : public ::testing::Test { ASSERT_EQ(actual_costs, expected_costs); - ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( - kAllocateQuotaResponse, "a_response)); - - std::string body = quota_response.SerializeAsString(); - - request->OnComplete(Status::OK, std::move(headers), std::move(body)); + request->OnComplete(Status::OK, std::move(headers), + std::move(getResponseBody(kAllocateQuotaResponse))); } void DoRunHTTPRequestAllocationFailed(HTTPRequest* request) { std::map headers; - AllocateQuotaResponse quota_response; - - ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( - kAllocateQuotaResponseErrorExhausted, "a_response)); - std::string body = quota_response.SerializeAsString(); - - request->OnComplete(Status::OK, std::move(headers), std::move(body)); + request->OnComplete( + Status::OK, std::move(headers), + std::move(getResponseBody(kAllocateQuotaResponseErrorExhausted))); } ::google::api::Service service_; diff --git a/contrib/endpoints/src/api_manager/service_control/proto_test.cc b/contrib/endpoints/src/api_manager/service_control/proto_test.cc index 1e966b2a02e..89799a10bf4 100644 --- a/contrib/endpoints/src/api_manager/service_control/proto_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/proto_test.cc @@ -174,14 +174,10 @@ TEST_F(ProtoTest, FillGoodCheckRequestTest) { } TEST_F(ProtoTest, FillGoodAllocateQuotaRequestTest) { - std::vector> metric_cost_vector; - metric_cost_vector.push_back( - std::make_pair("metric_first", 1)); - metric_cost_vector.push_back( - std::make_pair("metric_second", 2)); + std::vector> metric_cost_vector = { + {"metric_first", 1}, {"metric_second", 2}}; google::api_manager::service_control::QuotaRequestInfo info; - info.metric_cost_vector = &metric_cost_vector; FillOperationInfo(&info); @@ -196,14 +192,10 @@ TEST_F(ProtoTest, FillGoodAllocateQuotaRequestTest) { } TEST_F(ProtoTest, FillAllocateQuotaRequestNoMethodNameTest) { - std::vector> metric_cost_vector; - metric_cost_vector.push_back( - std::make_pair("metric_first", 1)); - metric_cost_vector.push_back( - std::make_pair("metric_second", 2)); + std::vector> metric_cost_vector = { + {"metric_first", 1}, {"metric_second", 2}}; google::api_manager::service_control::QuotaRequestInfo info; - FillOperationInfo(&info); info.metric_cost_vector = &metric_cost_vector; info.client_ip = "1.2.3.4";