Ben Copsey

Add test for S3 gzipped content

Tweaks to use accessor for cancelledLock
... ... @@ -72,6 +72,7 @@ static NSError *ASITooMuchRedirectionError;
@property (assign, nonatomic) int redirectCount;
@property (retain, nonatomic) NSData *compressedPostBody;
@property (retain, nonatomic) NSString *compressedPostBodyFilePath;
@property (retain) NSConditionLock *authenticationLock;
@end
@implementation ASIHTTPRequest
... ... @@ -428,15 +429,14 @@ static NSError *ASITooMuchRedirectionError;
- (void)startRequest
{
[cancelledLock lock];
[[self cancelledLock] lock];
if ([self isCancelled]) {
[cancelledLock unlock];
[[self cancelledLock] unlock];
return;
}
[authenticationLock release];
authenticationLock = [[NSConditionLock alloc] initWithCondition:1];
[self setAuthenticationLock:[[[NSConditionLock alloc] initWithCondition:1] autorelease]];
[self setComplete:NO];
[self setTotalBytesRead:0];
... ... @@ -470,7 +470,7 @@ static NSError *ASITooMuchRedirectionError;
readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
}
if (!readStream) {
[cancelledLock unlock];
[[self cancelledLock] unlock];
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create read stream",NSLocalizedDescriptionKey,nil]]];
return;
}
... ... @@ -502,7 +502,7 @@ static NSError *ASITooMuchRedirectionError;
if (!CFReadStreamSetClient(readStream, kNetworkEvents, ReadStreamClientCallBack, &ctxt)) {
CFRelease(readStream);
readStream = NULL;
[cancelledLock unlock];
[[self cancelledLock] unlock];
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to setup read stream",NSLocalizedDescriptionKey,nil]]];
return;
}
... ... @@ -516,11 +516,11 @@ static NSError *ASITooMuchRedirectionError;
CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), ASIHTTPRequestRunMode);
CFRelease(readStream);
readStream = NULL;
[cancelledLock unlock];
[[self cancelledLock] unlock];
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]];
return;
}
[cancelledLock unlock];
[[self cancelledLock] unlock];
if (shouldResetProgressIndicators) {
... ... @@ -606,7 +606,7 @@ static NSError *ASITooMuchRedirectionError;
// Cancel loading and clean up
- (void)cancelLoad
{
[cancelledLock lock];
[[self cancelledLock] lock];
if (readStream) {
CFReadStreamClose(readStream);
CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
... ... @@ -636,7 +636,7 @@ static NSError *ASITooMuchRedirectionError;
}
[self setResponseHeaders:nil];
[cancelledLock unlock];
[[self cancelledLock] unlock];
}
... ... @@ -738,9 +738,9 @@ static NSError *ASITooMuchRedirectionError;
- (void)updateUploadProgress
{
[cancelledLock lock];
[[self cancelledLock] lock];
if ([self isCancelled]) {
[cancelledLock unlock];
[[self cancelledLock] unlock];
return;
}
... ... @@ -763,7 +763,7 @@ static NSError *ASITooMuchRedirectionError;
[cancelledLock unlock];
[[self cancelledLock] unlock];
if (totalBytesSent == 0) {
return;
... ... @@ -1571,7 +1571,6 @@ static NSError *ASITooMuchRedirectionError;
[ASIHTTPRequest setSessionCookies:nil];
}
#pragma mark gzip decompression
//
... ... @@ -1656,7 +1655,7 @@ static NSError *ASITooMuchRedirectionError;
// http://www.zlib.net/zpipe.c
//
#define CHUNK 16384
#define SET_BINARY_MODE(file)
+ (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest
{
int ret;
... ... @@ -1906,4 +1905,5 @@ static NSError *ASITooMuchRedirectionError;
@synthesize needsRedirect;
@synthesize redirectCount;
@synthesize shouldCompressRequestBody;
@synthesize authenticationLock;
@end
... ...
... ... @@ -731,5 +731,4 @@
}
@end
... ...
... ... @@ -121,7 +121,7 @@ static NSString *bucket = @"";
NSString *path = @"/test";
// Create the fle
// Create the file
NSString *text = @"This is my content";
NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
... ... @@ -205,6 +205,47 @@ static NSString *bucket = @"";
GHAssertTrue(success, @"Got the wrong error message");
}
// Will upload a file to S3, gzipping it before uploading
// The file will be stored deflate, and automatically inflated when downloaded
// This means the file will take up less storage space, and will upload and download faster
// The file should still be accessible by any HTTP client that supports gzipped responses (eg browsers, NSURLConnection, etc)
- (void)testGZippedContent
{
// Create the file
NSString *text = @"This is my content This is my content This is my content This is my content This is my content This is my content";
NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
NSString *path = @"/gzipped-data";
ASIS3Request *request = [ASIS3Request PUTRequestForFile:filePath withBucket:bucket path:path];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request setShouldCompressRequestBody:YES];
[request setAccessPolicy:ASIS3AccessPolicyPublicRead]; // We'll make it public
[request start];
BOOL success = [[request responseString] isEqualToString:@""];
GHAssertTrue(success,@"Failed to PUT the gzipped file");
// GET the file
request = [ASIS3Request requestWithBucket:bucket path:path];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request start];
success = [[request responseString] isEqualToString:text];
GHAssertTrue(success,@"Failed to GET the correct data from S3");
success = [[[request responseHeaders] valueForKey:@"Content-Encoding"] isEqualToString:@"gzip"];
GHAssertTrue(success,@"Failed to GET the correct data from S3");
// Now grab the data using something other than ASIHTTPRequest to ensure other HTTP clients can parse the gzipped content
NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/gzipped-data",bucket]]] returningResponse:NULL error:NULL];
NSString *string = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease];
success = [string isEqualToString:text];
GHAssertTrue(success,@"Failed to GET the correct data from S3");
}
- (void)testListRequest
{
... ...