Permalink
Browse files

Added tests for S3 GET/PUT/DELETE

Fix mime-type function
  • Loading branch information...
1 parent 70389c8 commit 3ab29ef07e4d02f599f00c49afd8f374f501353e @pokeb committed Jul 13, 2009
View
@@ -390,8 +390,7 @@ - (void)main
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);
View
@@ -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:]
View
@@ -37,7 +37,7 @@ - (void)dealloc
+ (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 @@ + (id)PUTRequestForFile:(NSString *)filePath withBucket:(NSString *)bucket path:
[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 @@ + (NSString *)mimeTypeForFileAtPath:(NSString *)path
[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 @@ + (NSString *)mimeTypeForFileAtPath:(NSString *)path
- (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 @@ - (void)generateS3Headers
[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 @@ - (void)testAuthenticationHeaderGeneration
[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

0 comments on commit 3ab29ef

Please sign in to comment.