-
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
12 changed files
with
382 additions
and
22 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,27 @@ | ||
import { Server } from '@hapi/hapi'; | ||
import '../controllers/rootController'; | ||
|
||
import '../controllers/deleteController'; | ||
import '../controllers/getController'; | ||
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 { RegisterRoutes } from './routes'; | ||
|
||
export const server = new Server({}); | ||
|
||
RegisterRoutes(server); | ||
|
||
server.start().catch(err => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); |
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,70 @@ | ||
import { expect } from 'chai'; | ||
import 'mocha'; | ||
import * as request from 'supertest'; | ||
import { server } from '../fixtures/hapi-success-code/server'; | ||
|
||
describe('Hapi 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: any) => any, methodOperation: (request: request.SuperTest<any>) => request.Test, expectedStatus = 200) { | ||
return new Promise((resolve, reject) => { | ||
methodOperation(request(server.listener)) | ||
.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 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 named 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
Oops, something went wrong.