Skip to content

Commit

Permalink
Merge pull request #118 from seasonedcc/fix-double-decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoguichard authored Nov 1, 2023
2 parents e188b8f + fc2cb44 commit 1bce41a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/input-resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const makeGet: (entries: Array<[string, string]>, url?: string) => Request = (
})

describe('inputFromForm', () => {
it('should parse all symbols correctly', async () => {
const request = makePost([
['formula', '3 % 2']
])
assertEquals(await subject.inputFromForm(request), {
formula: '3 % 2'
})
})

it("extracts the input values from a Request's FormData as an Object", async () => {
const request = makePost([
['foo', 'bar'],
Expand Down Expand Up @@ -246,8 +255,8 @@ describe('inputFromSearch', () => {

it('takes keys encoded as URI components', () => {
const qs = new URLSearchParams()
qs.append('some%20colors[0]', 'blue')
qs.append('some%20colors[1]', 'red%20ish')
qs.append('some colors[0]', 'blue')
qs.append('some colors[1]', 'red ish')
assertEquals(subject.inputFromSearch(qs), {
'some colors': ['blue', 'red ish'],
})
Expand All @@ -256,8 +265,8 @@ describe('inputFromSearch', () => {
it('takes values encoded as URI components', () => {
const qs = new URLSearchParams()
qs.append('colors[0]', 'blue')
qs.append('colors[1]', 'red%20ish')
qs.append('person[name]', 'Average%20Joe')
qs.append('colors[1]', 'red ish')
qs.append('person[name]', 'Average Joe')
assertEquals(subject.inputFromSearch(qs), {
colors: ['blue', 'red ish'],
person: { name: 'Average Joe' },
Expand Down
4 changes: 1 addition & 3 deletions src/input-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const inputFromSearch = (queryString: URLSearchParams) => {

return pairs
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
.reduce((parsed, [encodedKey, encodedValue]) => {
const key = decodeURIComponent(encodedKey)
const value = decodeURIComponent(encodedValue)
.reduce((parsed, [key, value]) => {
const compositeKey = key.match(/([^\[\]]*)(\[.*\].*)$/)
if (compositeKey) {
const [, rootKey, subKeys] = compositeKey
Expand Down

0 comments on commit 1bce41a

Please sign in to comment.