Ben Copsey

Added new method to set file data on ASIFormDataRequest with an NSData object

Set content-length for request when setting postBody
Tweak tests
... ... @@ -23,7 +23,10 @@
// Add a POST variable to the request
- (void)setPostValue:(id)value forKey:(NSString *)key;
// Add the contents of a local file as a POST variable to the request
// Add the contents of a local file to the request
- (void)setFile:(NSString *)filePath forKey:(NSString *)key;
// Add the contents of an NSData object to the request
- (void)setData:(NSData *)data forKey:(NSString *)key;
@end
... ...
... ... @@ -44,10 +44,25 @@
if (!fileData) {
fileData = [[NSMutableDictionary alloc] init];
}
[fileData setValue:filePath forKey:key];
NSMutableDictionary *file = [[[NSMutableDictionary alloc] init] autorelease];
[file setObject:[NSData dataWithContentsOfFile:filePath options:NSUncachedRead error:NULL] forKey:@"data"];
[file setObject:[filePath lastPathComponent] forKey:@"filename"];
[fileData setValue:file forKey:key];
[self setRequestMethod:@"POST"];
}
- (void)setData:(NSData *)data forKey:(NSString *)key
{
if (!fileData) {
fileData = [[NSMutableDictionary alloc] init];
}
NSMutableDictionary *file = [[[NSMutableDictionary alloc] init] autorelease];
[file setObject:data forKey:@"data"];
[file setObject:@"file" forKey:@"filename"];
[fileData setValue:file forKey:key];
[self setRequestMethod:@"POST"];
}
- (void)buildPostBody
{
if (!postData && ! fileData) {
... ... @@ -83,10 +98,10 @@
e = [fileData keyEnumerator];
i=0;
while (key = [e nextObject]) {
NSString *filePath = [fileData objectForKey:key];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",key,[filePath lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *fileInfo = [fileData objectForKey:key];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",key,[fileInfo objectForKey:@"filename"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:contentTypeHeader];
[body appendData: [NSData dataWithContentsOfFile:filePath options:NSUncachedRead error:NULL]];
[body appendData: [fileInfo objectForKey:@"data"]];
i++;
// Only add the boundary if this is not the last item in the post body
if (i != [fileData count]) {
... ...
... ... @@ -15,6 +15,7 @@
- (void)testPostWithFileUpload
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"];
//Create a 32kb file
unsigned int size = 1024*32;
... ... @@ -22,14 +23,22 @@
NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"bigfile"];
[data writeToFile:path atomically:NO];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/asi-http-request/tests/post"]] autorelease];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"foo" forKey:@"post_var"];
[request setFile:path forKey:@"file"];
[request start];
BOOL success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",@"bigfile",size]]);
STAssertTrue(success,@"Failed to upload the correct data");
STAssertTrue(success,@"Failed to upload the correct data (using local file)");
//Try the same with the raw data
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"foo" forKey:@"post_var"];
[request setData:data forKey:@"file"];
[request start];
success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",@"file",size]]);
STAssertTrue(success,@"Failed to upload the correct data (using NSData)");
}
... ...
... ... @@ -5,7 +5,7 @@
// Copyright 2007-2008 All-Seeing Interactive. All rights reserved.
//
// A guide to the main features is available at:
// http://allseeing-i.com/asi-http-request
// http://allseeing-i.com/ASIHTTPRequest
//
// Portions are based on the ImageClient example from Apple:
// See: http://developer.apple.com/samplecode/ImageClient/listing37.html
... ...
... ... @@ -5,7 +5,7 @@
// Copyright 2007-2008 All-Seeing Interactive. All rights reserved.
//
// A guide to the main features is available at:
// http://allseeing-i.com/asi-http-request
// http://allseeing-i.com/ASIHTTPRequest
//
// Portions are based on the ImageClient example from Apple:
// See: http://developer.apple.com/samplecode/ImageClient/listing37.html
... ... @@ -132,6 +132,7 @@ static NSError *ASIUnableToCreateRequestError;
{
postBody = [body retain];
postLength = [postBody length];
[self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",postLength]];
if (postBody && postLength > 0 && ![requestMethod isEqualToString:@"POST"] && ![requestMethod isEqualToString:@"PUT"]) {
[self setRequestMethod:@"POST"];
}
... ... @@ -329,7 +330,7 @@ static NSError *ASIUnableToCreateRequestError;
[pool release];
pool = [[NSAutoreleasePool alloc] init];
NSDate *now = [NSDate new];
NSDate *now = [NSDate date];
// See if we need to timeout
if (lastActivityTime && timeOutSeconds > 0) {
... ... @@ -350,7 +351,6 @@ static NSError *ASIUnableToCreateRequestError;
// This thread should wait for 1/4 second for the stream to do something. We'll stop early if it does.
CFRunLoopRunInMode(ASIHTTPRequestRunMode,0.25,YES);
[now release];
}
[pool release];
... ...
... ... @@ -16,7 +16,8 @@
- (void)testBasicDownload;
- (void)testTimeOut;
- (void)testRequestMethod;
- (void)testContentLength;
- (void)testUploadContentLength;
- (void)testDownloadContentLength;
- (void)testFileDownload;
- (void)testDownloadProgress;
- (void)testUploadProgress;
... ...
... ... @@ -70,7 +70,7 @@
- (void)testRequestMethod
{
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASI-HTTP-Request/tests/request-method"] autorelease];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/request-method"] autorelease];
NSArray *methods = [[[NSArray alloc] initWithObjects:@"GET",@"POST",@"PUT",@"DELETE"] autorelease];
for (NSString *method in methods) {
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
... ... @@ -81,7 +81,19 @@
}
}
- (void)testContentLength
- (void)testUploadContentLength
{
//This url will return the contents of the Content-Length request header
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-length"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setPostBody:[NSMutableData dataWithLength:1024*32]];
[request start];
BOOL success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"%hu",(1024*32)]]);
STAssertTrue(success,@"Sent wrong content length");
}
- (void)testDownloadContentLength
{
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/i/logo.png"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
... ... @@ -95,7 +107,7 @@
{
NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile"];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/first"] autorelease];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/first"] autorelease];
ASIHTTPRequest *request1 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request1 setDownloadDestinationPath:path];
[request1 start];
... ... @@ -121,7 +133,7 @@
- (void)testUploadProgress
{
progress = 0;
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://asi/ignore"]] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ignore"]] autorelease];
[request setPostBody:[NSMutableData dataWithLength:1024*32]];
[request setUploadProgressDelegate:self];
[request start];
... ... @@ -143,7 +155,7 @@
BOOL success;
// Set setting a cookie
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/set_cookie"] autorelease];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/set_cookie"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseCookiePersistance:YES];
[request start];
... ... @@ -166,7 +178,7 @@
STAssertTrue(success,@"Failed to store the correct value for a cookie");
success = [[cookie domain] isEqualToString:@"allseeing-i.com"];
STAssertTrue(success,@"Failed to store the correct domain for a cookie");
success = [[cookie path] isEqualToString:@"/asi-http-request/tests"];
success = [[cookie path] isEqualToString:@"/ASIHTTPRequest/tests"];
STAssertTrue(success,@"Failed to store the correct path for a cookie");
break;
}
... ... @@ -178,7 +190,7 @@
}
// Test a cookie is presented when manually added to the request
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/read_cookie"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseCookiePersistance:NO];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
... ... @@ -188,7 +200,7 @@
STAssertTrue(success,@"Cookie not presented to the server with cookie persistance OFF");
// Test a cookie is presented from the persistent store
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/read_cookie"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseCookiePersistance:YES];
[request start];
... ... @@ -197,7 +209,7 @@
STAssertTrue(success,@"Cookie not presented to the server with cookie persistance ON");
// Test removing a cookie
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/remove_cookie"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/remove_cookie"] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request start];
html = [request dataString];
... ... @@ -205,7 +217,7 @@
STAssertTrue(success,@"Failed to remove a cookie");
// Test making sure cookie was properly removed
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/read_cookie"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request start];
html = [request dataString];
... ... @@ -218,10 +230,10 @@
[cookieProperties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
[cookieProperties setValue:@"allseeing-i.com" forKey:NSHTTPCookieDomain];
[cookieProperties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60*4] forKey:NSHTTPCookieExpires];
[cookieProperties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];
[cookieProperties setValue:@"/ASIHTTPRequest/tests" forKey:NSHTTPCookiePath];
cookie = [[[NSHTTPCookie alloc] initWithProperties:cookieProperties] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/read_cookie"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseCookiePersistance:NO];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
... ... @@ -233,7 +245,7 @@
// Test removing all cookies works
[ASIHTTPRequest clearSession];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/read_cookie"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request start];
html = [request dataString];
... ... @@ -245,7 +257,7 @@
- (void)testBasicAuthentication
{
NSURL *url = [[[NSURL alloc] initWithString:@"http://asi/asi-http-request/tests/basic-authentication"] autorelease];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/basic-authentication"] autorelease];
ASIHTTPRequest *request;
BOOL success;
NSError *err;
... ... @@ -310,7 +322,7 @@
{
[ASIHTTPRequest clearSession];
NSURL *url = [[[NSURL alloc] initWithString:@"http://asi/asi-http-request/tests/digest-authentication"] autorelease];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/digest-authentication"] autorelease];
ASIHTTPRequest *request;
BOOL success;
NSError *err;
... ...
... ... @@ -95,15 +95,15 @@
[networkQueue setShouldCancelAllRequestsOnFailure:NO];
NSURL *url;
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/first"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/first"] autorelease];
ASIHTTPRequest *request1 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request1];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/second"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/second"] autorelease];
ASIHTTPRequest *request2 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request2];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/third"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/third"] autorelease];
ASIHTTPRequest *request3 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request3];
... ... @@ -111,7 +111,7 @@
requestThatShouldFail = [[ASIHTTPRequest alloc] initWithURL:url];
[networkQueue addOperation:requestThatShouldFail];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/broken"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/broken"] autorelease];
ASIHTTPRequest *request5 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request5];
... ... @@ -165,15 +165,15 @@
[networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
NSURL *url;
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/first"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/first"] autorelease];
ASIHTTPRequest *request1 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request1];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/second"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/second"] autorelease];
ASIHTTPRequest *request2 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request2];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/third"] autorelease];
url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/third"] autorelease];
ASIHTTPRequest *request3 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[networkQueue addOperation:request3];
... ...
... ... @@ -14,4 +14,4 @@ It provides:
* Based on NSOperation to make queuing requests and background operation easy
* Basic unit tests (more to come!)
Documentation is available "here":http://allseeing-i.com/ASI-HTTP-REQUEST.
\ No newline at end of file
Documentation is available "here":http://allseeing-i.com/ASIHTTPRequest.
\ No newline at end of file
... ...