-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue-89 - Add support for arrays in query parameters #98
Changes from 4 commits
7cebc87
3b6c0e8
3899a9e
b8b6ab5
3fd49d9
01ef2e1
b7bd8f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#import "NSString+DPLQuery.h" | ||
|
||
@interface NSString (DPLQuery_Private) | ||
|
||
|
||
|
||
///--------------------- | ||
/// @name Array Literals | ||
///--------------------- | ||
|
||
|
||
/** | ||
Checks if string ends with array literal ('[]') | ||
@return YES if string ends with '[]', NO otherwise | ||
*/ | ||
- (BOOL)DPL_containsArrayLiteral; | ||
|
||
|
||
/** | ||
Removes array literal ('[]') from the string | ||
@return String without array literal ('[]') | ||
*/ | ||
- (NSString *)DPL_stringByRemovingArrayLiteral; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
|
||
public class DPLArrayRouteHandler: DPLRouteHandler { | ||
public override func shouldHandleDeepLink(deepLink: DPLDeepLink!) -> Bool { | ||
if let list = deepLink["list"] as? Array<String> { | ||
let alertController = UIAlertController(title: NSLocalizedString("Shopping List", comment: ""), | ||
message: NSLocalizedString("Please buy the following:", comment: ""), | ||
preferredStyle: .ActionSheet) | ||
for title in list { | ||
alertController.addAction(UIAlertAction(title: title, style: .Default, handler: nil)) | ||
} | ||
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) | ||
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) | ||
} | ||
return false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#import "NSString+DPLQuery.h" | ||
#import "NSString+DPLQuery_Private.h" | ||
|
||
SpecBegin(NSString_DPLQuery) | ||
|
||
|
@@ -18,6 +19,7 @@ | |
NSString *encodedString = [plainTestString DPL_stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | ||
expect(encodedString).to.equal(plainTestString); | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
@@ -32,6 +34,7 @@ | |
NSString *decodedString = [plainTestString DPL_stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | ||
expect(decodedString).to.equal(plainTestString); | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
@@ -54,6 +57,40 @@ | |
NSString *query = [NSString DPL_queryStringWithParameters:params]; | ||
expect(query).to.equal(@"one=a%20one&two=http%3A%2F%2Fwww.example.com%3Ffoo%3Dbar"); | ||
}); | ||
|
||
it(@"should serialize array from dictionary into the query string", ^{ | ||
NSDictionary *params = @{ @"beers": @[ @"stout", @"ale" ] }; | ||
NSString *query = [NSString DPL_queryStringWithParameters:params]; | ||
expect(query).to.equal(@"beers[]=stout&beers[]=ale"); | ||
}); | ||
|
||
it(@"should serialize empty array from dictionary into the query string", ^{ | ||
NSDictionary *params = @{ @"beers": @[ ] }; | ||
NSString *query = [NSString DPL_queryStringWithParameters:params]; | ||
expect(query).to.equal(@"beers[]"); | ||
}); | ||
|
||
it(@"should serialize multiple arrays from dictionary into the query string", ^{ | ||
NSDictionary *params = @{ @"beers": @[ @"stout", @"ale" ], @"liquors": @[ @"vodka", @"whiskey" ] }; | ||
NSString *query = [NSString DPL_queryStringWithParameters:params]; | ||
|
||
NSString *queryStringToMatch; | ||
NSString *queryStringPartBeers = @"beers[]=stout&beers[]=ale"; | ||
NSString *queryStringPartLiquors = @"liquors[]=vodka&liquors[]=whiskey"; | ||
if ([[params allKeys][0] isEqualToString:@"beers"]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should probably have a suitable default here such as ordered ascending that way we can expect a consistent result. Similarly, when parsing a url, I would expect to have the parameter order preserved so re-serialization mirrors the original url though that can't really happen in the category (#15) without more information. For example: NSURL *url = [NSURL URLWithString:@"dpl://something/?liquors[]=vodka&liquors[]=whiskey&beers[]=stout&beers[]=ale"];
DPLDeepLink *link = [[DPLDeepLink alloc] initWithURL:url];
link.URL.query // should serialize as => @"liquors[]=vodka&liquors[]=whiskey&beers[]=stout&beers[]=ale" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wessmith Just making sure I understand that correctly: you are talking here about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Yeah. |
||
queryStringToMatch = [NSString stringWithFormat:@"%@&%@", queryStringPartBeers, queryStringPartLiquors]; | ||
} else { | ||
queryStringToMatch = [NSString stringWithFormat:@"%@&%@", queryStringPartLiquors, queryStringPartBeers]; | ||
} | ||
expect(query).to.equal(queryStringToMatch); | ||
}); | ||
|
||
it(@"should percent encode parameters from dictionary into the query array", ^{ | ||
NSDictionary *params = @{ @"one": @"a one", @"two": @[ @"http://www.example.com?foo=bar", @"a two" ] }; | ||
NSString *query = [NSString DPL_queryStringWithParameters:params]; | ||
expect(query).to.equal(@"one=a%20one&two[]=http%3A%2F%2Fwww.example.com%3Ffoo%3Dbar&two[]=a%20two"); | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
@@ -80,6 +117,79 @@ | |
expect(params[@"one"]).to.equal(@"a one"); | ||
expect(params[@"two"]).to.equal(@"http://www.example.com?foo=bar"); | ||
}); | ||
|
||
it(@"should decode array query parameters into an array preserving order", ^{ | ||
NSString *query = @"beers[]=stout&beers[]=ale"; | ||
NSDictionary *params = [query DPL_parametersFromQueryString]; | ||
expect(params[@"beers"]).notTo.beNil; | ||
expect([params[@"beers"] isKindOfClass:[NSArray class]]).to.beTruthy; | ||
expect([params[@"beers"] count]).to.equal(2); | ||
expect(params[@"beers"][0]).to.contain(@"stout"); | ||
expect(params[@"beers"][1]).to.contain(@"ale"); | ||
}); | ||
|
||
it(@"should decode mixed arrays query parameters into appropriate arrays preserving order", ^{ | ||
NSString *query = @"beers[]=stout&liquors[]=vodka&beers[]=ale&liquors[]=whiskey"; | ||
NSDictionary *params = [query DPL_parametersFromQueryString]; | ||
expect(params[@"beers"]).notTo.beNil; | ||
expect(params[@"liquors"]).notTo.beNil; | ||
expect([params[@"beers"] isKindOfClass:[NSArray class]]).to.beTruthy; | ||
expect([params[@"liquors"] isKindOfClass:[NSArray class]]).to.beTruthy; | ||
expect([params[@"beers"] count]).to.equal(2); | ||
expect([params[@"liquors"] count]).to.equal(2); | ||
expect(params[@"beers"][0]).to.contain(@"stout"); | ||
expect(params[@"beers"][1]).to.contain(@"ale"); | ||
expect(params[@"liquors"][0]).to.contain(@"vodka"); | ||
expect(params[@"liquors"][1]).to.contain(@"whiskey"); | ||
}); | ||
|
||
it (@"should decode empty array in case values were not provided", ^{ | ||
NSString *query = @"beers[]"; | ||
NSDictionary *params = [query DPL_parametersFromQueryString]; | ||
expect(params[@"beers"]).notTo.beNil; | ||
expect([params[@"beers"] isKindOfClass:[NSArray class]]).to.beTruthy; | ||
expect([params[@"beers"] count]).to.equal(0); | ||
}); | ||
|
||
}); | ||
|
||
describe(@"Array literals", ^{ | ||
|
||
it (@"should return YES in case there's array literal in the key", ^{ | ||
NSString *key = @"beers[]"; | ||
expect([key DPL_containsArrayLiteral]).to.beTruthy; | ||
}); | ||
|
||
it (@"should return NO in case there's no array literal in the key", ^{ | ||
NSString *key = @"beers"; | ||
expect([key DPL_containsArrayLiteral]).to.beFalsy; | ||
}); | ||
|
||
it (@"should return NO in case there's array literal in the middle of the key", ^{ | ||
NSString *key = @"be[]ers"; | ||
expect([key DPL_containsArrayLiteral]).to.beFalsy; | ||
}); | ||
|
||
it (@"should reply YES in case there's array literal in the key", ^{ | ||
NSString *key = @"beers[]"; | ||
expect([key DPL_containsArrayLiteral]).to.beTruthy; | ||
}); | ||
|
||
it (@"should delete array literal at the end of a string", ^{ | ||
NSString *key = @"beers[]"; | ||
expect([key DPL_stringByRemovingArrayLiteral]).to.equal(@"beers"); | ||
}); | ||
|
||
it (@"should return the same string if there's no array literal", ^{ | ||
NSString *key = @"beers"; | ||
expect([key DPL_stringByRemovingArrayLiteral]).to.equal(@"beers"); | ||
}); | ||
|
||
it (@"should not delete array literal in the middle of a string", ^{ | ||
NSString *key = @"be[]ers"; | ||
expect([key DPL_stringByRemovingArrayLiteral]).to.equal(@"be[]ers"); | ||
}); | ||
|
||
}); | ||
|
||
SpecEnd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if here should be easier way to initialize deep link with URLs of such kind?