Tags: hathway/JSONRequest
Tags
(HWAYNN-1106)|Fix encoding of URL query parameters (#17) Summary -- Apple's implementation of query parameters in `URLComponents` does not encode all characters that need to be escaped according to RFC 3986 (https://www.ietf.org/rfc/rfc3986.txt, page 12) The popular Alamofire networking library handles this problem by manually encoding query parameters. Because this library is battle-tested, I copied its URL encoding code, modifying it only slightly. (It's `encode` function accepts a URLRequest as input and returns a modified URLRequest. Our implementation accepts and returns an updated `URL`.) An additional win is that Alamofire's implementation properly encodes arrays and dictionaries into URL query strings. Tests -- **Adds two tests:** - Encoding basic URL parameters (aka "hello=world") - Encoding URL parameters that need escaping (&'()*+,;=:#[]@) **Removes one test:** There was a test asserting that creating a `URL` from an empty string would return a non-nil `URL`, with an absolute string value of `""`. This is NOT the behavior when using the `URL(string:)` initializer, but IS the behavior when initializing through `URLComponents`, which yields: `URLComponents(string "")!.url == ""`. This behavior is certainly unexpected, and it's really strange the original author (Eneko) chose to test for this behavior, especially when the tested method returns an optional `URL`: `func createURL(urlString:, queryParams:) -> URL?`. This might be a (odd) breaking change for client code, but because the intention is to tag this version 1.0, not compatible with previous versions, this feels appropriate.