-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add useSuccessResponseCode feature flag
- Loading branch information
Showing
9 changed files
with
268 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as bodyParser from 'body-parser'; | ||
import * as express from 'express'; | ||
import * as methodOverride from 'method-override'; | ||
import '../controllers/rootController'; | ||
|
||
import { RegisterRoutes } from './routes'; | ||
|
||
export const app: express.Express = express(); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(bodyParser.json()); | ||
app.use(methodOverride()); | ||
app.use((req: any, res: any, next: any) => { | ||
req.stringValue = 'fancyStringForContext'; | ||
next(); | ||
}); | ||
RegisterRoutes(app); | ||
|
||
// It's important that this come after the main routes are registered | ||
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
const status = err.status || 500; | ||
const body: any = { | ||
fields: err.fields || undefined, | ||
message: err.message || 'An error occurred during the request.', | ||
name: err.name, | ||
status, | ||
}; | ||
res.status(status).json(body); | ||
}); | ||
|
||
app.listen(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as Koa from 'koa'; | ||
import * as KoaRouter from '@koa/router'; | ||
import '../controllers/rootController'; | ||
|
||
import '../controllers/deleteController'; | ||
import '../controllers/getController'; | ||
import '../controllers/headController'; | ||
import '../controllers/patchController'; | ||
import '../controllers/postController'; | ||
import '../controllers/putController'; | ||
|
||
import '../controllers/methodController'; | ||
import '../controllers/parameterController'; | ||
import '../controllers/securityController'; | ||
import '../controllers/testController'; | ||
import '../controllers/validateController'; | ||
import '../controllers/noExtendsController'; | ||
|
||
import * as bodyParser from 'koa-bodyparser'; | ||
import { RegisterRoutes } from './routes'; | ||
|
||
const app = new Koa(); | ||
app.use(bodyParser()); | ||
|
||
const router = new KoaRouter(); | ||
|
||
RegisterRoutes(router); | ||
|
||
// It's important that this come after the main routes are registered | ||
app.use(async (context, next) => { | ||
try { | ||
await next(); | ||
} catch (err) { | ||
context.status = err.status || 500; | ||
context.body = err.message || 'An error occurred during the request.'; | ||
} | ||
}); | ||
|
||
app.use(router.routes()).use(router.allowedMethods()); | ||
|
||
export const server = app.listen(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { expect } from 'chai'; | ||
import 'mocha'; | ||
import * as request from 'supertest'; | ||
import { app } from '../fixtures/express-success-code/server'; | ||
|
||
describe('Express Server with useSuccessResponseCode', () => { | ||
const basePath = '/v1'; | ||
|
||
function verifyRequest(verifyResponse: (err: any, res: request.Response) => any, methodOperation: (request: request.SuperTest<any>) => request.Test, expectedStatus = 200) { | ||
return new Promise((resolve, reject) => { | ||
methodOperation(request(app)) | ||
.expect(expectedStatus) | ||
.end((err: any, res: any) => { | ||
let parsedError: any; | ||
try { | ||
parsedError = JSON.parse(res.error); | ||
} catch (err) { | ||
parsedError = res.error; | ||
} | ||
|
||
if (err) { | ||
reject({ | ||
error: err, | ||
response: parsedError, | ||
}); | ||
return; | ||
} | ||
|
||
verifyResponse(parsedError, res); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
function verifyGetRequest(path: string, verifyResponse: (err: any, res: request.Response) => any, expectedStatus?: number) { | ||
return verifyRequest(verifyResponse, request => request.get(path), expectedStatus); | ||
} | ||
|
||
describe('NoExtends', () => { | ||
it('should apply custom code from success response', () => { | ||
return verifyGetRequest( | ||
basePath + `/NoExtends/customSuccessResponseCode`, | ||
(err, res) => { | ||
expect(res.status).to.equal(202); | ||
}, | ||
202, | ||
); | ||
}); | ||
|
||
it('should apply enum code from success response', () => { | ||
return verifyGetRequest( | ||
basePath + `/NoExtends/enumSuccessResponseCode`, | ||
(err, res) => { | ||
expect(res.status).to.equal(202); | ||
}, | ||
202, | ||
); | ||
}); | ||
|
||
it('should ignore 2XX code range from success response', () => { | ||
return verifyGetRequest( | ||
basePath + `/NoExtends/rangedSuccessResponse`, | ||
(err, res) => { | ||
expect(res.status).to.equal(204); | ||
}, | ||
204, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { expect } from 'chai'; | ||
import 'mocha'; | ||
import * as request from 'supertest'; | ||
import { server } from '../fixtures/koa-success-code/server'; | ||
|
||
describe('Koa Server with useSuccessResponseCode', () => { | ||
const basePath = '/v1'; | ||
|
||
function verifyGetRequest(path: string, verifyResponse: (err: any, res: request.Response) => any, expectedStatus?: number) { | ||
return verifyRequest(verifyResponse, request => request.get(path), expectedStatus); | ||
} | ||
|
||
function verifyRequest(verifyResponse: (err: any, res: request.Response) => any, methodOperation: (request: request.SuperTest<any>) => request.Test, expectedStatus = 200) { | ||
return new Promise((resolve, reject) => { | ||
methodOperation(request(server)) | ||
.expect(expectedStatus) | ||
.end((err: any, res: any) => { | ||
let parsedError: any; | ||
|
||
try { | ||
parsedError = JSON.parse(res.error); | ||
} catch (err) { | ||
parsedError = res.error; | ||
} | ||
|
||
if (err) { | ||
reject({ | ||
error: err, | ||
response: parsedError, | ||
}); | ||
return; | ||
} | ||
|
||
verifyResponse(parsedError, res); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
describe('NoExtends', () => { | ||
it('should ignore SuccessResponse code and use default code', () => { | ||
return verifyGetRequest( | ||
basePath + `/NoExtends/customSuccessResponseCode`, | ||
(err, res) => { | ||
expect(res.status).to.equal(202); | ||
}, | ||
202, | ||
); | ||
}); | ||
|
||
it('should ignore SuccessResponse enum code and use default code', () => { | ||
return verifyGetRequest( | ||
basePath + `/NoExtends/enumSuccessResponseCode`, | ||
(err, res) => { | ||
expect(res.status).to.equal(202); | ||
}, | ||
202, | ||
); | ||
}); | ||
|
||
it('should ignore SuccessResponse 2XX code and use default code', () => { | ||
return verifyGetRequest( | ||
basePath + `/NoExtends/rangedSuccessResponse`, | ||
(err, res) => { | ||
expect(res.status).to.equal(204); | ||
}, | ||
204, | ||
); | ||
}); | ||
}); | ||
|
||
it('shutdown server', () => server.close()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters