Last active
August 29, 2015 14:01
-
-
Save ierceg/98e5e7919e59f304d8e9 to your computer and use it in GitHub Desktop.
pgte/nock automated recording/replaying
This file contains hidden or 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
RESOURCES_TESTS_PATH = '<whatever works for you>' | |
pathToTestJson = (filename) -> | |
return path.join __dirname, RESOURCES_TESTS_PATH, filename | |
nockActionIsRecording = () -> | |
return not _.isUndefined process.env.NOCK_RECORDING and process.env.NOCK_RECORDING == '1' | |
# If we are recording (NOCK_ACTION == 'recording') then we start the recording and return the filename | |
# to which we will be saving them. | |
# Otherwise, we load the nock definitions from the file and give the user a chance to post-process them. | |
startNocking = (filename, options) -> | |
if nockActionIsRecording() | |
debug 'recording nock requests to', filename | |
recordNocks() | |
return filename | |
else | |
debug 'reading nock requests from', filename | |
defs = loadNockDefs filename | |
if options and options.preprocessor | |
options.preprocessor defs | |
nocks = defineNocks defs | |
if options and options.postprocessor | |
options.postprocessor nocks | |
debug 'tracking', nocks.length, 'nock requests' | |
return nocks | |
stopNocking = (nocksOrFilename) -> | |
if nockActionIsRecording() | |
debug 'stopped recording nock requests' | |
dumpRecordedNocks nocksOrFilename | |
else | |
debug 'stopped tracking nock requests' | |
nocksDone nocksOrFilename | |
loadNockDefs = (filename) -> | |
defs = nock.loadDefs pathToTestJson filename | |
expect(defs).to.exist | |
defs | |
defineNocks = (nockDefs) -> | |
expect(nockDefs).to.exist | |
nock.define nockDefs | |
loadNocks = (filename) -> | |
nockDefs = loadNockDefs filename | |
defineNocks nockDefs | |
nocksDone = (nocks) -> | |
_.each nocks, (nock) -> | |
nock.done() | |
recordNocks = () -> | |
nock.recorder.rec {dont_print: true, output_objects: true} | |
dumpRecordedNocks = (filename) -> | |
# Stop recording requests | |
nock.restore() | |
# Format output JSON for easier reading | |
recordedNocksJson = (JSON.stringify nock.recorder.play()).replace /{"scope"/g, '\n\r{"scope"' | |
if not filename | |
console.log recordedNocksJson | |
else | |
fs.writeFileSync pathToTestJson(filename), recordedNocksJson | |
# Clear the recorder requests | |
nock.recorder.clear() |
This file contains hidden or 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
describe 'module', -> | |
it 'does some easy HTTP', (done) -> | |
nocks = nockHelper.startNocking 'easyTest.json' | |
# Do your HTTP and once all the requests have completed... | |
nockHelper.endNocking nocks | |
done() | |
it 'does some complex HTTP with say Dropbox and CouchDb', (done) -> | |
nocks = nockHelper.startNocking 'dropboxTest.json', { | |
preprocessor: (nockDefs) -> | |
# You can modify nock definitions before nock objects are created out of them. | |
_.each nockDefs, (nockDef) -> | |
if nockDef.scope.indexOf('dropbox.com') != -1 | |
nockDef.options = nockDef.options || {}; | |
nockDef.options.filteringScope = (scope) -> | |
/^https:\/\/api[0-9]*.dropbox.com/.test(scope) | |
postprocessor: (nocks) -> | |
# And you can modify nock objects once they have been created. | |
nocks[3].filteringRequestBody (body) -> | |
return body if not body or typeof body != 'string' | |
return body.replace /("(?:createdOn|timestamp)"):([0-9]+)/g, (match, key, value) -> | |
return key + ':' + 1399582666185 | |
} | |
# Do your HTTP and once all the requests have completed... | |
nockHelper.endNocking nocks | |
done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment