Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #23 from Nike-Inc/pr/20
Browse files Browse the repository at this point in the history
Pr/20
  • Loading branch information
jimmyeisenhauer authored Oct 11, 2019
2 parents 063d575 + ca26e94 commit f782fb6
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 204 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

The changelog for BOKOR includes information about the each release including any update notes, release notes as well as bug fixes, updates to existing features and new features.

---
## 2.0.0

### Release Notes
#### Added

- Migrated logging to Bunyan - thanks! [Erik Sutherland](https://github.com/MrRacoon)
- Replaced deprecated istanbul with nyc for code coverage
- Updated dependencies for security

---
## 1.3.3

Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,16 @@ bokorServer/
```

## Logs
`====[ cache miss ]====`
Bokor uses [Bunyan](https://github.com/trentm/node-bunyan) for logging. Bunyan is a simple and fast JSON logging library.
Bunyan has many features. One of them allows pretty-printing of the log. We show this feature in the below [Demo From Source](#demo-from-source)

❤️ If the request does not find a prerecorded response it will log cache miss in the color red.
`(cache=miss)`

`====[ cache hit ]====`
❤️ If the request does not find a prerecorded response it will log cache miss.

💚 If the request finds a prerecorded response it will log cache hit in the color green.
`(cache=hit)`

💚 If the request finds a prerecorded response it will log cache hit.

## Demo from Source
Follow the below steps to run the Bokor Demo.
Expand All @@ -303,7 +306,7 @@ $ git clone https://github.com/Nike-Inc/bokor.git
$ cd bokor
$ npm install
$ cd examples/source_example/
$ node server.js
$ node server.js | ../../node_modules/.bin/bunyan
```

Record a response
Expand All @@ -320,20 +323,20 @@ $ curl http://localhost:7777/users/jimmyeisenhauer
## Tests

```
$ npm test
$ npm run coverage
```

## FAQ
### Where did the name Bokor come from?
It is a long story, but one of my favorite quotes is:

#####“any sufficiently advanced technology is indistinguishable from magic.”
##### “any sufficiently advanced technology is indistinguishable from magic.”

-arthur c. clarke

So of course I penned a similar quote around test automation.

#####“software tests that unexplainably pass or fail are indistinguishable from voodoo.”
##### “software tests that unexplainably pass or fail are indistinguishable from voodoo.”
-james r. eisenhauer

And a Bokor is a voodoo sorcerer for hire who are said to serve the loa 'with both hands', practicing for both good and evil.
Expand Down
29 changes: 15 additions & 14 deletions lib/bokor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,38 @@ var bokorOptions = {};
require('colors');
require('http');

function start(options) {

// -- sepia config --------------
// default bokor admin server on by default
var sepia;
if (options.admin === false) {
sepia = require('./sepia');
} else {
sepia = require('./sepia').withSepiaServer();
}

function start(options) {
var logger = sepia.logger;

// -- bokor config --------------
if (options.servers != null) {
bokorOptions.servers = options.servers;
} else {
console.log('CONFIGURATION ERROR: Missing server config'.red);
logger.error('CONFIGURATION ERROR: Missing server config');
return;
}

if (options.filters != null) {
bokorOptions.filters = options.filters;
} else {
console.log('CONFIGURATION ERROR: Missing filters config'.red);
logger.error('CONFIGURATION ERROR: Missing filters config');
return;
}

bokorOptions.port = options.port || 7777; // bokor server port
bokorOptions.staticFileLocation = options.staticFileLocation || 'static_files'; // bokor server static file location
bokorOptions.secure = options.secure === undefined ? true : false;

// -- sepia config --------------
// default bokor admin server on by default
var sepia;
if (options.admin === false) {
sepia = require('./sepia');
} else {
sepia = require('./sepia').withSepiaServer();
}

sepia.configure({
verbose: true,
Expand Down Expand Up @@ -120,8 +121,8 @@ function start(options) {
host: serverConfig.url
}
}, function(e) {
console.log('[ERROR] proxying to endpoint: '.red + serverConfig.url);
console.log(e);
logger.error({ url: serverConfig.url }, 'proxying to endpoint');
logger.error({ err: e });
res.send(JSON.stringify({ bokorProxyError: 1 }));
});
});
Expand All @@ -131,7 +132,7 @@ function start(options) {

return new Promise(function (resolve) {
server.listen(server.get('port'), function() {
console.log('bokor'.red + ' server'.blue + ' rolled '.green.bold + 'lucky '.blue + server.get('port'));
logger.info({ port: server.get('port') }, 'bokor listening');
resolve(server);
});
});
Expand Down
5 changes: 0 additions & 5 deletions lib/sepia/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# This is a heavily modified version of sepia for bokor
# Thanks linkedin for the inspiration and code!






# sepia - the way things used to be

Sepia is a VCR-like module for node.js that records HTTP interactions, then
Expand Down
1 change: 1 addition & 0 deletions lib/sepia/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function shutdown(next) {
}

var sepiaUtil = require('./src/util');
module.exports.logger = sepiaUtil.internal.log;
module.exports.filter = sepiaUtil.addFilter;
module.exports.substitute = sepiaUtil.addSubstitution;
module.exports.fixtureDir = sepiaUtil.setFixtureDir;
Expand Down
7 changes: 7 additions & 0 deletions lib/sepia/src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var bunyan = require('bunyan');

const logger = bunyan.createLogger({
name: 'bokor',
});

module.exports = logger;
54 changes: 17 additions & 37 deletions lib/sepia/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ var url = require('url');
var path = require('path');
var crypto = require('crypto');
var fs = require('fs');
var logger = require('./logger');
var Levenshtein = require('levenshtein');

const COLOR_RESET = '\033[0m';
const COLOR_RED_BOLD = '\033[1;31m';
const COLOR_GREEN_BOLD = '\033[1;32m';
const COLOR_BLUE_BOLD = '\033[1;34m';

// -- GLOBAL STATE HANDLING ----------------------------------------------------

var globalOptions = {};
Expand Down Expand Up @@ -220,18 +216,6 @@ function usesGlobalFixtures(reqUrl) {

// -- LOGGING ------------------------------------------------------------------

function log(color, args) {
if (!globalOptions.verbose) {
return;
}

var args = Array.prototype.slice.call(args);
args.unshift(color);
args.push(COLOR_RESET);

console.log.apply(console, args);
}

function logFixtureStatus(filename, filenameParts) {
if (!globalOptions.verbose) {
return;
Expand All @@ -240,19 +224,17 @@ function logFixtureStatus(filename, filenameParts) {
filename = filename + '.headers';

if (fs.existsSync(filename)) {
log(COLOR_GREEN_BOLD, [
'\n ====[ cache hit ]====\n',
filenameParts, '\n',
'filename:', filename, '\n',
'======================\n'
]);
logger.info({
cache: 'hit',
filenameParts: filenameParts,
filename: filename,
});
} else {
log(COLOR_RED_BOLD, [
'\n ====[ cache miss ]====\n',
filenameParts, '\n',
'filename:', filename, '\n',
'======================\n'
]);
logger.info({
cache: 'miss',
filenameParts: filenameParts,
filename: filename,
});
}
}

Expand All @@ -262,13 +244,11 @@ function logFixtureDebugStatus(filename, bestMatchingFixture, fileHash) {
}

// Print the hashParts
log(COLOR_BLUE_BOLD, [
'\n ==== Best matching Fixture ====\n',
'to :', filename, '\n',
'filename:', bestMatchingFixture, '\n\n',
'hashParts:', fileHash, '\n',
'======================\n'
]);
logger.info({
to: filename,
filename: bestMatchingFixture,
hashParts: fileHash,
});
}

// -- FILENAME CONSTRUCTION ----------------------------------------------------
Expand Down Expand Up @@ -476,7 +456,7 @@ module.exports.internal.mkdirpSync = mkdirpSync;
module.exports.internal.filterByWhitelist = filterByWhitelist;
module.exports.internal.usesGlobalFixtures = usesGlobalFixtures;
module.exports.internal.touchOnHit = touchOnHit;
module.exports.internal.log = log;
module.exports.internal.log = logger;
module.exports.internal.logFixtureStatus = logFixtureStatus;
module.exports.internal.logFixtureDebugStatus = logFixtureDebugStatus;
module.exports.internal.parseCookiesNames = parseCookiesNames;
Expand Down
Loading

0 comments on commit f782fb6

Please sign in to comment.