diff --git a/OAuthSwiftTests/URLTests.swift b/OAuthSwiftTests/URLTests.swift index 7507729c..39426149 100644 --- a/OAuthSwiftTests/URLTests.swift +++ b/OAuthSwiftTests/URLTests.swift @@ -29,6 +29,20 @@ class URLTest: XCTestCase { } XCTAssertFalse(responseParameters.isEmpty) XCTAssertNotNil(responseParameters["code"]) + XCTAssertEqual(responseParameters["code"], "azeaze") + } + + func testWithQueryMultiple() { + let url = URL(string: "http://localhost/?code=azeaze&code2=zaeaze") + var responseParameters = [String: String]() + if let query = url?.query { + responseParameters += query.parametersFromQueryString + } + XCTAssertFalse(responseParameters.isEmpty) + XCTAssertNotNil(responseParameters["code"]) + XCTAssertEqual(responseParameters["code"], "azeaze") + XCTAssertNotNil(responseParameters["code2"]) + XCTAssertEqual(responseParameters["code2"], "zaeaze") } func testWithQueryWithFlag() { @@ -38,7 +52,9 @@ class URLTest: XCTestCase { responseParameters += query.parametersFromQueryString } XCTAssertFalse(responseParameters.isEmpty) + XCTAssertNotNil(responseParameters["close"]) XCTAssertNotNil(responseParameters["code"]) + XCTAssertEqual(responseParameters["code"], "azeaze") } func testWithQueryWithMultipleFlag() { @@ -48,7 +64,24 @@ class URLTest: XCTestCase { responseParameters += query.parametersFromQueryString } XCTAssertFalse(responseParameters.isEmpty) + XCTAssertNotNil(responseParameters["close"]) XCTAssertNotNil(responseParameters["code"]) + XCTAssertEqual(responseParameters["code"], "azeaze") + XCTAssertNotNil(responseParameters["flag"]) } + func testWithQueryWithMultipleFlagAndValue() { + let url = URL(string: "http://localhost/?close&test&code=azeaze&code2=zaeaze&flag") + var responseParameters = [String: String]() + if let query = url?.query { + responseParameters += query.parametersFromQueryString + } + XCTAssertFalse(responseParameters.isEmpty) + XCTAssertNotNil(responseParameters["close"]) + XCTAssertNotNil(responseParameters["code"]) + XCTAssertEqual(responseParameters["code"], "azeaze") + XCTAssertNotNil(responseParameters["code2"]) + XCTAssertEqual(responseParameters["code2"], "zaeaze") + XCTAssertNotNil(responseParameters["flag"]) + } } diff --git a/Sources/OAuthWebViewController.swift b/Sources/OAuthWebViewController.swift index fd56ac84..e89a6242 100644 --- a/Sources/OAuthWebViewController.swift +++ b/Sources/OAuthWebViewController.swift @@ -8,7 +8,7 @@ import Foundation -#if os(iOS) || os(tvOS) +#if os(iOS) || os(tvOS) import UIKit public typealias OAuthViewController = UIViewController #elseif os(watchOS) diff --git a/Sources/String+OAuthSwift.swift b/Sources/String+OAuthSwift.swift index fc5bf845..48c25a43 100755 --- a/Sources/String+OAuthSwift.swift +++ b/Sources/String+OAuthSwift.swift @@ -53,33 +53,55 @@ extension String { let scanner = Scanner(string: string) - var key: NSString? - var value: NSString? - while !scanner.isAtEnd { - key = nil - scanner.scanUpTo(keyValueSeparator, into: &key) - scanner.scanString(keyValueSeparator, into: nil) - - value = nil - scanner.scanUpTo(elementSeparator, into: &value) - scanner.scanString(elementSeparator, into: nil) - - if let key = key as String? { - if let value = value as String? { - if key.contains(elementSeparator) { - var keys = key.components(separatedBy: elementSeparator) - if let key = keys.popLast() { - parameters.updateValue(value, forKey: String(key)) + if #available(iOS 13.0, OSX 10.15, *) { + let key = scanner.scanUpToString(keyValueSeparator) + _ = scanner.scanString(keyValueSeparator) + + let value = scanner.scanUpToString(elementSeparator) + _ = scanner.scanString(elementSeparator) + + if let key = key { + if let value = value { + if key.contains(elementSeparator) { + var keys = key.components(separatedBy: elementSeparator) + if let key = keys.popLast() { + parameters.updateValue(value, forKey: String(key)) + } + for flag in keys { + parameters.updateValue("", forKey: flag) + } + } else { + parameters.updateValue(value, forKey: key) } - for flag in keys { - parameters.updateValue("", forKey: flag) + } else { + parameters.updateValue("", forKey: key) + } + } + } else { + var key: NSString? + scanner.scanUpTo(keyValueSeparator, into: &key) + scanner.scanString(keyValueSeparator, into: nil) + + var value: NSString? + scanner.scanUpTo(elementSeparator, into: &value) + scanner.scanString(elementSeparator, into: nil) + if let key = key as String? { + if let value = value as String? { + if key.contains(elementSeparator) { + var keys = key.components(separatedBy: elementSeparator) + if let key = keys.popLast() { + parameters.updateValue(value, forKey: String(key)) + } + for flag in keys { + parameters.updateValue("", forKey: flag) + } + } else { + parameters.updateValue(value, forKey: key) } } else { - parameters.updateValue(value, forKey: key) + parameters.updateValue("", forKey: key) } - } else { - parameters.updateValue("", forKey: key) } } }