Skip to content

Commit

Permalink
Handle sent response
Browse files Browse the repository at this point in the history
  • Loading branch information
Rokt33r committed Mar 21, 2019
1 parent 15473d4 commit 96f3d32
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/specs/decorators/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tachijs, { ConfigSetter, controller, httpGet } from '../../index'
import request from 'supertest'
import { ErrorRequestHandler, RequestHandler } from 'express'
import { reqParams } from '../../decorators'
import { ErrorRequestHandler, RequestHandler, Response } from 'express'
import { reqParams, handlerParam } from '../../decorators'

describe('controller', () => {
it('sets path to router', async () => {
Expand Down Expand Up @@ -143,4 +143,33 @@ describe('controller', () => {
text: 'Hello, test'
})
})

it('does not send data if response is already sent', async () => {
// Given
function injectRes() {
return handlerParam((req, res) => res)
}
@controller('/:name', [], {
mergeParams: true
})
class HomeController {
@httpGet('/hello')
index(@injectRes() res: Response) {
res.send('Hello, test')
return 'Other thing'
}
}
const app = tachijs({
controllers: [HomeController]
})

// When
const response = await request(app).get('/test/hello')

// Then
expect(response).toMatchObject({
status: 200,
text: 'Hello, test'
})
})
})
4 changes: 3 additions & 1 deletion src/tachijs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class TachiJSApp<C> {
await result.execute(req, res, next)
return
}
res.send(result)
if (!res.finished) {
res.send(result)
}
return
} catch (error) {
next(error)
Expand Down

0 comments on commit 96f3d32

Please sign in to comment.