Test user agent suffix for grpc-objc.

pull/25428/head
yulin-liang 4 years ago
parent 8b2bd69ef3
commit d4b83fd1bc
  1. 1
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.h
  2. 5
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.m
  3. 1
      src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.h
  4. 1
      src/objective-c/GRPCClient/private/GRPCCore/GRPCHost.m
  5. 11
      src/objective-c/tests/UnitTests/GRPCClientTests.m

@ -26,6 +26,7 @@
@interface GRPCCall (ChannelArg) @interface GRPCCall (ChannelArg)
+ (void)setUserAgentPrefix:(nonnull NSString *)userAgentPrefix forHost:(nonnull NSString *)host; + (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)setResponseSizeLimit:(NSUInteger)limit forHost:(nonnull NSString *)host;
+ (void)closeOpenConnections DEPRECATED_MSG_ATTRIBUTE("The API for this feature is experimental, " + (void)closeOpenConnections DEPRECATED_MSG_ATTRIBUTE("The API for this feature is experimental, "
"and might be removed or modified at any " "and might be removed or modified at any "

@ -30,6 +30,11 @@
hostConfig.userAgentPrefix = userAgentPrefix; 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 { + (void)setResponseSizeLimit:(NSUInteger)limit forHost:(nonnull NSString *)host {
GRPCHost *hostConfig = [GRPCHost hostWithAddress:host]; GRPCHost *hostConfig = [GRPCHost hostWithAddress:host];
hostConfig.responseSizeLimitOverride = limit; hostConfig.responseSizeLimitOverride = limit;

@ -36,6 +36,7 @@ struct grpc_channel_credentials;
@property(nonatomic, readonly) NSString *address; @property(nonatomic, readonly) NSString *address;
@property(nonatomic, copy, nullable) NSString *userAgentPrefix; @property(nonatomic, copy, nullable) NSString *userAgentPrefix;
@property(nonatomic, copy, nullable) NSString *userAgentSuffix;
@property(nonatomic) grpc_compression_algorithm compressAlgorithm; @property(nonatomic) grpc_compression_algorithm compressAlgorithm;
@property(nonatomic) int keepaliveInterval; @property(nonatomic) int keepaliveInterval;
@property(nonatomic) int keepaliveTimeout; @property(nonatomic) int keepaliveTimeout;

@ -100,6 +100,7 @@ static NSMutableDictionary *gHostCache;
- (GRPCCallOptions *)callOptions { - (GRPCCallOptions *)callOptions {
GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init]; GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
options.userAgentPrefix = _userAgentPrefix; options.userAgentPrefix = _userAgentPrefix;
options.userAgentSuffix = _userAgentSuffix;
options.responseSizeLimit = _responseSizeLimitOverride; options.responseSizeLimit = _responseSizeLimitOverride;
options.compressionAlgorithm = (GRPCCompressionAlgorithm)_compressAlgorithm; options.compressionAlgorithm = (GRPCCompressionAlgorithm)_compressAlgorithm;
options.retryEnabled = _retryEnabled; options.retryEnabled = _retryEnabled;

@ -108,8 +108,9 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
} }
- (void)setUp { - (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 setUserAgentPrefix:@"Foo" forHost:kHostAddress];
[GRPCCall setUserAgentSuffix:@"Suffix" forHost:kHostAddress];
// Register test server as non-SSL. // Register test server as non-SSL.
[GRPCCall useInsecureConnectionsForHost:kHostAddress]; [GRPCCall useInsecureConnectionsForHost:kHostAddress];
@ -280,7 +281,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
} }
- (void)testUserAgentPrefix { - (void)testUserAgentPrefixAndSuffix {
__weak XCTestExpectation *response = __weak XCTestExpectation *response =
[self expectationWithDescription:@"Empty response received."]; [self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
@ -303,6 +304,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
// Test the regex is correct // Test the regex is correct
NSString *expectedUserAgent = @"Foo grpc-objc-cfstream/"; NSString *expectedUserAgent = @"Foo grpc-objc-cfstream/";
expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING]; expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@" Suffix"];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@" grpc-c/"]; expectedUserAgent = [expectedUserAgent stringByAppendingString:@" grpc-c/"];
expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING]; expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@" ("]; expectedUserAgent = [expectedUserAgent stringByAppendingString:@" ("];
@ -322,8 +324,11 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
options:0 options:0
range:NSMakeRange(0, [userAgent length]) range:NSMakeRange(0, [userAgent length])
withTemplate:@""]; withTemplate:@""];
XCTAssertEqualObjects(customUserAgent, @"Foo");
NSArray *userAgentArray = [customUserAgent componentsSeparatedByString:@" "];
XCTAssertEqual([userAgentArray count], 2);
XCTAssertEqualObjects([userAgentArray objectAtIndex:0], @"Foo");
XCTAssertEqualObjects([userAgentArray objectAtIndex:1], @"Suffix");
[response fulfill]; [response fulfill];
} }
completionHandler:^(NSError *errorOrNil) { completionHandler:^(NSError *errorOrNil) {

Loading…
Cancel
Save