Add gzip response handling, contributed by Enormego http://developers.enormego.c…
…om/view/asihttprequest_gzip File downloads are not playing nice with gzip yet Added compressed data tests
Showing
8 changed files
with
143 additions
and
34 deletions
| @@ -32,7 +32,7 @@ | @@ -32,7 +32,7 @@ | ||
| 32 | [request setFile:path forKey:@"file"]; | 32 | [request setFile:path forKey:@"file"]; |
| 33 | [request start]; | 33 | [request start]; |
| 34 | 34 | ||
| 35 | - BOOL success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]]); | 35 | + BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]]); |
| 36 | STAssertTrue(success,@"Failed to upload the correct data (using local file)"); | 36 | STAssertTrue(success,@"Failed to upload the correct data (using local file)"); |
| 37 | 37 | ||
| 38 | //Try the same with the raw data | 38 | //Try the same with the raw data |
| @@ -43,7 +43,7 @@ | @@ -43,7 +43,7 @@ | ||
| 43 | [request setData:data forKey:@"file"]; | 43 | [request setData:data forKey:@"file"]; |
| 44 | [request start]; | 44 | [request start]; |
| 45 | 45 | ||
| 46 | - success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"file",size]]); | 46 | + success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"file",size]]); |
| 47 | STAssertTrue(success,@"Failed to upload the correct data (using NSData)"); | 47 | STAssertTrue(success,@"Failed to upload the correct data (using NSData)"); |
| 48 | } | 48 | } |
| 49 | 49 |
| @@ -15,6 +15,7 @@ | @@ -15,6 +15,7 @@ | ||
| 15 | #if TARGET_OS_IPHONE | 15 | #if TARGET_OS_IPHONE |
| 16 | #import <CFNetwork/CFNetwork.h> | 16 | #import <CFNetwork/CFNetwork.h> |
| 17 | #endif | 17 | #endif |
| 18 | +#import <zlib.h> | ||
| 18 | 19 | ||
| 19 | typedef enum _ASINetworkErrorType { | 20 | typedef enum _ASINetworkErrorType { |
| 20 | ASIConnectionFailureErrorType = 1, | 21 | ASIConnectionFailureErrorType = 1, |
| @@ -62,6 +63,9 @@ typedef enum _ASINetworkErrorType { | @@ -62,6 +63,9 @@ typedef enum _ASINetworkErrorType { | ||
| 62 | // If useSessionPersistance is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called) | 63 | // If useSessionPersistance is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called) |
| 63 | BOOL useSessionPersistance; | 64 | BOOL useSessionPersistance; |
| 64 | 65 | ||
| 66 | + // If allowCompressedResponse is true, requests will inform the server they can accept compressed data, and will automatically decompress gzipped responses. Default is true. | ||
| 67 | + BOOL allowCompressedResponse; | ||
| 68 | + | ||
| 65 | // When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location | 69 | // When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location |
| 66 | // If downloadDestinationPath is not set, download data will be stored in memory | 70 | // If downloadDestinationPath is not set, download data will be stored in memory |
| 67 | NSString *downloadDestinationPath; | 71 | NSString *downloadDestinationPath; |
| @@ -95,8 +99,8 @@ typedef enum _ASINetworkErrorType { | @@ -95,8 +99,8 @@ typedef enum _ASINetworkErrorType { | ||
| 95 | // Whether we've seen the headers of the response yet | 99 | // Whether we've seen the headers of the response yet |
| 96 | BOOL haveExaminedHeaders; | 100 | BOOL haveExaminedHeaders; |
| 97 | 101 | ||
| 98 | - // Data we receive will be stored here | 102 | + // Data we receive will be stored here. Data may be compressed unless allowCompressedResponse is false - you should use [request responseData] instead in most cases |
| 99 | - NSMutableData *receivedData; | 103 | + NSMutableData *rawResponseData; |
| 100 | 104 | ||
| 101 | // Used for sending and receiving data | 105 | // Used for sending and receiving data |
| 102 | CFHTTPMessageRef request; | 106 | CFHTTPMessageRef request; |
| @@ -154,7 +158,7 @@ typedef enum _ASINetworkErrorType { | @@ -154,7 +158,7 @@ typedef enum _ASINetworkErrorType { | ||
| 154 | ASIHTTPRequest *mainRequest; | 158 | ASIHTTPRequest *mainRequest; |
| 155 | 159 | ||
| 156 | // When NO, this request will only update the progress indicator when it completes | 160 | // When NO, this request will only update the progress indicator when it completes |
| 157 | - // When YES, this request will update the progress indicator according to how much data it has recieved so far | 161 | + // When YES, this request will update the progress indicator according to how much data it has received so far |
| 158 | // The default for requests is YES | 162 | // The default for requests is YES |
| 159 | // Also see the comments in ASINetworkQueue.h | 163 | // Also see the comments in ASINetworkQueue.h |
| 160 | BOOL showAccurateProgress; | 164 | BOOL showAccurateProgress; |
| @@ -185,8 +189,11 @@ typedef enum _ASINetworkErrorType { | @@ -185,8 +189,11 @@ typedef enum _ASINetworkErrorType { | ||
| 185 | 189 | ||
| 186 | #pragma mark get information about this request | 190 | #pragma mark get information about this request |
| 187 | 191 | ||
| 188 | -// Returns the contents of the result as an NSString (not appropriate for binary data - used receivedData instead) | 192 | +// Returns the contents of the result as an NSString (not appropriate for binary data - used responseData instead) |
| 189 | -- (NSString *)dataString; | 193 | +- (NSString *)responseString; |
| 194 | + | ||
| 195 | +// Response data, automatically uncompressed where appropriate | ||
| 196 | +- (NSData *)responseData; | ||
| 190 | 197 | ||
| 191 | #pragma mark request logic | 198 | #pragma mark request logic |
| 192 | 199 | ||
| @@ -272,6 +279,11 @@ typedef enum _ASINetworkErrorType { | @@ -272,6 +279,11 @@ typedef enum _ASINetworkErrorType { | ||
| 272 | // Dump all session data (authentication and cookies) | 279 | // Dump all session data (authentication and cookies) |
| 273 | + (void)clearSession; | 280 | + (void)clearSession; |
| 274 | 281 | ||
| 282 | +#pragma mark gzip compression | ||
| 283 | + | ||
| 284 | +// Uncompress gzipped data with zlib | ||
| 285 | ++ (NSData *)uncompressZippedData:(NSData*)compressedData; | ||
| 286 | + | ||
| 275 | 287 | ||
| 276 | @property (retain) NSString *username; | 288 | @property (retain) NSString *username; |
| 277 | @property (retain) NSString *password; | 289 | @property (retain) NSString *password; |
| @@ -296,7 +308,7 @@ typedef enum _ASINetworkErrorType { | @@ -296,7 +308,7 @@ typedef enum _ASINetworkErrorType { | ||
| 296 | @property (assign) BOOL useCookiePersistance; | 308 | @property (assign) BOOL useCookiePersistance; |
| 297 | @property (retain) NSDictionary *requestCredentials; | 309 | @property (retain) NSDictionary *requestCredentials; |
| 298 | @property (assign) int responseStatusCode; | 310 | @property (assign) int responseStatusCode; |
| 299 | -@property (retain) NSMutableData *receivedData; | 311 | +@property (retain) NSMutableData *rawResponseData; |
| 300 | @property (retain) NSDate *lastActivityTime; | 312 | @property (retain) NSDate *lastActivityTime; |
| 301 | @property (assign) NSTimeInterval timeOutSeconds; | 313 | @property (assign) NSTimeInterval timeOutSeconds; |
| 302 | @property (retain) NSString *requestMethod; | 314 | @property (retain) NSString *requestMethod; |
| @@ -310,4 +322,5 @@ typedef enum _ASINetworkErrorType { | @@ -310,4 +322,5 @@ typedef enum _ASINetworkErrorType { | ||
| 310 | @property (assign) unsigned long long uploadBufferSize; | 322 | @property (assign) unsigned long long uploadBufferSize; |
| 311 | @property (assign) NSStringEncoding defaultResponseEncoding; | 323 | @property (assign) NSStringEncoding defaultResponseEncoding; |
| 312 | @property (assign) NSStringEncoding responseEncoding; | 324 | @property (assign) NSStringEncoding responseEncoding; |
| 325 | +@property (assign) BOOL allowCompressedResponse; | ||
| 313 | @end | 326 | @end |
| @@ -72,6 +72,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -72,6 +72,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 72 | requestAuthentication = NULL; | 72 | requestAuthentication = NULL; |
| 73 | haveBuiltPostBody = NO; | 73 | haveBuiltPostBody = NO; |
| 74 | request = NULL; | 74 | request = NULL; |
| 75 | + [self setAllowCompressedResponse:YES]; | ||
| 75 | [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; | 76 | [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; |
| 76 | [self setUploadBufferSize:0]; | 77 | [self setUploadBufferSize:0]; |
| 77 | [self setResponseHeaders:nil]; | 78 | [self setResponseHeaders:nil]; |
| @@ -113,7 +114,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -113,7 +114,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 113 | [authenticationLock release]; | 114 | [authenticationLock release]; |
| 114 | [lastActivityTime release]; | 115 | [lastActivityTime release]; |
| 115 | [responseCookies release]; | 116 | [responseCookies release]; |
| 116 | - [receivedData release]; | 117 | + [rawResponseData release]; |
| 117 | [responseHeaders release]; | 118 | [responseHeaders release]; |
| 118 | [requestMethod release]; | 119 | [requestMethod release]; |
| 119 | [cancelledLock release]; | 120 | [cancelledLock release]; |
| @@ -166,17 +167,28 @@ static NSError *ASIUnableToCreateRequestError; | @@ -166,17 +167,28 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 166 | } | 167 | } |
| 167 | 168 | ||
| 168 | 169 | ||
| 169 | -// Call this method to get the recieved data as an NSString. Don't use for Binary data! | 170 | +// Call this method to get the received data as an NSString. Don't use for Binary data! |
| 170 | -- (NSString *)dataString | 171 | +- (NSString *)responseString |
| 171 | { | 172 | { |
| 172 | - if (!receivedData) { | 173 | + NSData *data = [self responseData]; |
| 174 | + if (!data) { | ||
| 173 | return nil; | 175 | return nil; |
| 174 | } | 176 | } |
| 175 | 177 | ||
| 176 | - return [[[NSString alloc] initWithBytes:[receivedData bytes] length:[receivedData length] encoding:[self responseEncoding]] autorelease]; | 178 | + return [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease]; |
| 177 | } | 179 | } |
| 178 | 180 | ||
| 179 | 181 | ||
| 182 | +- (NSData *)responseData | ||
| 183 | +{ | ||
| 184 | + NSString *encoding = [[self responseHeaders] objectForKey:@"Content-Encoding"]; | ||
| 185 | + if(encoding && [encoding rangeOfString:@"gzip"].location != NSNotFound) { | ||
| 186 | + return [ASIHTTPRequest uncompressZippedData:[self rawResponseData]]; | ||
| 187 | + } else { | ||
| 188 | + return [self rawResponseData]; | ||
| 189 | + } | ||
| 190 | +} | ||
| 191 | + | ||
| 180 | #pragma mark request logic | 192 | #pragma mark request logic |
| 181 | 193 | ||
| 182 | // Create the request | 194 | // Create the request |
| @@ -243,6 +255,12 @@ static NSError *ASIUnableToCreateRequestError; | @@ -243,6 +255,12 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 243 | [self buildPostBody]; | 255 | [self buildPostBody]; |
| 244 | } | 256 | } |
| 245 | 257 | ||
| 258 | + | ||
| 259 | + // Accept a compressed response | ||
| 260 | + if ([self allowCompressedResponse]) { | ||
| 261 | + [self addRequestHeader:@"Accept-Encoding" value:@"gzip"]; | ||
| 262 | + } | ||
| 263 | + | ||
| 246 | // Add custom headers | 264 | // Add custom headers |
| 247 | NSDictionary *headers; | 265 | NSDictionary *headers; |
| 248 | 266 | ||
| @@ -296,7 +314,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -296,7 +314,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 296 | contentLength = 0; | 314 | contentLength = 0; |
| 297 | } | 315 | } |
| 298 | [self setResponseHeaders:nil]; | 316 | [self setResponseHeaders:nil]; |
| 299 | - [self setReceivedData:[[[NSMutableData alloc] init] autorelease]]; | 317 | + [self setRawResponseData:[[[NSMutableData alloc] init] autorelease]]; |
| 300 | 318 | ||
| 301 | // Create the stream for the request. | 319 | // Create the stream for the request. |
| 302 | readStream = CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,readStream); | 320 | readStream = CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,readStream); |
| @@ -395,8 +413,8 @@ static NSError *ASIUnableToCreateRequestError; | @@ -395,8 +413,8 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 395 | readStream = NULL; | 413 | readStream = NULL; |
| 396 | } | 414 | } |
| 397 | 415 | ||
| 398 | - if (receivedData) { | 416 | + if (rawResponseData) { |
| 399 | - [self setReceivedData:nil]; | 417 | + [self setRawResponseData:nil]; |
| 400 | 418 | ||
| 401 | // If we were downloading to a file, let's remove it | 419 | // If we were downloading to a file, let's remove it |
| 402 | } else if (downloadDestinationPath) { | 420 | } else if (downloadDestinationPath) { |
| @@ -1025,7 +1043,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -1025,7 +1043,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 1025 | 1043 | ||
| 1026 | //Otherwise, let's add the data to our in-memory store | 1044 | //Otherwise, let's add the data to our in-memory store |
| 1027 | } else { | 1045 | } else { |
| 1028 | - [receivedData appendBytes:buffer length:bytesRead]; | 1046 | + [rawResponseData appendBytes:buffer length:bytesRead]; |
| 1029 | } | 1047 | } |
| 1030 | } | 1048 | } |
| 1031 | 1049 | ||
| @@ -1164,6 +1182,59 @@ static NSError *ASIUnableToCreateRequestError; | @@ -1164,6 +1182,59 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 1164 | } | 1182 | } |
| 1165 | 1183 | ||
| 1166 | 1184 | ||
| 1185 | +#pragma mark gzip data handling | ||
| 1186 | + | ||
| 1187 | +// | ||
| 1188 | +// Contributed by Shaun Harrison of Enormego, see: http://developers.enormego.com/view/asihttprequest_gzip | ||
| 1189 | +// Based on this: http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html | ||
| 1190 | +// | ||
| 1191 | ++ (NSData *)uncompressZippedData:(NSData*)compressedData | ||
| 1192 | +{ | ||
| 1193 | + if ([compressedData length] == 0) return compressedData; | ||
| 1194 | + | ||
| 1195 | + unsigned full_length = [compressedData length]; | ||
| 1196 | + unsigned half_length = [compressedData length] / 2; | ||
| 1197 | + | ||
| 1198 | + NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length]; | ||
| 1199 | + BOOL done = NO; | ||
| 1200 | + int status; | ||
| 1201 | + | ||
| 1202 | + z_stream strm; | ||
| 1203 | + strm.next_in = (Bytef *)[compressedData bytes]; | ||
| 1204 | + strm.avail_in = [compressedData length]; | ||
| 1205 | + strm.total_out = 0; | ||
| 1206 | + strm.zalloc = Z_NULL; | ||
| 1207 | + strm.zfree = Z_NULL; | ||
| 1208 | + | ||
| 1209 | + if (inflateInit2(&strm, (15+32)) != Z_OK) return nil; | ||
| 1210 | + | ||
| 1211 | + while (!done) { | ||
| 1212 | + // Make sure we have enough room and reset the lengths. | ||
| 1213 | + if (strm.total_out >= [decompressed length]) { | ||
| 1214 | + [decompressed increaseLengthBy: half_length]; | ||
| 1215 | + } | ||
| 1216 | + strm.next_out = [decompressed mutableBytes] + strm.total_out; | ||
| 1217 | + strm.avail_out = [decompressed length] - strm.total_out; | ||
| 1218 | + | ||
| 1219 | + // Inflate another chunk. | ||
| 1220 | + status = inflate (&strm, Z_SYNC_FLUSH); | ||
| 1221 | + if (status == Z_STREAM_END) { | ||
| 1222 | + done = YES; | ||
| 1223 | + } else if (status != Z_OK) { | ||
| 1224 | + break; | ||
| 1225 | + } | ||
| 1226 | + } | ||
| 1227 | + if (inflateEnd (&strm) != Z_OK) return nil; | ||
| 1228 | + | ||
| 1229 | + // Set real length. | ||
| 1230 | + if (done) { | ||
| 1231 | + [decompressed setLength: strm.total_out]; | ||
| 1232 | + return [NSData dataWithData: decompressed]; | ||
| 1233 | + } else { | ||
| 1234 | + return nil; | ||
| 1235 | + } | ||
| 1236 | +} | ||
| 1237 | + | ||
| 1167 | @synthesize username; | 1238 | @synthesize username; |
| 1168 | @synthesize password; | 1239 | @synthesize password; |
| 1169 | @synthesize domain; | 1240 | @synthesize domain; |
| @@ -1186,7 +1257,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -1186,7 +1257,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 1186 | @synthesize requestCookies; | 1257 | @synthesize requestCookies; |
| 1187 | @synthesize requestCredentials; | 1258 | @synthesize requestCredentials; |
| 1188 | @synthesize responseStatusCode; | 1259 | @synthesize responseStatusCode; |
| 1189 | -@synthesize receivedData; | 1260 | +@synthesize rawResponseData; |
| 1190 | @synthesize lastActivityTime; | 1261 | @synthesize lastActivityTime; |
| 1191 | @synthesize timeOutSeconds; | 1262 | @synthesize timeOutSeconds; |
| 1192 | @synthesize requestMethod; | 1263 | @synthesize requestMethod; |
| @@ -1201,4 +1272,5 @@ static NSError *ASIUnableToCreateRequestError; | @@ -1201,4 +1272,5 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 1201 | @synthesize uploadBufferSize; | 1272 | @synthesize uploadBufferSize; |
| 1202 | @synthesize defaultResponseEncoding; | 1273 | @synthesize defaultResponseEncoding; |
| 1203 | @synthesize responseEncoding; | 1274 | @synthesize responseEncoding; |
| 1275 | +@synthesize allowCompressedResponse; | ||
| 1204 | @end | 1276 | @end |
| @@ -25,5 +25,6 @@ | @@ -25,5 +25,6 @@ | ||
| 25 | - (void)testBasicAuthentication; | 25 | - (void)testBasicAuthentication; |
| 26 | - (void)testDigestAuthentication; | 26 | - (void)testDigestAuthentication; |
| 27 | - (void)testCharacterEncoding; | 27 | - (void)testCharacterEncoding; |
| 28 | +- (void)testCompressedResponse; | ||
| 28 | 29 | ||
| 29 | @end | 30 | @end |
| @@ -20,7 +20,7 @@ | @@ -20,7 +20,7 @@ | ||
| 20 | NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com"] autorelease]; | 20 | NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com"] autorelease]; |
| 21 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 21 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 22 | [request start]; | 22 | [request start]; |
| 23 | - NSString *html = [request dataString]; | 23 | + NSString *html = [request responseString]; |
| 24 | STAssertNotNil(html,@"Basic synchronous request failed"); | 24 | STAssertNotNil(html,@"Basic synchronous request failed"); |
| 25 | 25 | ||
| 26 | // Check we're getting the correct response headers | 26 | // Check we're getting the correct response headers |
| @@ -97,7 +97,7 @@ | @@ -97,7 +97,7 @@ | ||
| 97 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 97 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 98 | [request setRequestMethod:method]; | 98 | [request setRequestMethod:method]; |
| 99 | [request start]; | 99 | [request start]; |
| 100 | - BOOL success = [[request dataString] isEqualToString:method]; | 100 | + BOOL success = [[request responseString] isEqualToString:method]; |
| 101 | STAssertTrue(success,@"Failed to set the request method correctly"); | 101 | STAssertTrue(success,@"Failed to set the request method correctly"); |
| 102 | } | 102 | } |
| 103 | } | 103 | } |
| @@ -110,7 +110,7 @@ | @@ -110,7 +110,7 @@ | ||
| 110 | [request setPostBody:[NSMutableData dataWithLength:1024*32]]; | 110 | [request setPostBody:[NSMutableData dataWithLength:1024*32]]; |
| 111 | [request start]; | 111 | [request start]; |
| 112 | 112 | ||
| 113 | - BOOL success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"%hu",(1024*32)]]); | 113 | + BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"%hu",(1024*32)]]); |
| 114 | STAssertTrue(success,@"Sent wrong content length"); | 114 | STAssertTrue(success,@"Sent wrong content length"); |
| 115 | } | 115 | } |
| 116 | 116 | ||
| @@ -133,6 +133,7 @@ | @@ -133,6 +133,7 @@ | ||
| 133 | [request1 setDownloadDestinationPath:path]; | 133 | [request1 setDownloadDestinationPath:path]; |
| 134 | [request1 start]; | 134 | [request1 start]; |
| 135 | 135 | ||
| 136 | + NSString *s = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path]]; | ||
| 136 | BOOL success = [[NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path]] isEqualToString:@"This is the expected content for the first string"]; | 137 | BOOL success = [[NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path]] isEqualToString:@"This is the expected content for the first string"]; |
| 137 | STAssertTrue(success,@"Failed to download data to a file"); | 138 | STAssertTrue(success,@"Failed to download data to a file"); |
| 138 | } | 139 | } |
| @@ -180,7 +181,7 @@ | @@ -180,7 +181,7 @@ | ||
| 180 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 181 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 181 | [request setUseCookiePersistance:YES]; | 182 | [request setUseCookiePersistance:YES]; |
| 182 | [request start]; | 183 | [request start]; |
| 183 | - NSString *html = [request dataString]; | 184 | + NSString *html = [request responseString]; |
| 184 | success = [html isEqualToString:@"I have set a cookie"]; | 185 | success = [html isEqualToString:@"I have set a cookie"]; |
| 185 | STAssertTrue(success,@"Failed to set a cookie"); | 186 | STAssertTrue(success,@"Failed to set a cookie"); |
| 186 | 187 | ||
| @@ -216,7 +217,7 @@ | @@ -216,7 +217,7 @@ | ||
| 216 | [request setUseCookiePersistance:NO]; | 217 | [request setUseCookiePersistance:NO]; |
| 217 | [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]]; | 218 | [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]]; |
| 218 | [request start]; | 219 | [request start]; |
| 219 | - html = [request dataString]; | 220 | + html = [request responseString]; |
| 220 | success = [html isEqualToString:@"I have 'This is the value' as the value of 'ASIHTTPRequestTestCookie'"]; | 221 | success = [html isEqualToString:@"I have 'This is the value' as the value of 'ASIHTTPRequestTestCookie'"]; |
| 221 | STAssertTrue(success,@"Cookie not presented to the server with cookie persistance OFF"); | 222 | STAssertTrue(success,@"Cookie not presented to the server with cookie persistance OFF"); |
| 222 | 223 | ||
| @@ -225,7 +226,7 @@ | @@ -225,7 +226,7 @@ | ||
| 225 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 226 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 226 | [request setUseCookiePersistance:YES]; | 227 | [request setUseCookiePersistance:YES]; |
| 227 | [request start]; | 228 | [request start]; |
| 228 | - html = [request dataString]; | 229 | + html = [request responseString]; |
| 229 | success = [html isEqualToString:@"I have 'This is the value' as the value of 'ASIHTTPRequestTestCookie'"]; | 230 | success = [html isEqualToString:@"I have 'This is the value' as the value of 'ASIHTTPRequestTestCookie'"]; |
| 230 | STAssertTrue(success,@"Cookie not presented to the server with cookie persistance ON"); | 231 | STAssertTrue(success,@"Cookie not presented to the server with cookie persistance ON"); |
| 231 | 232 | ||
| @@ -233,7 +234,7 @@ | @@ -233,7 +234,7 @@ | ||
| 233 | url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/remove_cookie"] autorelease]; | 234 | url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/remove_cookie"] autorelease]; |
| 234 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 235 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 235 | [request start]; | 236 | [request start]; |
| 236 | - html = [request dataString]; | 237 | + html = [request responseString]; |
| 237 | success = [html isEqualToString:@"I have removed a cookie"]; | 238 | success = [html isEqualToString:@"I have removed a cookie"]; |
| 238 | STAssertTrue(success,@"Failed to remove a cookie"); | 239 | STAssertTrue(success,@"Failed to remove a cookie"); |
| 239 | 240 | ||
| @@ -241,7 +242,7 @@ | @@ -241,7 +242,7 @@ | ||
| 241 | url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease]; | 242 | url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease]; |
| 242 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 243 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 243 | [request start]; | 244 | [request start]; |
| 244 | - html = [request dataString]; | 245 | + html = [request responseString]; |
| 245 | success = [html isEqualToString:@"No cookie exists"]; | 246 | success = [html isEqualToString:@"No cookie exists"]; |
| 246 | STAssertTrue(success,@"Cookie presented to the server when it should have been removed"); | 247 | STAssertTrue(success,@"Cookie presented to the server when it should have been removed"); |
| 247 | 248 | ||
| @@ -259,7 +260,7 @@ | @@ -259,7 +260,7 @@ | ||
| 259 | [request setUseCookiePersistance:NO]; | 260 | [request setUseCookiePersistance:NO]; |
| 260 | [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]]; | 261 | [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]]; |
| 261 | [request start]; | 262 | [request start]; |
| 262 | - html = [request dataString]; | 263 | + html = [request responseString]; |
| 263 | success = [html isEqualToString:@"I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'"]; | 264 | success = [html isEqualToString:@"I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'"]; |
| 264 | STAssertTrue(success,@"Custom cookie not presented to the server with cookie persistance OFF"); | 265 | STAssertTrue(success,@"Custom cookie not presented to the server with cookie persistance OFF"); |
| 265 | 266 | ||
| @@ -275,7 +276,7 @@ | @@ -275,7 +276,7 @@ | ||
| 275 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 276 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 276 | [request setUseCookiePersistance:YES]; | 277 | [request setUseCookiePersistance:YES]; |
| 277 | [request start]; | 278 | [request start]; |
| 278 | - html = [request dataString]; | 279 | + html = [request responseString]; |
| 279 | success = [html isEqualToString:@"No cookie exists"]; | 280 | success = [html isEqualToString:@"No cookie exists"]; |
| 280 | STAssertTrue(success,@"Cookie presented to the server when it should have been removed"); | 281 | STAssertTrue(success,@"Cookie presented to the server when it should have been removed"); |
| 281 | } | 282 | } |
| @@ -398,7 +399,29 @@ | @@ -398,7 +399,29 @@ | ||
| 398 | [request start]; | 399 | [request start]; |
| 399 | success = [[request error] code] == ASIAuthenticationErrorType; | 400 | success = [[request error] code] == ASIAuthenticationErrorType; |
| 400 | STAssertTrue(success,@"Failed to clear credentials"); | 401 | STAssertTrue(success,@"Failed to clear credentials"); |
| 402 | +} | ||
| 401 | 403 | ||
| 404 | +- (void)testCompressedResponse | ||
| 405 | +{ | ||
| 406 | + // allseeing-i.com does not gzip png images | ||
| 407 | + NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/i/logo.png"] autorelease]; | ||
| 408 | + ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
| 409 | + [request start]; | ||
| 410 | + NSString *encoding = [[request responseHeaders] objectForKey:@"Content-Encoding"]; | ||
| 411 | + BOOL success = (!encoding || [encoding rangeOfString:@"gzip"].location != NSNotFound); | ||
| 412 | + STAssertTrue(success,@"Got incorrect request headers from server"); | ||
| 413 | + | ||
| 414 | + success = ([request rawResponseData] == [request responseData]); | ||
| 415 | + STAssertTrue(success,@"Attempted to uncompress data that was not compressed"); | ||
| 416 | + | ||
| 417 | + url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/first"] autorelease]; | ||
| 418 | + request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | ||
| 419 | + [request start]; | ||
| 420 | + success = ([request rawResponseData] != [request responseData]); | ||
| 421 | + STAssertTrue(success,@"Uncompressed data is the same as compressed data"); | ||
| 422 | + | ||
| 423 | + success = [[request responseString] isEqualToString:@"This is the expected content for the first string"]; | ||
| 424 | + STAssertTrue(success,@"Failed to decompress data correctly?"); | ||
| 402 | } | 425 | } |
| 403 | 426 | ||
| 404 | 427 |
| @@ -126,19 +126,19 @@ | @@ -126,19 +126,19 @@ | ||
| 126 | success = ([request1 error] == nil); | 126 | success = ([request1 error] == nil); |
| 127 | STAssertTrue(success,@"Request 1 failed"); | 127 | STAssertTrue(success,@"Request 1 failed"); |
| 128 | 128 | ||
| 129 | - success = [[request1 dataString] isEqualToString:@"This is the expected content for the first string"]; | 129 | + success = [[request1 responseString] isEqualToString:@"This is the expected content for the first string"]; |
| 130 | STAssertTrue(success,@"Failed to download the correct data for request 1"); | 130 | STAssertTrue(success,@"Failed to download the correct data for request 1"); |
| 131 | 131 | ||
| 132 | success = ([request2 error] == nil); | 132 | success = ([request2 error] == nil); |
| 133 | STAssertTrue(success,@"Request 2 failed"); | 133 | STAssertTrue(success,@"Request 2 failed"); |
| 134 | 134 | ||
| 135 | - success = [[request2 dataString] isEqualToString:@"This is the expected content for the second string"]; | 135 | + success = [[request2 responseString] isEqualToString:@"This is the expected content for the second string"]; |
| 136 | STAssertTrue(success,@"Failed to download the correct data for request 2"); | 136 | STAssertTrue(success,@"Failed to download the correct data for request 2"); |
| 137 | 137 | ||
| 138 | success = ([request3 error] == nil); | 138 | success = ([request3 error] == nil); |
| 139 | STAssertTrue(success,@"Request 3 failed"); | 139 | STAssertTrue(success,@"Request 3 failed"); |
| 140 | 140 | ||
| 141 | - success = [[request3 dataString] isEqualToString:@"This is the expected content for the third string"]; | 141 | + success = [[request3 responseString] isEqualToString:@"This is the expected content for the third string"]; |
| 142 | STAssertTrue(success,@"Failed to download the correct data for request 3"); | 142 | STAssertTrue(success,@"Failed to download the correct data for request 3"); |
| 143 | 143 | ||
| 144 | success = ([requestThatShouldFail error] != nil); | 144 | success = ([requestThatShouldFail error] != nil); |
| @@ -34,8 +34,8 @@ | @@ -34,8 +34,8 @@ | ||
| 34 | [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; | 34 | [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; |
| 35 | 35 | ||
| 36 | [request start]; | 36 | [request start]; |
| 37 | - if ([request dataString]) { | 37 | + if ([request responseString]) { |
| 38 | - [htmlSource setString:[request dataString]]; | 38 | + [htmlSource setString:[request responseString]]; |
| 39 | } | 39 | } |
| 40 | } | 40 | } |
| 41 | 41 | ||
| @@ -131,7 +131,7 @@ | @@ -131,7 +131,7 @@ | ||
| 131 | - (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)request | 131 | - (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)request |
| 132 | { | 132 | { |
| 133 | if (![request error]) { | 133 | if (![request error]) { |
| 134 | - [topSecretInfo setStringValue:[request dataString]]; | 134 | + [topSecretInfo setStringValue:[request responseString]]; |
| 135 | [topSecretInfo setFont:[NSFont boldSystemFontOfSize:13]]; | 135 | [topSecretInfo setFont:[NSFont boldSystemFontOfSize:13]]; |
| 136 | } | 136 | } |
| 137 | } | 137 | } |
This diff was suppressed by a .gitattributes entry.
-
Please register or login to post a comment