Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tweak behaviour of ASIFormDataRequests various set/add value methods:

* If key is empty, don't add anything (value can be empty)
* All file/data methods now do through the long form addData:withFileName:andContentType:forKey: method, to cut down on a bit of duplication
Thanks to Bas Scheffers for suggesting changes here!
  • Loading branch information...
commit 1fe1027d681db1ddf2a3e3b50ab762ac71ad79f3 1 parent 404bbfe
@pokeb authored
View
4 Classes/ASIFormDataRequest.h
@@ -50,13 +50,13 @@ typedef enum _ASIPostFormat {
- (void)addFile:(NSString *)filePath forKey:(NSString *)key;
// Same as above, but you can specify the content-type and file name
-- (void)addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key;
+- (void)addFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key;
// Add the contents of a local file to the request, clearing any others with the same key
- (void)setFile:(NSString *)filePath forKey:(NSString *)key;
// Same as above, but you can specify the content-type and file name
-- (void)setFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key;
+- (void)setFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key;
// Add the contents of an NSData object to the request
- (void)addData:(NSData *)data forKey:(NSString *)key;
View
53 Classes/ASIFormDataRequest.m
@@ -67,10 +67,16 @@ - (void)dealloc
- (void)addPostValue:(id <NSObject>)value forKey:(NSString *)key
{
+ if (!key) {
+ return;
+ }
if (![self postData]) {
[self setPostData:[NSMutableArray array]];
}
- [[self postData] addObject:[NSDictionary dictionaryWithObjectsAndKeys:[value description],@"value",key,@"key",nil]];
+ NSMutableDictionary *keyValuePair = [NSMutableDictionary dictionaryWithCapacity:2];
+ [keyValuePair setValue:key forKey:@"key"];
+ [keyValuePair setValue:[value description] forKey:@"value"];
+ [[self postData] addObject:keyValuePair];
}
- (void)setPostValue:(id <NSObject>)value forKey:(NSString *)key
@@ -93,36 +99,26 @@ - (void)addFile:(NSString *)filePath forKey:(NSString *)key
[self addFile:filePath withFileName:nil andContentType:nil forKey:key];
}
-- (void)addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key
+- (void)addFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key
{
- if (![self fileData]) {
- [self setFileData:[NSMutableArray array]];
+ BOOL isDirectory = NO;
+ BOOL fileExists = [[[[NSFileManager alloc] init] autorelease] fileExistsAtPath:filePath isDirectory:&isDirectory];
+ if (!fileExists || isDirectory) {
+ [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"No file exists at %@",filePath],NSLocalizedDescriptionKey,nil]]];
}
-
- // If data is a path to a local file
- if ([data isKindOfClass:[NSString class]]) {
- BOOL isDirectory = NO;
- BOOL fileExists = [[[[NSFileManager alloc] init] autorelease] fileExistsAtPath:(NSString *)data isDirectory:&isDirectory];
- if (!fileExists || isDirectory) {
- [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"No file exists at %@",data],NSLocalizedDescriptionKey,nil]]];
- }
- // If the caller didn't specify a custom file name, we'll use the file name of the file we were passed
- if (!fileName) {
- fileName = [(NSString *)data lastPathComponent];
- }
+ // If the caller didn't specify a custom file name, we'll use the file name of the file we were passed
+ if (!fileName) {
+ fileName = [filePath lastPathComponent];
+ }
- // If we were given the path to a file, and the user didn't specify a mime type, we can detect it from the file extension
- if (!contentType) {
- contentType = [ASIHTTPRequest mimeTypeForFileAtPath:data];
- }
+ // If we were given the path to a file, and the user didn't specify a mime type, we can detect it from the file extension
+ if (!contentType) {
+ contentType = [ASIHTTPRequest mimeTypeForFileAtPath:filePath];
}
-
- NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys:data, @"data", contentType, @"contentType", fileName, @"fileName", key, @"key", nil];
- [[self fileData] addObject:fileInfo];
+ [self addData:filePath withFileName:fileName andContentType:contentType forKey:key];
}
-
- (void)setFile:(NSString *)filePath forKey:(NSString *)key
{
[self setFile:filePath withFileName:nil andContentType:nil forKey:key];
@@ -155,8 +151,13 @@ - (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSStr
if (!contentType) {
contentType = @"application/octet-stream";
}
-
- NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys:data, @"data", contentType, @"contentType", fileName, @"fileName", key, @"key", nil];
+
+ NSMutableDictionary *fileInfo = [NSMutableDictionary dictionaryWithCapacity:4];
+ [fileInfo setValue:key forKey:@"key"];
+ [fileInfo setValue:fileName forKey:@"fileName"];
+ [fileInfo setValue:contentType forKey:@"contentType"];
+ [fileInfo setValue:data forKey:@"data"];
+
[[self fileData] addObject:fileInfo];
}
View
2  Classes/ASIHTTPRequest.m
@@ -24,7 +24,7 @@
#import "ASIDataCompressor.h"
// Automatically set on build
-NSString *ASIHTTPRequestVersion = @"v1.8-95 2011-05-28";
+NSString *ASIHTTPRequestVersion = @"v1.8.1 2011-06-04";
static NSString *defaultUserAgent = nil;
View
21 Classes/Tests/ASIFormDataRequestTests.m
@@ -17,6 +17,27 @@ @implementation ASIFormDataRequestSubclass;
@implementation ASIFormDataRequestTests
+- (void)testAddNilKeysAndValues
+{
+ ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/empty-post-value"]];
+ [request setPostValue:nil forKey:@"key1"];
+ [request setPostValue:@"value2" forKey:@"key2"];
+ [request setData:nil forKey:@"file1"];
+ [request setData:[@"hello" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"file2"];
+ [request startSynchronous];
+ BOOL success = ([[request responseString] isEqualToString:@"key1: \r\nkey2: value2\r\nfile1: \r\nfile2: hello"]);
+ GHAssertTrue(success, @"Sent wrong data");
+
+ // Test nil key (no key or value should be sent to the server)
+ request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
+ [request addPostValue:@"value1" forKey:nil];
+ [request addPostValue:@"value2" forKey:@"key2"];
+ [request buildPostBody];
+ NSString *postBody = [[[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding] autorelease];
+ success = ([postBody isEqualToString:@"key2=value2"]);
+ GHAssertTrue(success, @"Sent wrong data");
+}
+
- (void)testPostWithFileUpload
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"];
View
5 Mac.xcodeproj/project.pbxproj
@@ -422,7 +422,7 @@
isa = PBXNativeTarget;
buildConfigurationList = B55B5EE40F7658C80064029C /* Build configuration list for PBXNativeTarget "Unit Tests (GHUnit)" */;
buildPhases = (
- B52D490910DA4009008E8365 /* ShellScript */,
+ B52D490910DA4009008E8365 /* Run Script */,
B55B5EDB0F7658C70064029C /* Resources */,
B55B5EDC0F7658C70064029C /* Sources */,
B55B5EDD0F7658C70064029C /* Frameworks */,
@@ -494,13 +494,14 @@
shellPath = /bin/sh;
shellScript = "./Build\\ Scripts/set_version_number.rb";
};
- B52D490910DA4009008E8365 /* ShellScript */ = {
+ B52D490910DA4009008E8365 /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
+ name = "Run Script";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
Please sign in to comment.
Something went wrong with that request. Please try again.