Permalink
Browse files

Add test for S3 gzipped content

Tweaks to use accessor for cancelledLock
  • Loading branch information...
1 parent 4f116f5 commit 0762d5418ef6a6362eb43f323a8ded622a82d052 @pokeb committed Jul 18, 2009
Showing with 60 additions and 20 deletions.
  1. +18 −18 Classes/ASIHTTPRequest.m
  2. +0 −1 Classes/Tests/ASIHTTPRequestTests.m
  3. +42 −1 Classes/Tests/ASIS3RequestTests.m
View
36 Classes/ASIHTTPRequest.m
@@ -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
@@ -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];
@@ -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;
}
@@ -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;
}
@@ -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) {
@@ -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);
@@ -636,7 +636,7 @@ - (void)cancelLoad
}
[self setResponseHeaders:nil];
- [cancelledLock unlock];
+ [[self cancelledLock] unlock];
}
@@ -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;
}
@@ -763,7 +763,7 @@ - (void)updateUploadProgress
- [cancelledLock unlock];
+ [[self cancelledLock] unlock];
if (totalBytesSent == 0) {
return;
@@ -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];
@@ -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];
@@ -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];
}
@@ -1571,7 +1571,6 @@ + (void)clearSession
[ASIHTTPRequest setSessionCookies:nil];
}
-
#pragma mark gzip decompression
//
@@ -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;
@@ -1906,4 +1905,5 @@ + (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest
@synthesize needsRedirect;
@synthesize redirectCount;
@synthesize shouldCompressRequestBody;
+@synthesize authenticationLock;
@end
View
1 Classes/Tests/ASIHTTPRequestTests.m
@@ -731,5 +731,4 @@ - (void)testCompression
}
-
@end
View
43 Classes/Tests/ASIS3RequestTests.m
@@ -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];
@@ -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
{

0 comments on commit 0762d54

Please sign in to comment.