From ba94ed7db4d89ad0d0e33d6e139a73ba21c6dca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 23 Dec 2015 23:00:25 +0100 Subject: [PATCH] Add percent encoding --- Source/Dictionary+FormURLEncoded.swift | 4 +++- Tests/Dictionary+FormURLEncodedTests.swift | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Source/Dictionary+FormURLEncoded.swift b/Source/Dictionary+FormURLEncoded.swift index 60da2db..ed771ce 100644 --- a/Source/Dictionary+FormURLEncoded.swift +++ b/Source/Dictionary+FormURLEncoded.swift @@ -1,3 +1,5 @@ +import Foundation + public extension Dictionary where Key: StringLiteralConvertible { func formURLEncodedFormat() -> String { var converted = "" @@ -8,6 +10,6 @@ public extension Dictionary where Key: StringLiteralConvertible { converted.appendContentsOf("\(entry.0)=\(entry.1)") } - return converted + return converted.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! } } \ No newline at end of file diff --git a/Tests/Dictionary+FormURLEncodedTests.swift b/Tests/Dictionary+FormURLEncodedTests.swift index 7b8bc68..a0e862d 100644 --- a/Tests/Dictionary+FormURLEncodedTests.swift +++ b/Tests/Dictionary+FormURLEncodedTests.swift @@ -16,9 +16,9 @@ class Dictionary_FormURLEncodedTests: XCTestCase { } func testFormattingOneParameter() { - let parameters = ["password" : "secret"] + let parameters = ["name" : "Elvis Nuñez"] let formatted = parameters.formURLEncodedFormat() - XCTAssertEqual(formatted, "password=secret") + XCTAssertEqual(formatted, "name=Elvis%20Nu%C3%B1ez") } func testFormattingWithEmpty() {