Make user agent string generation work on Mac OS X
Added test for user agent
Showing
9 changed files
with
91 additions
and
19 deletions
| @@ -397,6 +397,12 @@ extern NSString* const NetworkRequestErrorDomain; | @@ -397,6 +397,12 @@ extern NSString* const NetworkRequestErrorDomain; | ||
| 397 | + (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath; | 397 | + (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath; |
| 398 | + (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest; | 398 | + (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest; |
| 399 | 399 | ||
| 400 | +#pragma mark get user agent | ||
| 401 | + | ||
| 402 | +// Will be used as a user agent if requests do not specify a custom user agent | ||
| 403 | +// Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist | ||
| 404 | ++ (NSString *)defaultUserAgentString; | ||
| 405 | + | ||
| 400 | @property (retain) NSString *username; | 406 | @property (retain) NSString *username; |
| 401 | @property (retain) NSString *password; | 407 | @property (retain) NSString *password; |
| 402 | @property (retain) NSString *domain; | 408 | @property (retain) NSString *domain; |
| @@ -389,25 +389,14 @@ static NSError *ASITooMuchRedirectionError; | @@ -389,25 +389,14 @@ static NSError *ASITooMuchRedirectionError; | ||
| 389 | } | 389 | } |
| 390 | } | 390 | } |
| 391 | 391 | ||
| 392 | - // Set a logical user agent (on iPhones, for now) | 392 | + // Build and set the user agent string if the request does not already have a custom user agent specified |
| 393 | -#if TARGET_OS_IPHONE | 393 | + if (![[self requestHeaders] objectForKey:@"User-Agent"]) { |
| 394 | - if ([[self requestHeaders] objectForKey:@"User-Agent"] == nil) { | 394 | + NSString *userAgentString = [ASIHTTPRequest defaultUserAgentString]; |
| 395 | - UIDevice *device = [UIDevice currentDevice]; | 395 | + if (userAgentString) { |
| 396 | - NSBundle *bundle = [NSBundle mainBundle]; | 396 | + [self addRequestHeader:@"User-Agent" value:userAgentString]; |
| 397 | - NSLocale *locale = [NSLocale currentLocale]; | 397 | + } |
| 398 | - | ||
| 399 | - NSString *userAgent = [NSString stringWithFormat:@"%@ %@ (%@; %@ %@; %@)", | ||
| 400 | - [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"], | ||
| 401 | - [bundle objectForInfoDictionaryKey:@"CFBundleVersion"], | ||
| 402 | - [device model], | ||
| 403 | - [device systemName], | ||
| 404 | - [device systemVersion], | ||
| 405 | - [locale localeIdentifier] | ||
| 406 | - ]; | ||
| 407 | - | ||
| 408 | - [self addRequestHeader:@"User-Agent" value:userAgent]; | ||
| 409 | } | 398 | } |
| 410 | -#endif | 399 | + |
| 411 | 400 | ||
| 412 | // Accept a compressed response | 401 | // Accept a compressed response |
| 413 | if ([self allowCompressedResponse]) { | 402 | if ([self allowCompressedResponse]) { |
| @@ -1869,6 +1858,53 @@ static NSError *ASITooMuchRedirectionError; | @@ -1869,6 +1858,53 @@ static NSError *ASITooMuchRedirectionError; | ||
| 1869 | return Z_OK; | 1858 | return Z_OK; |
| 1870 | } | 1859 | } |
| 1871 | 1860 | ||
| 1861 | +#pragma mark get user agent | ||
| 1862 | + | ||
| 1863 | ++ (NSString *)defaultUserAgentString | ||
| 1864 | +{ | ||
| 1865 | + NSBundle *bundle = [NSBundle mainBundle]; | ||
| 1866 | + | ||
| 1867 | + // Attempt to find a name for this application | ||
| 1868 | + NSString *appName = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; | ||
| 1869 | + if (!appName) { | ||
| 1870 | + appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"]; | ||
| 1871 | + } | ||
| 1872 | + // If we couldn't find one, we'll give up (and ASIHTTPRequest will use the standard CFNetwork user agent) | ||
| 1873 | + if (!appName) { | ||
| 1874 | + return nil; | ||
| 1875 | + } | ||
| 1876 | + NSString *appVersion = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"]; | ||
| 1877 | + NSString *deviceName;; | ||
| 1878 | + NSString *OSName; | ||
| 1879 | + NSString *OSVersion; | ||
| 1880 | + | ||
| 1881 | + NSString *locale = [[NSLocale currentLocale] localeIdentifier]; | ||
| 1882 | + | ||
| 1883 | +#if TARGET_OS_IPHONE | ||
| 1884 | + UIDevice *device = [UIDevice currentDevice]; | ||
| 1885 | + deviceName = [device model]; | ||
| 1886 | + OSName = [device systemName]; | ||
| 1887 | + OSVersion = [device systemVersion]; | ||
| 1888 | + | ||
| 1889 | +#else | ||
| 1890 | + deviceName = @"Macintosh"; | ||
| 1891 | + OSName = @"Mac OS X"; | ||
| 1892 | + | ||
| 1893 | + // From http://www.cocoadev.com/index.pl?DeterminingOSVersion | ||
| 1894 | + // We won't bother to check for systems prior to 10.4, since ASIHTTPRequest only works on 10.5+ | ||
| 1895 | + OSErr err; | ||
| 1896 | + SInt32 versionMajor, versionMinor, versionBugFix; | ||
| 1897 | + if ((err = Gestalt(gestaltSystemVersionMajor, &versionMajor)) != noErr) return nil; | ||
| 1898 | + if ((err = Gestalt(gestaltSystemVersionMinor, &versionMinor)) != noErr) return nil; | ||
| 1899 | + if ((err = Gestalt(gestaltSystemVersionBugFix, &versionBugFix)) != noErr) return nil; | ||
| 1900 | + OSVersion = [NSString stringWithFormat:@"%u.%u.%u", versionMajor, versionMinor, versionBugFix]; | ||
| 1901 | + | ||
| 1902 | +#endif | ||
| 1903 | + // Takes the form "My Application 1.0 (Macintosh; Mac OS X 10.5.7; en_GB)" | ||
| 1904 | + return [NSString stringWithFormat:@"%@ %@ (%@; %@ %@; %@)", appName, appVersion, deviceName, OSName, OSVersion, locale]; | ||
| 1905 | +} | ||
| 1906 | + | ||
| 1907 | + | ||
| 1872 | 1908 | ||
| 1873 | @synthesize username; | 1909 | @synthesize username; |
| 1874 | @synthesize password; | 1910 | @synthesize password; |
| @@ -20,6 +20,7 @@ | @@ -20,6 +20,7 @@ | ||
| 20 | - (void)testTimeOut; | 20 | - (void)testTimeOut; |
| 21 | - (void)testRequestMethod; | 21 | - (void)testRequestMethod; |
| 22 | - (void)testHTTPVersion; | 22 | - (void)testHTTPVersion; |
| 23 | +- (void)testUserAgent; | ||
| 23 | - (void)testUploadContentLength; | 24 | - (void)testUploadContentLength; |
| 24 | - (void)testDownloadContentLength; | 25 | - (void)testDownloadContentLength; |
| 25 | - (void)testFileDownload; | 26 | - (void)testFileDownload; |
| @@ -132,6 +132,27 @@ | @@ -132,6 +132,27 @@ | ||
| 132 | GHAssertTrue(success,@"Wrong HTTP version used"); | 132 | GHAssertTrue(success,@"Wrong HTTP version used"); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | +- (void)testUserAgent | ||
| 136 | +{ | ||
| 137 | + // defaultUserAgentString will be nil if we haven't set a Bundle Name or Bundle Display Name | ||
| 138 | + if ([ASIHTTPRequest defaultUserAgentString]) { | ||
| 139 | + | ||
| 140 | + // Returns the user agent it received in the response body | ||
| 141 | + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/user-agent"]]; | ||
| 142 | + [request start]; | ||
| 143 | + BOOL success = [[request responseString] isEqualToString:[ASIHTTPRequest defaultUserAgentString]]; | ||
| 144 | + GHAssertTrue(success,@"Failed to set the correct user agent"); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + // Now test specifying a custom user agent | ||
| 148 | + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/user-agent"]]; | ||
| 149 | + [request addRequestHeader:@"User-Agent" value:@"Ferdinand Fuzzworth's Magic Tent of Mystery"]; | ||
| 150 | + [request start]; | ||
| 151 | + BOOL success = [[request responseString] isEqualToString:@"Ferdinand Fuzzworth's Magic Tent of Mystery"]; | ||
| 152 | + GHAssertTrue(success,@"Failed to set the correct user agent"); | ||
| 153 | + | ||
| 154 | +} | ||
| 155 | + | ||
| 135 | - (void)testAutomaticRedirection | 156 | - (void)testAutomaticRedirection |
| 136 | { | 157 | { |
| 137 | ASIHTTPRequest *request; | 158 | ASIHTTPRequest *request; |
| @@ -24,5 +24,7 @@ | @@ -24,5 +24,7 @@ | ||
| 24 | <string>MainMenu</string> | 24 | <string>MainMenu</string> |
| 25 | <key>NSPrincipalClass</key> | 25 | <key>NSPrincipalClass</key> |
| 26 | <string>NSApplication</string> | 26 | <string>NSApplication</string> |
| 27 | + <key>CFBundleDisplayName</key> | ||
| 28 | + <string>ASIHTTPRequest Demo</string> | ||
| 27 | </dict> | 29 | </dict> |
| 28 | </plist> | 30 | </plist> |
| @@ -24,5 +24,7 @@ | @@ -24,5 +24,7 @@ | ||
| 24 | <string>MainMenu</string> | 24 | <string>MainMenu</string> |
| 25 | <key>NSPrincipalClass</key> | 25 | <key>NSPrincipalClass</key> |
| 26 | <string>NSApplication</string> | 26 | <string>NSApplication</string> |
| 27 | + <key>CFBundleDisplayName</key> | ||
| 28 | + <string>ASIHTTPRequest Unit Tests</string> | ||
| 27 | </dict> | 29 | </dict> |
| 28 | </plist> | 30 | </plist> |
| @@ -9,7 +9,7 @@ | @@ -9,7 +9,7 @@ | ||
| 9 | <key>CFBundleIconFile</key> | 9 | <key>CFBundleIconFile</key> |
| 10 | <string>iphone-icon.png</string> | 10 | <string>iphone-icon.png</string> |
| 11 | <key>CFBundleIdentifier</key> | 11 | <key>CFBundleIdentifier</key> |
| 12 | - <string>com.yourcompany.etc</string> | 12 | + <string>com.yourcompany.${PRODUCT_NAME:identifier}</string> |
| 13 | <key>CFBundleInfoDictionaryVersion</key> | 13 | <key>CFBundleInfoDictionaryVersion</key> |
| 14 | <string>6.0</string> | 14 | <string>6.0</string> |
| 15 | <key>CFBundlePackageType</key> | 15 | <key>CFBundlePackageType</key> |
| @@ -20,5 +20,7 @@ | @@ -20,5 +20,7 @@ | ||
| 20 | <string>1.0</string> | 20 | <string>1.0</string> |
| 21 | <key>NSMainNibFile</key> | 21 | <key>NSMainNibFile</key> |
| 22 | <string>MainWindow</string> | 22 | <string>MainWindow</string> |
| 23 | + <key>CFBundleDisplayName</key> | ||
| 24 | + <string>ASIHTTPRequest Demo</string> | ||
| 23 | </dict> | 25 | </dict> |
| 24 | </plist> | 26 | </plist> |
| @@ -18,5 +18,7 @@ | @@ -18,5 +18,7 @@ | ||
| 18 | <string>1.0</string> | 18 | <string>1.0</string> |
| 19 | <key>NSMainNibFile</key> | 19 | <key>NSMainNibFile</key> |
| 20 | <string>GHUnitIPhone</string> | 20 | <string>GHUnitIPhone</string> |
| 21 | + <key>CFBundleDisplayName</key> | ||
| 22 | + <string>ASIHTTPRequest Unit Tests</string> | ||
| 21 | </dict> | 23 | </dict> |
| 22 | </plist> | 24 | </plist> |
This diff was suppressed by a .gitattributes entry.
-
Please register or login to post a comment