Skip to content

Commit

Permalink
We had a double decoding in our input resolvers. Our values are decod…
Browse files Browse the repository at this point in the history
…ed by URLSearchParams.
  • Loading branch information
diogob committed Nov 1, 2023
1 parent e188b8f commit 9e4c02b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 12 additions & 3 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 @@ -247,7 +256,7 @@ 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%20colors[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
3 changes: 1 addition & 2 deletions src/input-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ const inputFromSearch = (queryString: URLSearchParams) => {

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

0 comments on commit 9e4c02b

Please sign in to comment.