Эх сурвалжийг харах

Merge pull request #25428 from yulin-liang/suffix-test

Test user agent suffix for grpc-objc.
yulin liang 4 жил өмнө
parent
commit
74f14f3f6f

+ 1 - 0
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 "

+ 5 - 0
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;

+ 1 - 0
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;

+ 1 - 0
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;

+ 8 - 3
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) {