xref: /aosp_15_r20/external/grpc-grpc/test/cpp/cocoapods/test/server_context_test_spouse_test.mm (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1/*
2 *
3 * Copyright 2016 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// Hack TEST macro of gTest and make they conform XCTest style. We only
20// need test name (b), not test case name (a).
21#define TEST(a, b) -(void)test##b
22#define ASSERT_TRUE XCTAssert
23#define ASSERT_EQ XCTAssertEqual
24
25#import <XCTest/XCTest.h>
26
27#include <grpcpp/test/server_context_test_spouse.h>
28
29#include <cstring>
30#include <vector>
31
32#include <grpcpp/impl/grpc_library.h>
33
34const char key1[] = "metadata-key1";
35const char key2[] = "metadata-key2";
36const char val1[] = "metadata-val1";
37const char val2[] = "metadata-val2";
38
39bool ClientMetadataContains(const grpc::ServerContext& context, const grpc::string_ref& key,
40                            const grpc::string_ref& value) {
41  const auto& client_metadata = context.client_metadata();
42  for (auto iter = client_metadata.begin(); iter != client_metadata.end(); ++iter) {
43    if (iter->first == key && iter->second == value) {
44      return true;
45    }
46  }
47  return false;
48}
49
50@interface ServerContextTestSpouseTest : XCTestCase
51
52@end
53
54@implementation ServerContextTestSpouseTest
55
56TEST(ServerContextTestSpouseTest, ClientMetadataHandle) {
57  grpc::ServerContext context;
58  grpc::testing::ServerContextTestSpouse spouse(&context);
59
60  spouse.AddClientMetadata(key1, val1);
61  ASSERT_TRUE(ClientMetadataContains(context, key1, val1));
62
63  spouse.AddClientMetadata(key2, val2);
64  ASSERT_TRUE(ClientMetadataContains(context, key1, val1));
65  ASSERT_TRUE(ClientMetadataContains(context, key2, val2));
66}
67
68TEST(ServerContextTestSpouseTest, InitialMetadata) {
69  grpc::ServerContext context;
70  grpc::testing::ServerContextTestSpouse spouse(&context);
71  std::multimap<std::string, std::string> metadata;
72
73  context.AddInitialMetadata(key1, val1);
74  metadata.insert(std::pair<std::string, std::string>(key1, val1));
75  ASSERT_EQ(metadata, spouse.GetInitialMetadata());
76
77  context.AddInitialMetadata(key2, val2);
78  metadata.insert(std::pair<std::string, std::string>(key2, val2));
79  ASSERT_EQ(metadata, spouse.GetInitialMetadata());
80}
81
82TEST(ServerContextTestSpouseTest, ServerMetadataHandle) {
83  grpc::ServerContext context;
84  grpc::testing::ServerContextTestSpouse spouse(&context);
85  std::multimap<std::string, std::string> metadata;
86
87  context.AddTrailingMetadata(key1, val1);
88  metadata.insert(std::pair<std::string, std::string>(key1, val1));
89  ASSERT_EQ(metadata, spouse.GetTrailingMetadata());
90
91  context.AddTrailingMetadata(key2, val2);
92  metadata.insert(std::pair<std::string, std::string>(key2, val2));
93  ASSERT_EQ(metadata, spouse.GetTrailingMetadata());
94}
95
96@end
97