Don't encode twice (#13138)
This commit is contained in:
parent
b1dd8999cb
commit
3d48a8f7f3
|
@ -17,6 +17,10 @@ export function breakQueryString(qs) {
|
|||
return paramObj
|
||||
}
|
||||
|
||||
function isEncoded(str) {
|
||||
return typeof str == "string" && decodeURIComponent(str) !== str
|
||||
}
|
||||
|
||||
export function buildQueryString(obj) {
|
||||
let str = ""
|
||||
if (obj) {
|
||||
|
@ -35,7 +39,7 @@ export function buildQueryString(obj) {
|
|||
value = value.replace(binding, marker)
|
||||
bindingMarkers[marker] = binding
|
||||
})
|
||||
let encoded = encodeURIComponent(value || "")
|
||||
let encoded = isEncoded(value) ? value : encodeURIComponent(value || "")
|
||||
Object.entries(bindingMarkers).forEach(([marker, binding]) => {
|
||||
encoded = encoded.replace(marker, binding)
|
||||
})
|
||||
|
|
|
@ -39,4 +39,11 @@ describe("check query string utils", () => {
|
|||
expect(broken.key1).toBe(obj2.key1)
|
||||
expect(broken.key2).toBe(obj2.key2)
|
||||
})
|
||||
|
||||
it("should not encode a URL more than once when building the query string", () => {
|
||||
const queryString = buildQueryString({
|
||||
values: "a%2Cb%2Cc",
|
||||
})
|
||||
expect(queryString).toBe("values=a%2Cb%2Cc")
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue