-
Notifications
You must be signed in to change notification settings - Fork 42
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(encodeQueryValue): encode the slash character #198
fix(encodeQueryValue): encode the slash character #198
Conversation
Make function `encodeQueryValue` also encode the slash character so that it's consistent with the native URL interface and tools
encodeQueryValue
encode the slash character
Can you please add tests and also (manually) confirm if |
Before this fix
// setting the `new $URL().query` object
import { $URL } from "ufo"
const url = new $URL("https://example.com/")
url.query = { foo: "bar/egg" }
console.log(url.toString()) // "https://example.com/?foo=bar/egg"
console.log(url.href) // "https://example.com/?foo=bar/egg" // assigning a property to the `new $URL().query` object
import { $URL } from "ufo"
const url = new $URL("https://example.com/")
url.query = { foo: "bar/egg" }
console.log(url.toString()) // "https://example.com/?foo=bar/egg"
console.log(url.href) // "https://example.com/?foo=bar/egg" // using the `$URL` constructor(s) and pre-encoding slashes inside query parameter values
import { $URL } from "ufo"
const url = new $URL("https://example.com/?foo=bar%2Fegg")
// slashes get decoded!
console.log(url.toString()) // "https://example.com/?foo=bar/egg"
console.log(url.href) // "https://example.com/?foo=bar/egg" // using the `$URL` constructor(s) and not encoding slashes inside query parameter values
const url = new $URL("https://example.com/?foo=bar/egg")
console.log(url.toString()) // "https://example.com/?foo=bar/egg"
console.log(url.href) // "https://example.com/?foo=bar/egg" After this fix
// setting the `new $URL().query` object
import { $URL } from "ufo"
const url = new $URL("https://example.com/")
url.query = { foo: "bar/egg" }
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg" // assigning a property to the `new $URL().query` object
import { $URL } from "ufo"
const url = new $URL("https://example.com/")
url.query = { foo: "bar/egg" }
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg" // using the `$URL` constructor(s) and pre-encoding slashes inside query parameter values
import { $URL } from "ufo"
const url = new $URL("https://example.com/?foo=bar%2Fegg")
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg" // using the `$URL` constructor(s) and not encoding slashes inside query parameter values
const url = new $URL("https://example.com/?foo=bar/egg")
// see the note below
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg" Native behavior
// using the `new URL().search` Map and `new URLSearchParams().toString()`
const url = new URL("https://example.com/")
url.search = new URLSearchParams({ foo: "bar/egg" }).toString()
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg" // using `new URL().searchParams`
const url = new URL("https://example.com/")
url.searchParams.set("foo", "bar/egg")
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg" // using the `URL` constructor(s) and pre-encoding slashes inside query parameter values
const url = new URL("https://example.com/?foo=bar%2Fegg")
console.log(url.toString()) // "https://example.com/?foo=bar%2Fegg"
console.log(url.href) // "https://example.com/?foo=bar%2Fegg"
// using the `URL` constructor(s) and not encoding slashes inside query parameter values
const url = new URL("https://example.com/?foo=bar/egg")
console.log(url.toString()) // "https://example.com/?foo=bar/egg"
console.log(url.href) // "https://example.com/?foo=bar/egg" |
@pi0 my previous comment contains all results from my manual testing. Those did not seem to me as needing to be given separate unit tests, and you said to test it manually anyway. Also, I will now commit changes to the unit tests for encoding and normalization, which now pass as they expect the new adjusted behavior |
β¦tion to match the new behavior
@pi0 needs attention |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! β€οΈ
π Linked issue
Issue #196
β Type of change
π Description
Make function
encodeQueryValue
also encode the slash character so that it's consistent with the native URL interface and tools. Resolves #196π Checklist