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 34static grpc::internal::GrpcLibraryInitializer g_initializer; 35 36const char key1[] = "metadata-key1"; 37const char key2[] = "metadata-key2"; 38const char val1[] = "metadata-val1"; 39const char val2[] = "metadata-val2"; 40 41bool ClientMetadataContains(const grpc::ServerContext& context, const grpc::string_ref& key, 42 const grpc::string_ref& value) { 43 const auto& client_metadata = context.client_metadata(); 44 for (auto iter = client_metadata.begin(); iter != client_metadata.end(); ++iter) { 45 if (iter->first == key && iter->second == value) { 46 return true; 47 } 48 } 49 return false; 50} 51 52@interface ServerContextTestSpouseTest : XCTestCase 53 54@end 55 56@implementation ServerContextTestSpouseTest 57 58TEST(ServerContextTestSpouseTest, ClientMetadata) { 59 grpc::ServerContext context; 60 grpc::testing::ServerContextTestSpouse spouse(&context); 61 62 spouse.AddClientMetadata(key1, val1); 63 ASSERT_TRUE(ClientMetadataContains(context, key1, val1)); 64 65 spouse.AddClientMetadata(key2, val2); 66 ASSERT_TRUE(ClientMetadataContains(context, key1, val1)); 67 ASSERT_TRUE(ClientMetadataContains(context, key2, val2)); 68} 69 70TEST(ServerContextTestSpouseTest, InitialMetadata) { 71 grpc::ServerContext context; 72 grpc::testing::ServerContextTestSpouse spouse(&context); 73 std::multimap<grpc::string, grpc::string> metadata; 74 75 context.AddInitialMetadata(key1, val1); 76 metadata.insert(std::pair<grpc::string, grpc::string>(key1, val1)); 77 ASSERT_EQ(metadata, spouse.GetInitialMetadata()); 78 79 context.AddInitialMetadata(key2, val2); 80 metadata.insert(std::pair<grpc::string, grpc::string>(key2, val2)); 81 ASSERT_EQ(metadata, spouse.GetInitialMetadata()); 82} 83 84TEST(ServerContextTestSpouseTest, TrailingMetadata) { 85 grpc::ServerContext context; 86 grpc::testing::ServerContextTestSpouse spouse(&context); 87 std::multimap<grpc::string, grpc::string> metadata; 88 89 context.AddTrailingMetadata(key1, val1); 90 metadata.insert(std::pair<grpc::string, grpc::string>(key1, val1)); 91 ASSERT_EQ(metadata, spouse.GetTrailingMetadata()); 92 93 context.AddTrailingMetadata(key2, val2); 94 metadata.insert(std::pair<grpc::string, grpc::string>(key2, val2)); 95 ASSERT_EQ(metadata, spouse.GetTrailingMetadata()); 96} 97 98@end 99