Skip to content

Commit

Permalink
fix: do not re-assign args to same middleware node
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 17, 2022
1 parent edd9297 commit 16fdb35
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Server/PreCompiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ export class PreCompiler {
throw error
}

resolvedMiddleware.args = args
return resolvedMiddleware
return {
...resolvedMiddleware,
args,
}
})
}

Expand Down
35 changes: 35 additions & 0 deletions test/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,41 @@ test.group('Server | middleware', (group) => {
assert.isDefined(hookPacket!.parent_id)
assert.equal(hookPacket!.parent_id, requestPacket!.id)
})

test('use same middleware twice with different args', async ({ assert }) => {
const stack: string[] = []

const app = await setupApp()
const server = new Server(app, encryption, serverConfig)

const httpServer = createServer(server.handle.bind(server))

server.middleware.registerNamed({
access: async function middlewareFn1() {
return {
default: class Middleware {
public async handle(_ctx: HttpContextContract, next: any, args: string[]) {
stack.push(args[0])
await next()
}
},
}
},
})

server.router
.get('/', async () => {
return 'done'
})
.middleware('access:client')
.middleware('access:site')

server.optimize()

const { text } = await supertest(httpServer).get('/').expect(200)
assert.deepEqual(stack, ['client', 'site'])
assert.equal(text, 'done')
})
})

test.group('Server | hooks', (group) => {
Expand Down

0 comments on commit 16fdb35

Please sign in to comment.