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

No encode, decode in createLocation #465

Merged
merged 6 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/LocationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ export const createLocation = (path, state, key, currentLocation) => {
location.state = state
}

try {
location.pathname = decodeURI(location.pathname)
} catch (e) {
if (e instanceof URIError) {
throw new URIError(
'Pathname "' + location.pathname + '" could not be decoded. ' +
'This is likely caused by an invalid percent-encoding.'
)
} else {
throw e
}
}

if (key)
location.key = key

Expand Down
5 changes: 2 additions & 3 deletions modules/PathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export const parsePath = (path) => {
search = pathname.substr(searchIndex)
pathname = pathname.substr(0, searchIndex)
}

pathname = decodeURI(pathname)

return {
pathname,
Expand All @@ -42,13 +40,14 @@ export const parsePath = (path) => {
export const createPath = (location) => {
const { pathname, search, hash } = location

let path = encodeURI(pathname || '/')
let path = pathname || '/'

if (search && search !== '?')
path += (search.charAt(0) === '?' ? search : `?${search}`)

if (hash && hash !== '#')
path += (hash.charAt(0) === '#' ? hash : `#${hash}`)


return path
}
24 changes: 24 additions & 0 deletions modules/__tests__/BrowserHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ describeHistory('a browser history', () => {
})
})

describe('push with an invalid path string (bad percent-encoding)', () => {
it('throws an error', (done) => {
TestSequences.PushInvalidPathname(history, done)
})
})

describe('replace a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.ReplaceNewLocation(history, done)
Expand All @@ -89,6 +95,24 @@ describeHistory('a browser history', () => {
})
})

describe('replace with an invalid path string (bad percent-encoding)', () => {
it('throws an error', (done) => {
TestSequences.ReplaceInvalidPathname(history, done)
})
})

describe('location created by encoded and unencoded pathname', () => {
it('produces the same location.pathname', (done) => {
TestSequences.LocationPathnameAlwaysDecoded(history, done)
})
})

describe('location created with encoded/unencoded reserved characters', () => {
it('produces different location objects', (done) => {
TestSequences.EncodedReservedCharacters(history, done)
})
})

describe('goBack', () => {
it('calls change listeners with the previous location', (done) => {
TestSequences.GoBack(history, done)
Expand Down
24 changes: 24 additions & 0 deletions modules/__tests__/HashHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ describeHistory('a hash history', () => {
})
})

describe('push with an invalid path string (bad percent-encoding)', () => {
it('throws an error', (done) => {
TestSequences.PushInvalidPathname(history, done)
})
})

describe('replace a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.ReplaceNewLocation(history, done)
Expand All @@ -92,6 +98,24 @@ describeHistory('a hash history', () => {
})
})

describe('replace with an invalid path string (bad percent-encoding)', () => {
it('throws an error', (done) => {
TestSequences.ReplaceInvalidPathname(history, done)
})
})

describe('location created by encoded and unencoded pathname', () => {
it('produces the same location.pathname', (done) => {
TestSequences.LocationPathnameAlwaysDecoded(history, done)
})
})

describe('location created with encoded/unencoded reserved characters', () => {
it('produces different location objects', (done) => {
TestSequences.EncodedReservedCharacters(history, done)
})
})

describeGo('goBack', () => {
it('calls change listeners with the previous location', (done) => {
TestSequences.GoBack(history, done)
Expand Down
19 changes: 19 additions & 0 deletions modules/__tests__/LocationUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ describe('createLocation', () => {
})
})

describe('with a path that cannot be decoded', () => {

describe('given as a string', () => {
it('throws custom message when decodeURI throws a URIError', () => {
expect(() => {
createLocation('/test%')
}).toThrow('Pathname "/test%" could not be decoded.')
})
})

describe('given as an object', () => {
it('throws custom message when decodeURI throws a URIError', () => {
expect(() => {
createLocation({ pathname: '/test%' })
}).toThrow('Pathname "/test%" could not be decoded.')
})
})
})

describe('key', () => {
it('has a key property if a key is provided', () => {
const location = createLocation('/the/path', undefined, 'key')
Expand Down
24 changes: 24 additions & 0 deletions modules/__tests__/MemoryHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('a memory history', () => {
})
})

describe('push with an invalid path string (bad percent-encoding)', () => {
it('throws an error', (done) => {
TestSequences.PushInvalidPathname(history, done)
})
})

describe('replace a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.ReplaceNewLocation(history, done)
Expand All @@ -80,6 +86,24 @@ describe('a memory history', () => {
})
})

describe('replace with an invalid path string (bad percent-encoding)', () => {
it('throws an error', (done) => {
TestSequences.ReplaceInvalidPathname(history, done)
})
})

describe('location created by encoded and unencoded pathname', () => {
it('produces the same location.pathname', (done) => {
TestSequences.LocationPathnameAlwaysDecoded(history, done)
})
})

describe('location created with encoded/unencoded reserved characters', () => {
it('produces different location objects', (done) => {
TestSequences.EncodedReservedCharacters(history, done)
})
})

describe('goBack', () => {
it('calls change listeners with the previous location', (done) => {
TestSequences.GoBack(history, done)
Expand Down
46 changes: 46 additions & 0 deletions modules/__tests__/TestSequences/EncodedReservedCharacters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
() => {
// encoded string
const pathname = '/view/%23abc'
history.replace(pathname)
},
(location) => {
expect(location).toMatch({
pathname: '/view/%23abc'
})

// encoded object
const pathname = '/view/%23abc'
history.replace({ pathname })
},
(location) => {
expect(location).toMatch({
pathname: '/view/%23abc'
})
// unencoded string
const pathname = '/view/#abc'
history.replace(pathname)
}
,
(location) => {
expect(location).toMatch({
pathname: '/view/',
hash: '#abc'
})
// unencoded object
const pathname = '/view/#abc'
history.replace({ pathname })
},
(location) => {
expect(location).toMatch({
pathname: '/view/#abc'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure about this test case. It passes, but I'm not sure if the behavior is desirable. This only happens if the user passes a bad location object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the kind of thing we don't actually need to test. i.e. if anyone actually tried to do this I'd probably just tell them to use hash instead of putting it on the pathname.

})
}
]

execSteps(steps, history, done)
}
45 changes: 45 additions & 0 deletions modules/__tests__/TestSequences/LocationPathnameAlwaysDecoded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
() => {
// encoded string
const pathname = '/%E6%AD%B4%E5%8F%B2'
history.replace(pathname)
},
(location) => {
expect(location).toMatch({
pathname: '/歴史'
})

// encoded object
const pathname = '/%E6%AD%B4%E5%8F%B2'
history.replace({ pathname })
},
(location) => {
expect(location).toMatch({
pathname: '/歴史'
})
// unencoded string
const pathname = '/歴史'
history.replace(pathname)
}
,
(location) => {
expect(location).toMatch({
pathname: '/歴史'
})
// unencoded object
const pathname = '/歴史'
history.replace({ pathname })
},
(location) => {
expect(location).toMatch({
pathname: '/歴史'
})
}
]

execSteps(steps, history, done)
}
16 changes: 16 additions & 0 deletions modules/__tests__/TestSequences/PushInvalidPathname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
() => {
expect(() => {
history.push('/hello%')
}).toThrow(
'Pathname "/hello%" could not be decoded. This is likely caused by an invalid percent-encoding.'
)
}
]

execSteps(steps, history, done)
}
16 changes: 16 additions & 0 deletions modules/__tests__/TestSequences/ReplaceInvalidPathname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
() => {
expect(() => {
history.replace('/hello%')
}).toThrow(
'Pathname "/hello%" could not be decoded. This is likely caused by an invalid percent-encoding.'
)
}
]

execSteps(steps, history, done)
}
4 changes: 4 additions & 0 deletions modules/__tests__/TestSequences/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ export BlockPopWithoutListening from './BlockPopWithoutListening'
export DenyPush from './DenyPush'
export DenyGoBack from './DenyGoBack'
export DenyGoForward from './DenyGoForward'
export EncodedReservedCharacters from './EncodedReservedCharacters'
export GoBack from './GoBack'
export GoForward from './GoForward'
export HashbangHashPathCoding from './HashbangHashPathCoding'
export HashChangeTransitionHook from './HashChangeTransitionHook'
export InitialLocationNoKey from './InitialLocationNoKey'
export InitialLocationHasKey from './InitialLocationHasKey'
export Listen from './Listen'
export LocationPathnameAlwaysDecoded from './LocationPathnameAlwaysDecoded'
export NoslashHashPathCoding from './NoslashHashPathCoding'
export PushEncodedLocation from './PushEncodedLocation'
export PushInvalidPathname from './PushInvalidPathname'
export PushNewLocation from './PushNewLocation'
export PushMissingPathname from './PushMissingPathname'
export PushSamePath from './PushSamePath'
Expand All @@ -21,6 +24,7 @@ export PushState from './PushState'
export PushStateWarning from './PushStateWarning'
export PushRelativePathname from './PushRelativePathname'
export PushUnicodeLocation from './PushUnicodeLocation'
export ReplaceInvalidPathname from './ReplaceInvalidPathname'
export ReplaceNewLocation from './ReplaceNewLocation'
export ReplaceSamePath from './ReplaceSamePath'
export ReplaceState from './ReplaceState'
Expand Down