fix(hono-base): prevent options object mutation (#3975)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 8796371 · Browse files · 2 files·+77 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: ehsan <ehsan2003.2003.382@gmail.com>
Diff stats
2 files+77 added−69 removed
- tests:+74 added−65 removed
- other:+3 added−4 removed
Provenance
- Commit
2eaaa5e- Tree
8985af0- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
8796371
2 files changed+77 added−69 removed
modified:src/hono-base.ts+3 added-4 removed
160 unmodified lines161 })162 return this as any163 }164 165- const strict = options.strict ?? true166- delete options.strict167- Object.assign(this, options)168- this.getPath = strict ? options.getPath ?? getPath : getPathNoStrict165+ const { strict, ...optionsWithoutStrict } = options166+ Object.assign(this, optionsWithoutStrict)167+ this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict168 }169 170 #clone(): Hono<E, S, BasePath> {171 const clone = new Hono<E, S, BasePath>({344 unmodified linesmodified:src/hono.test.ts+74 added-65 removed
303 unmodified lines304 })305 })306 })307 308-describe('router option', () => {309- it('Should be SmartRouter', () => {310- const app = new Hono()311- expect(app.router instanceof SmartRouter).toBe(true)312- })313- it('Should be RegExpRouter', () => {314- const app = new Hono({315- router: new RegExpRouter(),308+describe('Options', () => {309+ describe('router option', () => {310+ it('Should be SmartRouter', () => {311+ const app = new Hono()312+ expect(app.router instanceof SmartRouter).toBe(true)313 })314+ it('Should be RegExpRouter', () => {315+ const app = new Hono({316+ router: new RegExpRouter(),317+ })317- expect(app.router instanceof RegExpRouter).toBe(true)318+ expect(app.router instanceof RegExpRouter).toBe(true)319+ })320 })319-})321 321-describe('strict parameter', () => {322- describe('strict is true with not slash', () => {323- const app = new Hono()322+ describe('strict parameter', () => {323+ describe('strict is true with not slash', () => {324+ const app = new Hono()325 325- app.get('/hello', (c) => {326- return c.text('/hello')327- })326+ app.get('/hello', (c) => {327+ return c.text('/hello')328+ })329 329- it('/hello/ is not found', async () => {330- let res = await app.request('http://localhost/hello')331- expect(res).not.toBeNull()332- expect(res.status).toBe(200)333- res = await app.request('http://localhost/hello/')334- expect(res).not.toBeNull()335- expect(res.status).toBe(404)330+ it('/hello/ is not found', async () => {331+ let res = await app.request('http://localhost/hello')332+ expect(res).not.toBeNull()333+ expect(res.status).toBe(200)334+ res = await app.request('http://localhost/hello/')335+ expect(res).not.toBeNull()336+ expect(res.status).toBe(404)337+ })338 })337- })339 339- describe('strict is true with slash', () => {340- const app = new Hono()340+ describe('strict is true with slash', () => {341+ const app = new Hono()342 342- app.get('/hello/', (c) => {343- return c.text('/hello/')344- })343+ app.get('/hello/', (c) => {344+ return c.text('/hello/')345+ })346 346- it('/hello is not found', async () => {347- let res = await app.request('http://localhost/hello/')348- expect(res).not.toBeNull()349- expect(res.status).toBe(200)350- res = await app.request('http://localhost/hello')351- expect(res).not.toBeNull()352- expect(res.status).toBe(404)347+ it('/hello is not found', async () => {348+ let res = await app.request('http://localhost/hello/')349+ expect(res).not.toBeNull()350+ expect(res.status).toBe(200)351+ res = await app.request('http://localhost/hello')352+ expect(res).not.toBeNull()353+ expect(res.status).toBe(404)354+ })355 })354- })356 356- describe('strict is false', () => {357- const app = new Hono({ strict: false })357+ describe('strict is false', () => {358+ const app = new Hono({ strict: false })359 359- app.get('/hello', (c) => {360- return c.text('/hello')361- })360+ app.get('/hello', (c) => {361+ return c.text('/hello')362+ })363 363- it('/hello and /hello/ are treated as the same', async () => {364- let res = await app.request('http://localhost/hello')365- expect(res).not.toBeNull()366- expect(res.status).toBe(200)367- res = await app.request('http://localhost/hello/')368- expect(res).not.toBeNull()369- expect(res.status).toBe(200)364+ it('/hello and /hello/ are treated as the same', async () => {365+ let res = await app.request('http://localhost/hello')366+ expect(res).not.toBeNull()367+ expect(res.status).toBe(200)368+ res = await app.request('http://localhost/hello/')369+ expect(res).not.toBeNull()370+ expect(res.status).toBe(200)371+ })372 })371- })373 373- describe('strict is false with `getPath` option', () => {374- const app = new Hono({375- strict: false,376- getPath: getPath,377- })374+ describe('strict is false with `getPath` option', () => {375+ const app = new Hono({376+ strict: false,377+ getPath: getPath,378+ })379 379- app.get('/hello', (c) => {380- return c.text('/hello')381- })380+ app.get('/hello', (c) => {381+ return c.text('/hello')382+ })383 383- it('/hello and /hello/ are treated as the same', async () => {384- let res = await app.request('http://localhost/hello')385- expect(res).not.toBeNull()386- expect(res.status).toBe(200)387- res = await app.request('http://localhost/hello/')388- expect(res).not.toBeNull()389- expect(res.status).toBe(200)384+ it('/hello and /hello/ are treated as the same', async () => {385+ let res = await app.request('http://localhost/hello')386+ expect(res).not.toBeNull()387+ expect(res.status).toBe(200)388+ res = await app.request('http://localhost/hello/')389+ expect(res).not.toBeNull()390+ expect(res.status).toBe(200)391+ })392 })393 })394+395+ it('Should not modify the options passed to it', () => {396+ const options = { strict: true }397+ const clone = structuredClone(options)398+ const app = new Hono(clone)399+ expect(clone).toEqual(options)400+ })401 })402 403 describe('Destruct functions in context', () => {404 it('Should return 200 response - text', async () => {3201 unmodified lines