Ben Copsey

Added tests for S3 GET/PUT/DELETE

Fix mime-type function
... ... @@ -390,8 +390,7 @@ static NSError *ASITooMuchRedirectionError;
for (header in headers) {
CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)header, (CFStringRef)[requestHeaders objectForKey:header]);
}
// If this is a post request and we have data in memory send, add it to the request
if ([self postBody]) {
CFHTTPMessageSetBody(request, (CFDataRef)postBody);
... ...
... ... @@ -15,7 +15,7 @@ extern NSString *const ASIS3AccessPolicyPrivate; // This is the default in S3 wh
extern NSString *const ASIS3AccessPolicyPublicRead;
extern NSString *const ASIS3AccessPolicyPublicReadWrote;
extern NSString *const ASIS3AccessPolicyAuthenticatedRead;
bug report in lighthouse
@interface ASIS3Request : ASIHTTPRequest {
// Your S3 access key. Set it on the request, or set it globally using [ASIS3Request setSharedAccessKey:]
... ...
... ... @@ -37,7 +37,7 @@ static NSString *sharedSecretAccessKey = nil;
+ (id)requestWithBucket:(NSString *)bucket path:(NSString *)path
{
ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/%@",bucket,path]]] autorelease];
ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://s3.amazonaws.com/%@/%@",bucket,path]]] autorelease];
[request setBucket:bucket];
[request setPath:path];
return request;
... ... @@ -49,14 +49,14 @@ static NSString *sharedSecretAccessKey = nil;
[request setPostBodyFilePath:filePath];
[request setShouldStreamPostDataFromDisk:YES];
[request setRequestMethod:@"PUT"];
[request setMimeType:[ASIS3Request mimeTypeForFileAtPath:path]];
[request setMimeType:[ASIS3Request mimeTypeForFileAtPath:filePath]];
return request;
}
+ (id)listRequestWithBucket:(NSString *)bucket prefix:(NSString *)prefix maxResults:(int)maxResults marker:(NSString *)marker
{
ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/?prefix=/%@&max-keys=%hi&marker=%@",bucket,prefix,maxResults,marker]]] autorelease];
ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://s3.amazonaws.com/%@?prefix=/%@&max-keys=%hi&marker=%@",bucket,prefix,maxResults,marker]]] autorelease];
[request setBucket:bucket];
return request;
}
... ... @@ -74,12 +74,17 @@ static NSString *sharedSecretAccessKey = nil;
[task setLaunchPath: @"/usr/bin/file"];
[task setArguments:[NSMutableArray arrayWithObjects:@"-Ib",path,nil]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSPipe *outputPipe = [NSPipe pipe];
[task setStandardOutput:outputPipe];
NSFileHandle *file = [pipe fileHandleForReading];
NSFileHandle *file = [outputPipe fileHandleForReading];
[task launch];
[task waitUntilExit];
if ([task terminationStatus] != 0) {
return @"application/octet-stream";
}
NSString *mimeTypeString = [[[NSString alloc] initWithData:[file readDataToEndOfFile] encoding: NSUTF8StringEncoding] autorelease];
return [[mimeTypeString componentsSeparatedByString:@";"] objectAtIndex:0];
... ... @@ -89,7 +94,7 @@ static NSString *sharedSecretAccessKey = nil;
- (void)setDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzzz"];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
[self setDateString:[dateFormatter stringFromDate:date]];
}
... ... @@ -135,10 +140,10 @@ static NSString *sharedSecretAccessKey = nil;
[self addRequestHeader:@"Authorization" value:authorizationString];
}
- (void)startRequest
- (void)main
{
[self generateS3Headers];
[super startRequest];
[super main];
}
#pragma mark Shared access keys
... ...
... ... @@ -17,5 +17,6 @@
}
- (void)testAuthenticationHeaderGeneration;
//- (void)testREST;
@end
... ...
... ... @@ -79,10 +79,49 @@
[request generateS3Headers];
success = [[[request requestHeaders] valueForKey:@"Authorization"] isEqualToString:@"AWS 0PN5J17HBGZHT7JJ3X82:dxhSBHoI6eVSPcXJqEghlUzZMnY="];
GHAssertTrue(success,@"Failed to generate the correct authorisation header for a list request");
}
// To run this test, uncomment and fill in your S3 access details
/*
- (void)testREST
{
NSString *secretAccessKey = @"my-secret-key";
NSString *accessKey = @"my-access-key";
NSString *bucket = @"bucket-name";
NSString *path = @"path/to/file";
// Create the fle
NSString *text = @"This is my content";
NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
// PUT the file
ASIS3Request *request = [ASIS3Request PUTRequestForFile:filePath withBucket:bucket path:path];
[request setRequestMethod:@"PUT"];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request start];
BOOL success = [[request responseString] isEqualToString:@""];
GHAssertTrue(success,@"Failed to PUT a file to S3");
// GET the file
request = [ASIS3Request requestWithBucket:bucket path:path];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request start];
success = [[request responseString] isEqualToString:@"This is my content"];
GHAssertTrue(success,@"Failed to GET the correct data from S3");
// DELETE the file
request = [ASIS3Request requestWithBucket:bucket path:path];
[request setSecretAccessKey:secretAccessKey];
[request setRequestMethod:@"DELETE"];
[request setAccessKey:accessKey];
[request start];
success = [[request responseString] isEqualToString:@""];
GHAssertTrue(success,@"Failed to DELETE the file from S3");
}
*/
@end
... ...