Skip to content

Commit

Permalink
Fix deprecation warning on scanner
Browse files Browse the repository at this point in the history
Add more test on query parsing
  • Loading branch information
phimage committed Nov 1, 2019
1 parent 20af790 commit edce6e2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
33 changes: 33 additions & 0 deletions OAuthSwiftTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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"])
}
}
2 changes: 1 addition & 1 deletion Sources/OAuthWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 44 additions & 22 deletions Sources/String+OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit edce6e2

Please sign in to comment.