Rework de/encoding in _sanitizePath
method to be more careful
#25
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is intended to fix the two issues brought up in #24, both of which involve encoding errors brought about by ham-fistedly decoding all paths fed in to the
_sanitizePath()
private method before attempting to re-encode things properly based on whether we determined the path was a fully qualified URL or just a path fragment.Instead of this clumsy approach, we're now evaluating the given path up front to determine which of the following categories it falls into. Each category is then handled slightly differently:
/^https?:\/\//
, we assume its a fully-qualified, unencoded URL. All we need to do here is encode it as though it was a URI component (since it will be just a component, in the final imgix URL)./^https?:%3A%2F%2F/
, we assume its a fully-qualified, pre-encoded URL. In this case, we decode and re-encode it usingdecodeURIComponent
andencodeURIComponent
, to ensure that all characters in the string are actually encoded properly.decodeURI
andencodeURI
so that legal path characters such as/
and@
aren't affected.This PR also reorganizes the library's tests so they're focused on the private component methods that make up the public
buildURL()
method:_sanitizePath()
,_buildParams()
, and_signParams()
. Testing these component functions separately allows us to untangle the various cases that need to be tested and gives us better confidence that all cases are being covered.@mchatman Do you mind giving this a quick review?