浏览代码

Exposing `AFPercentEscapedStringFromString` for #3227

Kevin Harwood 9 年之前
父节点
当前提交
9be1743c53

+ 16 - 0
AFNetworking/AFURLRequestSerialization.h

@@ -30,6 +30,22 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+/**
+ Returns a percent-escaped string following RFC 3986 for a query string key or value.
+ RFC 3986 states that the following characters are "reserved" characters.
+ - General Delimiters: ":", "#", "[", "]", "@", "?", "/"
+ - Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
+
+ In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
+ query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
+ should be percent-escaped in the query string.
+ 
+ @param string The string to be percent-escaped.
+ 
+ @return The percent-escaped string.
+ */
+FOUNDATION_EXPORT NSString * AFPercentEscapedStringFromString(NSString *string);
+
 /**
  A helper method to generate encoded url query parameters for appending to the end of a URL.
 

+ 1 - 1
AFNetworking/AFURLRequestSerialization.m

@@ -44,7 +44,7 @@ typedef NSString * (^AFQueryStringSerializationBlock)(NSURLRequest *request, id
     - parameter string: The string to be percent-escaped.
     - returns: The percent-escaped string.
  */
-static NSString * AFPercentEscapedStringFromString(NSString *string) {
+NSString * AFPercentEscapedStringFromString(NSString *string) {
     static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
     static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
 

+ 4 - 0
Tests/Tests/AFHTTPRequestSerializationTests.m

@@ -189,6 +189,10 @@
     XCTAssertTrue([AFQueryStringFromParameters(@{@"key":@"value",@"key1":@"value&"}) isEqualToString:@"key=value&key1=value%26"]);
 }
 
+- (void)testPercentEscapingString {
+    XCTAssertTrue([AFPercentEscapedStringFromString(@":#[]@!$&'()*+,;=?/") isEqualToString:@"%3A%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D?/"]);
+}
+
 #pragma mark - #3028 tests
 //https://github.com/AFNetworking/AFNetworking/pull/3028