Skip to content

Commit

Permalink
improve operation path rgx to accept querystring
Browse files Browse the repository at this point in the history
Signed-off-by: João Daniel <jotaf.daniel@gmail.com>
  • Loading branch information
jooaodanieel committed Nov 18, 2021
1 parent 5189805 commit 4e2cf29
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/domain/model/Operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ describe(Operation, () => {
})
})

describe('create with valid arguments', () => {
const validUnusualCases = [
{ caseID: `with ':'`, path: '/foo/:id' },
{ caseID: `with '?' and '='`, path: '/foo?key=value' },
]

validUnusualCases.forEach(({ caseID, path }) => {
test(`${caseID} doesn't throw error`, () => {
expect(() => Operation.create(HTTPVerb.GET, path)).not.toThrow()
})
})
})

describe('create with invalid arguments throwing InvalidStateError', () => {
test('when verb is a string not from the enum', () => {
// @ts-ignore
Expand All @@ -40,7 +53,6 @@ describe(Operation, () => {

const cases = [
{ id: 'with spaces', path: '/foo /bar' },
{ id: 'with invalid chars', path: '/foo?bar' },
{ id: 'without leading forward-slash', path: 'foo/bar' },
{ id: 'with trailling forward-slash', path: '/foo/bar/' },
]
Expand Down
2 changes: 1 addition & 1 deletion src/domain/model/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Operation {
const validVerb = verbs.includes(verb)
if (!validVerb) throw new InvalidStateError(`http verb must be one of: ${verbs.join(', ')}`)

const rgx = /^(\/[A-Za-z0-9\-_]+)+$/
const rgx = /^(\/[A-Za-z0-9\-_:]+)+(\?[A-Za-z0-9\-_=&\[\]]+)?$/
const validPath = rgx.test(path)

if (!validPath) throw new InvalidStateError('path must be of format /foo/baz/123, got ' + path)
Expand Down

0 comments on commit 4e2cf29

Please sign in to comment.