1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "src/core/lib/transport/status_metadata.h"
20 
21 #include <gtest/gtest.h>
22 
23 #include "src/core/lib/transport/static_metadata.h"
24 
25 namespace {
26 
TEST(GetStatusCodeFromMetadata,OK)27 TEST(GetStatusCodeFromMetadata, OK) {
28   EXPECT_EQ(GRPC_STATUS_OK,
29             grpc_get_status_code_from_metadata(GRPC_MDELEM_GRPC_STATUS_0));
30 }
31 
TEST(GetStatusCodeFromMetadata,CANCELLED)32 TEST(GetStatusCodeFromMetadata, CANCELLED) {
33   EXPECT_EQ(GRPC_STATUS_CANCELLED,
34             grpc_get_status_code_from_metadata(GRPC_MDELEM_GRPC_STATUS_1));
35 }
36 
TEST(GetStatusCodeFromMetadata,UNKNOWN)37 TEST(GetStatusCodeFromMetadata, UNKNOWN) {
38   EXPECT_EQ(GRPC_STATUS_UNKNOWN,
39             grpc_get_status_code_from_metadata(GRPC_MDELEM_GRPC_STATUS_2));
40 }
41 
TEST(GetStatusCodeFromMetadata,Other)42 TEST(GetStatusCodeFromMetadata, Other) {
43   grpc_mdelem status_md = grpc_mdelem_from_slices(
44       GRPC_MDSTR_GRPC_STATUS, grpc_slice_from_static_string("10"));
45   EXPECT_EQ(GRPC_STATUS_ABORTED, grpc_get_status_code_from_metadata(status_md));
46   GRPC_MDELEM_UNREF(status_md);
47 }
48 
TEST(GetStatusCodeFromMetadata,Unparseable)49 TEST(GetStatusCodeFromMetadata, Unparseable) {
50   grpc_mdelem status_md = grpc_mdelem_from_slices(
51       GRPC_MDSTR_GRPC_STATUS, grpc_slice_from_static_string("NaN"));
52   EXPECT_EQ(GRPC_STATUS_UNKNOWN, grpc_get_status_code_from_metadata(status_md));
53   GRPC_MDELEM_UNREF(status_md);
54 }
55 
56 }  // namespace
57 
main(int argc,char ** argv)58 int main(int argc, char** argv) {
59   ::testing::InitGoogleTest(&argc, argv);
60   return RUN_ALL_TESTS();
61 }
62