Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Added override to add already URL encoded key/value params
Browse files Browse the repository at this point in the history
  • Loading branch information
ivasic committed Sep 29, 2011
1 parent 0d634e7 commit 4aed596
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
6 changes: 6 additions & 0 deletions RESTframework/RESTframework/RFClasses/RFRequest.h
Expand Up @@ -112,6 +112,12 @@ typedef enum {
*/
-(void) addParam:(NSString*)value forKey:(NSString*)key;

/*!
* @method addParam:forKey:alreadyEncoded:
* @abstract Adds string param to request. If both value AND key are URL encoded pass YES for alreadyEncoded.
*/
-(void) addParam:(NSString*)value forKey:(NSString*)key alreadyEncoded:(BOOL)encoded;

/*!
* @method addData:withContentType:forKey:
* @abstract Adds data (e.g. file) with specified MIME type for specified key
Expand Down
20 changes: 15 additions & 5 deletions RESTframework/RESTframework/RFClasses/RFRequest.m
Expand Up @@ -24,6 +24,7 @@

#define kRFRequestParamKey @"kRFRequestParamKey"
#define kRFRequestParamValue @"kRFRequestParamValue"
#define kRFRequestParamEncoded @"kRFRequestParamEncoded"
#define kRFRequestParamMIME @"kRFRequestParamMIME"


Expand Down Expand Up @@ -131,12 +132,17 @@ -(BOOL) hasParams {

#pragma mark - Methods

-(void) addParam:(NSString*)value forKey:(NSString*)key {
-(void) addParam:(NSString*)value forKey:(NSString*)key
{
[self addParam:value forKey:key alreadyEncoded:NO];
}

-(void) addParam:(NSString*)value forKey:(NSString*)key alreadyEncoded:(BOOL)encoded {
if (!self.params) {
params = [[NSMutableArray array] retain];
}

NSDictionary* pd = [NSDictionary dictionaryWithObjectsAndKeys:key, kRFRequestParamKey, value, kRFRequestParamValue, nil];
NSDictionary* pd = [NSDictionary dictionaryWithObjectsAndKeys:key, kRFRequestParamKey, value, kRFRequestParamValue, [NSNumber numberWithBool:encoded], kRFRequestParamEncoded, nil];

//add param
[self.params addObject:pd];
Expand Down Expand Up @@ -169,13 +175,17 @@ -(NSURL*) URL
continue;
}

BOOL isEncoded = [[p objectForKey:kRFRequestParamEncoded] boolValue];
NSString* pKey = isEncoded ? [p objectForKey:kRFRequestParamKey] : [[p objectForKey:kRFRequestParamKey] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* pValue = isEncoded ? [p objectForKey:kRFRequestParamValue] : [[p objectForKey:kRFRequestParamValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

if(first) {
str = [str stringByAppendingFormat:@"%@=%@", [[p objectForKey:kRFRequestParamKey] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [[p objectForKey:kRFRequestParamValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
first = NO;
continue;
} else {
str = [str stringByAppendingString:@"&"];
}

str = [str stringByAppendingFormat:@"&%@=%@", [[p objectForKey:kRFRequestParamKey] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [[p objectForKey:kRFRequestParamValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
str = [str stringByAppendingFormat:@"%@=%@", pKey, pValue];
}

urlString = [urlString stringByAppendingString:str];
Expand Down
29 changes: 29 additions & 0 deletions RESTframework/RESTframeworkTests/RESTframeworkTests.m
Expand Up @@ -223,6 +223,35 @@ - (void)testPostRequestBodyType
STAssertEqualObjects(httpData, xmlDataString, @"Data don't match");


}


-(void) testGETWithDateChars
{
RFRequest* r = [[[RFRequest alloc] init] autorelease];
r.serviceEndpoint = [NSURL URLWithString:@"http://dummy.url/path1/"];
r.resourcePath = [NSArray arrayWithObjects:@"sub1", nil];

NSDate* d = [NSDate date];
NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
df.dateStyle = NSDateFormatterShortStyle;

NSString* date = [[df stringFromDate:d] stringByAppendingString:@" 15:22h"];
NSLog(@"%@", date);
NSLog(@"%@", [date stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
[r addParam:date forKey:@"date1 1"];
STAssertEqualObjects([[[r urlRequest] URL] absoluteString], @"http://dummy.url/path1/sub1?date1%201=9/29/11%2015:22h", @"Param not encoded properly?");
NSLog(@"%@", [[r urlRequest] URL]);

//now the same but encoded
r = [[[RFRequest alloc] init] autorelease];
r.serviceEndpoint = [NSURL URLWithString:@"http://dummy.url/path1/"];
r.resourcePath = [NSArray arrayWithObjects:@"sub1", nil];
date = [[df stringFromDate:d] stringByAppendingString:@"%2015:22h"];
[r addParam:date forKey:@"date1%201" alreadyEncoded:YES];
STAssertEqualObjects([[[r urlRequest] URL] absoluteString], @"http://dummy.url/path1/sub1?date1%201=9/29/11%2015:22h", @"Param not encoded properly?");


}

@end

0 comments on commit 4aed596

Please sign in to comment.