From d4b83fd1bcb7dc46b79ce926ad18785a4e2223db Mon Sep 17 00:00:00 2001 From: yulin-liang Date: Thu, 11 Feb 2021 16:16:58 -0800 Subject: [PATCH] Test user agent suffix for grpc-objc. --- src/objective-c/GRPCClient/GRPCCall+ChannelArg.h | 1 + src/objective-c/GRPCClient/GRPCCall+ChannelArg.m | 5 +++++ .../GRPCClient/private/GRPCCore/GRPCHost.h | 1 + .../GRPCClient/private/GRPCCore/GRPCHost.m | 1 + src/objective-c/tests/UnitTests/GRPCClientTests.m | 11 ++++++++--- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h index 3f41cf4b362..de5dd978f2e 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h @@ -26,6 +26,7 @@ @interface GRPCCall (ChannelArg) + (void)setUserAgentPrefix:(nonnull NSString *)userAgentPrefix forHost:(nonnull NSString *)host; ++ (void)setUserAgentSuffix:(nonnull NSString *)userAgentSuffix forHost:(nonnull NSString *)host; + (void)setResponseSizeLimit:(NSUInteger)limit forHost:(nonnull NSString *)host; + (void)closeOpenConnections DEPRECATED_MSG_ATTRIBUTE("The API for this feature is experimental, " "and might be removed or modified at any " diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m index aae1b740c71..ae1355dee10 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m @@ -30,6 +30,11 @@ hostConfig.userAgentPrefix = userAgentPrefix; } ++ (void)setUserAgentSuffix:(nonnull NSString *)userAgentSuffix forHost:(nonnull NSString *)host { + GRPCHost *hostConfig = [GRPCHost hostWithAddress:host]; + hostConfig.userAgentSuffix = userAgentSuffix; +} + + (void)setResponseSizeLimit:(NSUInteger)limit forHost:(nonnull NSString *)host { GRPCHost *hostConfig = [GRPCHost hostWithAddress:host]; hostConfig.responseSizeLimitOverride = limit; diff --git a/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.h b/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.h index ca3c52ea177..6da3139bc25 100644 --- a/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.h +++ b/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.h @@ -36,6 +36,7 @@ struct grpc_channel_credentials; @property(nonatomic, readonly) NSString *address; @property(nonatomic, copy, nullable) NSString *userAgentPrefix; +@property(nonatomic, copy, nullable) NSString *userAgentSuffix; @property(nonatomic) grpc_compression_algorithm compressAlgorithm; @property(nonatomic) int keepaliveInterval; @property(nonatomic) int keepaliveTimeout; diff --git a/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.m index 6a3853fc94e..9b205138435 100644 --- a/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.m @@ -100,6 +100,7 @@ static NSMutableDictionary *gHostCache; - (GRPCCallOptions *)callOptions { GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init]; options.userAgentPrefix = _userAgentPrefix; + options.userAgentSuffix = _userAgentSuffix; options.responseSizeLimit = _responseSizeLimitOverride; options.compressionAlgorithm = (GRPCCompressionAlgorithm)_compressAlgorithm; options.retryEnabled = _retryEnabled; diff --git a/src/objective-c/tests/UnitTests/GRPCClientTests.m b/src/objective-c/tests/UnitTests/GRPCClientTests.m index 25cf0e846f7..a79cfa9d934 100644 --- a/src/objective-c/tests/UnitTests/GRPCClientTests.m +++ b/src/objective-c/tests/UnitTests/GRPCClientTests.m @@ -108,8 +108,9 @@ static GRPCProtoMethod *kFullDuplexCallMethod; } - (void)setUp { - // Add a custom user agent prefix that will be used in test + // Add a custom user agent prefix and suffix that will be used in test [GRPCCall setUserAgentPrefix:@"Foo" forHost:kHostAddress]; + [GRPCCall setUserAgentSuffix:@"Suffix" forHost:kHostAddress]; // Register test server as non-SSL. [GRPCCall useInsecureConnectionsForHost:kHostAddress]; @@ -280,7 +281,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod; [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; } -- (void)testUserAgentPrefix { +- (void)testUserAgentPrefixAndSuffix { __weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; @@ -303,6 +304,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod; // Test the regex is correct NSString *expectedUserAgent = @"Foo grpc-objc-cfstream/"; expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING]; + expectedUserAgent = [expectedUserAgent stringByAppendingString:@" Suffix"]; expectedUserAgent = [expectedUserAgent stringByAppendingString:@" grpc-c/"]; expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING]; expectedUserAgent = [expectedUserAgent stringByAppendingString:@" ("]; @@ -322,8 +324,11 @@ static GRPCProtoMethod *kFullDuplexCallMethod; options:0 range:NSMakeRange(0, [userAgent length]) withTemplate:@""]; - XCTAssertEqualObjects(customUserAgent, @"Foo"); + NSArray *userAgentArray = [customUserAgent componentsSeparatedByString:@" "]; + XCTAssertEqual([userAgentArray count], 2); + XCTAssertEqualObjects([userAgentArray objectAtIndex:0], @"Foo"); + XCTAssertEqualObjects([userAgentArray objectAtIndex:1], @"Suffix"); [response fulfill]; } completionHandler:^(NSError *errorOrNil) {