-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
54 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
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 |
---|---|---|
@@ -1,27 +1,86 @@ | ||
import express, { Router } from "express"; | ||
import express from "express"; | ||
import bodyParser from "body-parser"; | ||
import { BshbUtils } from "../src"; | ||
import { BshbUtils, Logger } from "../src"; | ||
import https from "https"; | ||
|
||
const bshc = express(); | ||
let bshcRouter: Router; | ||
export const createBshcRouter = () => { | ||
let bshcRouter = express.Router(); | ||
export const resetBshcRouter = () => { | ||
bshcRouter = express.Router(); | ||
return bshcRouter; | ||
}; | ||
|
||
let bshcAdminRouter = express.Router(); | ||
export const resetBshcAdminRouter = () => { | ||
bshcAdminRouter = express.Router(); | ||
return bshcAdminRouter; | ||
}; | ||
const bshcAdmin = express(); | ||
|
||
bshc.use(bodyParser.json()); | ||
bshc.use((req, res, next) => { | ||
console.log(`@${req.method} ${req.path}`); | ||
bshcRouter(req, res, next); | ||
next(); | ||
}); | ||
|
||
bshcAdmin.use(bodyParser.json()); | ||
bshcAdmin.use((req, res, next) => { | ||
bshcAdminRouter(req, res, next); | ||
next(); | ||
}); | ||
|
||
const certResult = BshbUtils.generateClientCertificate(); | ||
|
||
const server = https.createServer({ key: certResult.private, cert: certResult.cert }, bshc); | ||
const adminServer = https.createServer({ key: certResult.private, cert: certResult.cert }, bshcAdmin); | ||
|
||
server.listen(8444, () => {}); | ||
adminServer.listen(8443, () => {}); | ||
|
||
export class DefaultTestLogger implements Logger { | ||
fine(message?: any, ...optionalParams: any[]): void { | ||
DefaultTestLogger.log("debug", message, ...optionalParams); | ||
} | ||
|
||
debug(message?: any, ...optionalParams: any[]): void { | ||
DefaultTestLogger.log("debug", message, ...optionalParams); | ||
} | ||
|
||
info(message?: any, ...optionalParams: any[]): void { | ||
DefaultTestLogger.log("info", message, ...optionalParams); | ||
} | ||
|
||
warn(message?: any, ...optionalParams: any[]): void { | ||
DefaultTestLogger.log("warn", message, ...optionalParams); | ||
} | ||
|
||
error(message?: any, ...optionalParams: any[]): void { | ||
DefaultTestLogger.log("error", message, ...optionalParams); | ||
} | ||
|
||
private static log(msgType: "debug" | "info" | "warn" | "error", message?: any, ...optionalParams: any[]) { | ||
if (optionalParams[0] && optionalParams[0].ca) { | ||
optionalParams = [ | ||
{ | ||
path: optionalParams[0].path, | ||
method: optionalParams[0].method, | ||
headers: optionalParams[0].headers, | ||
body: optionalParams[0].body, | ||
}, | ||
]; | ||
} | ||
if (message) { | ||
if (optionalParams.length > 0) { | ||
console[msgType](message, optionalParams); | ||
} else { | ||
console[msgType](message); | ||
} | ||
} else { | ||
if (optionalParams.length > 0) { | ||
console[msgType](optionalParams); | ||
} else { | ||
console[msgType](); | ||
} | ||
} | ||
} | ||
} |