Skip to content

Commit

Permalink
Merge pull request #43 from Eli-Liebman/el/patch
Browse files Browse the repository at this point in the history
Fix dropped non-string parameters types
  • Loading branch information
stury authored Oct 20, 2022
2 parents e328e19 + 045bd14 commit 2bb9947
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
75 changes: 75 additions & 0 deletions Example/Tests/Compat.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ + (NSURLRequest *)makeGetRequest
tokenSecret:@"mnop"];
}

+ (NSURLRequest *)makeGetRequestWithValidAdditionalParameters
{
return [TDOAuth URLRequestForPath:@"/service"
GETParameters:@{@"foo": @"bar",
@"fizz" : @123,
@"buzz" : @YES}
host:@"api.example.com"
consumerKey:@"abcd"
consumerSecret:@"efgh"
accessToken:@"ijkl"
tokenSecret:@"mnop"];
}

+ (NSURLRequest *)makeGetRequestWithInvalidAdditionalParameters
{
return [TDOAuth URLRequestForPath:@"/service"
GETParameters:@{@"foo": @"bar",
@"fizz" : @[@1,@2],
@"buzz" : @{@"bar": @"foo"}}
host:@"api.example.com"
consumerKey:@"abcd"
consumerSecret:@"efgh"
accessToken:@"ijkl"
tokenSecret:@"mnop"];
}


+ (NSURLRequest *)makeGetComponentsRequest
{
NSURLComponents *components = [NSURLComponents new];
Expand Down Expand Up @@ -111,7 +138,55 @@ - (void)testGetUrl
NSString *contentLength = [getRequest valueForHTTPHeaderField: @"Content-Length"];
XCTAssertNil(contentLength,
@"Content-Length was set when not expected)");
}

-(void)testGetRequestWithValidAdditionalParameters
{
NSURLRequest *getRequest = [self.class makeGetRequestWithValidAdditionalParameters];
NSString *url = [[getRequest URL] absoluteString];

/// Parameters are not guaranteed to be added to the url in a specific order
/// so direct string comparison is not an option. Breaking the url
/// down to `NSURLComponents` would allow us to sort and test.
NSURLComponents *components = [NSURLComponents componentsWithString:url];

NSArray <NSURLQueryItem *> *sortedArray = [components.queryItems sortedArrayUsingComparator:^NSComparisonResult(NSURLQueryItem * _Nonnull obj1, NSURLQueryItem * _Nonnull obj2) {
return [obj1.name compare:obj2.name];
}];
XCTAssertEqual(sortedArray.count, 3);

XCTAssertEqualObjects(sortedArray[0].name, @"buzz");
XCTAssertEqualObjects(sortedArray[0].value, @"1");

XCTAssertEqualObjects(sortedArray[1].name, @"fizz");
XCTAssertEqualObjects(sortedArray[1].value, @"123");

XCTAssertEqualObjects(sortedArray[2].name, @"foo");
XCTAssertEqualObjects(sortedArray[2].value, @"bar");

NSString *contentType = [getRequest valueForHTTPHeaderField: @"Content-Type"];
XCTAssertNil(contentType,
@"Content-Type was present when not expected)");

NSString *contentLength = [getRequest valueForHTTPHeaderField: @"Content-Length"];
XCTAssertNil(contentLength,
@"Content-Length was set when not expected)");
}

-(void)testGetRequestWithInvalidAdditionalParameters
{
NSURLRequest *getRequest = [self.class makeGetRequestWithInvalidAdditionalParameters];
NSString *url = [[getRequest URL] absoluteString];
XCTAssert([url isEqualToString:@"http://api.example.com/service?foo=bar"],
"url does not match expected value");

NSString *contentType = [getRequest valueForHTTPHeaderField: @"Content-Type"];
XCTAssertNil(contentType,
@"Content-Type was present when not expected)");

NSString *contentLength = [getRequest valueForHTTPHeaderField: @"Content-Length"];
XCTAssertNil(contentLength,
@"Content-Length was set when not expected)");
}

#ifdef TEST_NSURLCOMPONENTS
Expand Down
20 changes: 15 additions & 5 deletions Source/compat/TDOAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,22 @@ internal class TDOQueryItem : NSObject {
var queryItems = [TDOQueryItem]()

if let unencodedParameters = unencodedParameters {
for key in unencodedParameters.keys {
if let key = key as? String,
let value = unencodedParameters[key] as? String {
let queryItem = TDOQueryItem(name: key, value: value)
queryItems.append(queryItem)
for (key, value) in unencodedParameters {
guard let key = key as? String else { continue }
let formattedValue: String
switch value {
case let stringValue as String:
formattedValue = stringValue
case let intValue as Int:
formattedValue = String(intValue)
case let boolValue as Bool:
formattedValue = String(boolValue)
default:
/// `value` is not a valid type - skipping
continue
}
let queryItem = TDOQueryItem(name: key, value: formattedValue)
queryItems.append(queryItem)
}
}

Expand Down
2 changes: 1 addition & 1 deletion TDOAuth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'TDOAuth'
s.version = '1.6.0'
s.version = '1.6.1'
s.summary = 'Elegant, simple and compliant OAuth 1.x solution.'

s.description = <<-DESC
Expand Down

0 comments on commit 2bb9947

Please sign in to comment.