From 4e2cf29ed97f8ba39ceec1661257507a2c9e56c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Daniel?= Date: Thu, 18 Nov 2021 15:07:41 -0300 Subject: [PATCH] improve operation path rgx to accept querystring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Daniel --- src/domain/model/Operation.spec.ts | 14 +++++++++++++- src/domain/model/Operation.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/domain/model/Operation.spec.ts b/src/domain/model/Operation.spec.ts index b2b8399..cf06bb7 100644 --- a/src/domain/model/Operation.spec.ts +++ b/src/domain/model/Operation.spec.ts @@ -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 @@ -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/' }, ] diff --git a/src/domain/model/Operation.ts b/src/domain/model/Operation.ts index c5f63e6..59406ad 100644 --- a/src/domain/model/Operation.ts +++ b/src/domain/model/Operation.ts @@ -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)