Skip to content

Commit

Permalink
Add test for S3 gzipped content
Browse files Browse the repository at this point in the history
Tweaks to use accessor for cancelledLock
  • Loading branch information
pokeb committed Jul 18, 2009
1 parent 4f116f5 commit 0762d54
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
36 changes: 18 additions & 18 deletions Classes/ASIHTTPRequest.m
Expand Up @@ -72,6 +72,7 @@ @interface ASIHTTPRequest ()
@property (assign, nonatomic) int redirectCount;
@property (retain, nonatomic) NSData *compressedPostBody;
@property (retain, nonatomic) NSString *compressedPostBodyFilePath;
@property (retain) NSConditionLock *authenticationLock;
@end

@implementation ASIHTTPRequest
Expand Down Expand Up @@ -428,15 +429,14 @@ - (void)main

- (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];
Expand Down Expand Up @@ -470,7 +470,7 @@ - (void)startRequest
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;
}
Expand Down Expand Up @@ -502,7 +502,7 @@ - (void)startRequest
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;
}
Expand All @@ -516,11 +516,11 @@ - (void)startRequest
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) {
Expand Down Expand Up @@ -606,7 +606,7 @@ - (void)loadRequest
// Cancel loading and clean up
- (void)cancelLoad
{
[cancelledLock lock];
[[self cancelledLock] lock];
if (readStream) {
CFReadStreamClose(readStream);
CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
Expand Down Expand Up @@ -636,7 +636,7 @@ - (void)cancelLoad
}

[self setResponseHeaders:nil];
[cancelledLock unlock];
[[self cancelledLock] unlock];
}


Expand Down Expand Up @@ -738,9 +738,9 @@ - (void)resetUploadProgress:(unsigned long long)value

- (void)updateUploadProgress
{
[cancelledLock lock];
[[self cancelledLock] lock];
if ([self isCancelled]) {
[cancelledLock unlock];
[[self cancelledLock] unlock];
return;
}

Expand All @@ -763,7 +763,7 @@ - (void)updateUploadProgress



[cancelledLock unlock];
[[self cancelledLock] unlock];

if (totalBytesSent == 0) {
return;
Expand Down Expand Up @@ -1250,7 +1250,7 @@ - (void)attemptToApplyCredentialsAndResume
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]];
}

// Are a user name & password needed?
// Are a user name & password needed?
} else if (CFHTTPAuthenticationRequiresUserNameAndPassword(requestAuthentication)) {

NSMutableDictionary *newCredentials = [self findCredentials];
Expand Down Expand Up @@ -1341,7 +1341,7 @@ - (void)handleBytesAvailable
if (bytesRead < 0) {
[self handleStreamError];

// If zero bytes were read, wait for the EOF to come.
// If zero bytes were read, wait for the EOF to come.
} else if (bytesRead) {

[self setTotalBytesRead:[self totalBytesRead]+bytesRead];
Expand All @@ -1361,7 +1361,7 @@ - (void)handleBytesAvailable
}
[fileDownloadOutputStream write:buffer maxLength:bytesRead];

//Otherwise, let's add the data to our in-memory store
//Otherwise, let's add the data to our in-memory store
} else {
[rawResponseData appendBytes:buffer length:bytesRead];
}
Expand Down Expand Up @@ -1571,7 +1571,6 @@ + (void)clearSession
[ASIHTTPRequest setSessionCookies:nil];
}


#pragma mark gzip decompression

//
Expand Down Expand Up @@ -1656,7 +1655,7 @@ + (int)uncompressZippedDataFromFile:(NSString *)sourcePath toFile:(NSString *)de
// http://www.zlib.net/zpipe.c
//
#define CHUNK 16384
#define SET_BINARY_MODE(file)

+ (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest
{
int ret;
Expand Down Expand Up @@ -1906,4 +1905,5 @@ + (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest
@synthesize needsRedirect;
@synthesize redirectCount;
@synthesize shouldCompressRequestBody;
@synthesize authenticationLock;
@end
1 change: 0 additions & 1 deletion Classes/Tests/ASIHTTPRequestTests.m
Expand Up @@ -731,5 +731,4 @@ - (void)testCompression

}


@end
43 changes: 42 additions & 1 deletion Classes/Tests/ASIS3RequestTests.m
Expand Up @@ -121,7 +121,7 @@ - (void)testREST

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];
Expand Down Expand Up @@ -205,6 +205,47 @@ - (void)testREST
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
{

Expand Down

0 comments on commit 0762d54

Please sign in to comment.