Repository: cordova-ios Updated Branches: refs/heads/master b1ebdab27 -> 78ca06e1a
CB-2520 - iOS - "original" user agent needs to be overridable (closes #112) Signed-off-by: Shazron Abdullah <shaz...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/78ca06e1 Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/78ca06e1 Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/78ca06e1 Branch: refs/heads/master Commit: 78ca06e1ae14ad895d73ac5d141843cc94ecae33 Parents: b1ebdab Author: Kevin Hawkins <khawk...@salesforce.com> Authored: Sat Oct 4 19:45:54 2014 -0700 Committer: Shazron Abdullah <shaz...@apache.org> Committed: Mon Oct 13 15:07:10 2014 -0700 ---------------------------------------------------------------------- CordovaLib/Classes/CDVViewController.h | 11 ++++++ CordovaLib/Classes/CDVViewController.m | 11 ++++-- tests/CordovaLibTests/CDVUserAgentTest.m | 51 ++++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/78ca06e1/CordovaLib/Classes/CDVViewController.h ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVViewController.h b/CordovaLib/Classes/CDVViewController.h index 1015c50..1d34ddd 100644 --- a/CordovaLib/Classes/CDVViewController.h +++ b/CordovaLib/Classes/CDVViewController.h @@ -48,8 +48,19 @@ @property (nonatomic, readwrite, copy) NSString* startPage; @property (nonatomic, readonly, strong) CDVCommandQueue* commandQueue; @property (nonatomic, readonly, strong) id <CDVCommandDelegate> commandDelegate; + +/** + The complete user agent that Cordova will use when sending web requests. + */ @property (nonatomic, readonly) NSString* userAgent; +/** + The base user agent data that Cordova will use to build its user agent. If this + property isn't set, Cordova will use the standard web view user agent as its + base. + */ +@property (nonatomic, readwrite, copy) NSString* baseUserAgent; + + (NSDictionary*)getBundlePlist:(NSString*)plistName; + (NSString*)applicationDocumentsDirectory; http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/78ca06e1/CordovaLib/Classes/CDVViewController.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVViewController.m b/CordovaLib/Classes/CDVViewController.m index 66c1850..1e59b1b 100644 --- a/CordovaLib/Classes/CDVViewController.m +++ b/CordovaLib/Classes/CDVViewController.m @@ -52,7 +52,7 @@ @synthesize webView, supportedOrientations; @synthesize pluginObjects, pluginsMap, whitelist, startupPluginNames; @synthesize configParser, settings, loadFromString; -@synthesize wwwFolderName, startPage, initialized, openURL; +@synthesize wwwFolderName, startPage, initialized, openURL, baseUserAgent; @synthesize commandDelegate = _commandDelegate; @synthesize commandQueue = _commandQueue; @@ -562,9 +562,14 @@ - (NSString*)userAgent { if (_userAgent == nil) { - NSString* originalUserAgent = [CDVUserAgentUtil originalUserAgent]; + NSString *localBaseUserAgent; + if (self.baseUserAgent != nil) { + localBaseUserAgent = self.baseUserAgent; + } else { + localBaseUserAgent = [CDVUserAgentUtil originalUserAgent]; + } // Use our address as a unique number to append to the User-Agent. - _userAgent = [NSString stringWithFormat:@"%@ (%lld)", originalUserAgent, (long long)self]; + _userAgent = [NSString stringWithFormat:@"%@ (%lld)", localBaseUserAgent, (long long)self]; } return _userAgent; } http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/78ca06e1/tests/CordovaLibTests/CDVUserAgentTest.m ---------------------------------------------------------------------- diff --git a/tests/CordovaLibTests/CDVUserAgentTest.m b/tests/CordovaLibTests/CDVUserAgentTest.m index e9066b3..42d238c 100644 --- a/tests/CordovaLibTests/CDVUserAgentTest.m +++ b/tests/CordovaLibTests/CDVUserAgentTest.m @@ -31,14 +31,22 @@ @implementation CDVUserAgentTestViewController @synthesize vc1 = _vc1, vc2 = _vc2; +- (id)init +{ + self = [super init]; + if (self) { + _vc1 = [[CDVViewController alloc] init]; + _vc2 = [[CDVViewController alloc] init]; + } + return self; +} + - (void)loadView { - _vc1 = [[CDVViewController alloc] init]; _vc1.wwwFolderName = @"www"; _vc1.startPage = @"index.html"; [self addChildViewController:_vc1]; - _vc2 = [[CDVViewController alloc] init]; _vc2.wwwFolderName = @"www"; _vc2.startPage = @"index.html"; [self addChildViewController:_vc2]; @@ -74,6 +82,23 @@ [super tearDown]; } +- (void)testDefaultUserAgent +{ + CDVUserAgentTestViewController* rootVc = [[CDVUserAgentTestViewController alloc] init]; + + self.appDelegate.window.rootViewController = rootVc; + + + NSString* getWebUserAgent = @"navigator.userAgent"; + [self waitForConditionName:@"getting user-agents" block:^BOOL { + return (rootVc.vc1.webView.request != nil && rootVc.vc2.webView.request != nil); + }]; + NSString* webUserAgent = [rootVc.vc1.webView stringByEvaluatingJavaScriptFromString:getWebUserAgent]; + NSString* cordovaUserAgent = rootVc.vc1.userAgent; + + XCTAssertTrue([cordovaUserAgent hasPrefix:webUserAgent], @"Default Cordova user agent should be based on navigator.userAgent."); +} + - (void)testMultipleViews { CDVUserAgentTestViewController* rootVc = [[CDVUserAgentTestViewController alloc] init]; @@ -81,8 +106,8 @@ self.appDelegate.window.rootViewController = rootVc; NSString* getUserAgentCode = @"navigator.userAgent"; - [self waitForConditionName:@"getting user-agents" block:^{ - return (BOOL)(rootVc.vc1.webView.request != nil && rootVc.vc2.webView.request != nil); + [self waitForConditionName:@"getting user-agents" block:^BOOL { + return (rootVc.vc1.webView.request != nil && rootVc.vc2.webView.request != nil); }]; NSString* ua1 = [rootVc.vc1.webView stringByEvaluatingJavaScriptFromString:getUserAgentCode]; NSString* ua2 = [rootVc.vc2.webView stringByEvaluatingJavaScriptFromString:getUserAgentCode]; @@ -90,4 +115,22 @@ XCTAssertFalse([ua1 isEqual:ua2], @"User-Agents should be different."); } +- (void)testBaseUserAgent +{ + CDVUserAgentTestViewController* rootVc = [[CDVUserAgentTestViewController alloc] init]; + rootVc.vc1.baseUserAgent = @"A different baseline user agent 1"; + rootVc.vc2.baseUserAgent = @"A different baseline user agent 2"; + + self.appDelegate.window.rootViewController = rootVc; + + [self waitForConditionName:@"getting user-agents" block:^BOOL { + return (rootVc.vc1.webView.request != nil && rootVc.vc2.webView.request != nil); + }]; + NSString* cordovaUserAgent1 = rootVc.vc1.userAgent; + NSString* cordovaUserAgent2 = rootVc.vc2.userAgent; + + XCTAssertTrue([cordovaUserAgent1 hasPrefix:rootVc.vc1.baseUserAgent], @"Cordova user agent should be based on base user agent."); + XCTAssertTrue([cordovaUserAgent2 hasPrefix:rootVc.vc2.baseUserAgent], @"Cordova user agent should be based on base user agent."); +} + @end --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org