Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Query parameter url encoding #126

Closed
wants to merge 3 commits into from

Conversation

surpher
Copy link
Contributor

@surpher surpher commented Aug 26, 2021

When query parameter is provided as a String or Dictionary of [String: String] it replaces occurrences of space characters with %20. Added a warning to the documentation comment about it and when providing a Matcher for a query parameter, the same character occurrence replacement does not occur.

Because the method signature takes query = Any? it would not be wise to forcefully percentage encode all values. For example, if an existing test is providing a valid query parameter (eg: lives=on%20land) already, then the result would be the % sign would be encoded into %25 and would not represent the intended url. The same percentage encoding is applied in ruby-mock-service if %20 value is provided, the result in contract ends up being
%2520.

Changes in this PR update the Interaction.withRequest(method: path: query: headers: body) method and adds/updates tests to validate the change.

Test:

.withRequest(
  method: .GET, 
  path: "/animals", 
  query: ["live": "in%20bayou swamp"]
)

The result of the test in the .json file:

"request": {
    "method": "get",
    "path": "/animals",
    "query": "live=in%20bayou%20swamp"
},

Resolves #125

@codecov-commenter
Copy link

codecov-commenter commented Aug 26, 2021

Codecov Report

Merging #126 (c06ad2a) into master (7aba867) will increase coverage by 0.25%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #126      +/-   ##
==========================================
+ Coverage   91.80%   92.06%   +0.25%     
==========================================
  Files          13       13              
  Lines         866      995     +129     
==========================================
+ Hits          795      916     +121     
- Misses         71       79       +8     

@surpher surpher force-pushed the fix/query-param-urlencoding branch 2 times, most recently from c6eb089 to 933c15c Compare August 26, 2021 04:09
@surpher surpher force-pushed the fix/query-param-urlencoding branch 2 times, most recently from e80f1aa to 3b33dec Compare August 26, 2021 23:39
@surpher surpher force-pushed the fix/query-param-urlencoding branch from 3b33dec to 5745d9c Compare August 27, 2021 00:22
@bethesque
Copy link

The plus symbol is completely valid and there may be existing users that rely on it, so I'm hesitant about making a non-configurable change like this. Is there a way to allow this to be optionally turned on?

@surpher
Copy link
Contributor Author

surpher commented Sep 7, 2021

This only converts the space character into percent encoded. Nothing else. If user sets + as the expected character, sends +, it is then written as a + in the contract. This only really avoids the case where one declares a space in the expectation, sends a space but ends up with a + in the contract.

But like I wrote in a few comments now already, I'm hesitant to allow this to be merged for various reasons.

Since query param takes Any? Type a specific case for handling an object with query values and a flag whether to encode spaces or not is an option. Could've even sniffed the headers for x-www-form-urlencoded keyword and convert to + but... I don't wanna 😬. Something like now when it handles a Matcher, else it just passes it on as it is. But this framework is slowly sunsetting so we're really not fussed if it doesn't get a fix or an update 🤷‍♂️.

@surpher surpher closed this Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with URL Encoding of space character in query parameter
4 participants