-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record a .nock file for each test file
- Loading branch information
Showing
22 changed files
with
894 additions
and
429 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": [ | ||
["env", { | ||
"targets": { | ||
"node": "6" | ||
} | ||
}] | ||
], | ||
"plugins": ["transform-object-rest-spread"] | ||
} |
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 +1,5 @@ | ||
/.nyc_output | ||
/coverage | ||
/lib | ||
/fixtures | ||
!.eslintrc.js |
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,17 +1,11 @@ | ||
module.exports = { | ||
extends: ["standard", "prettier", "prettier/standard"], | ||
extends: ['standard', 'prettier', 'prettier/standard'], | ||
env: { | ||
es6: true, | ||
node: true | ||
}, | ||
plugins: ["prettier"], | ||
plugins: ['prettier'], | ||
rules: { | ||
"prettier/prettier": [ | ||
"warn", | ||
{ | ||
singleQuote: true, | ||
semi: false | ||
} | ||
] | ||
'prettier/prettier': 'warn' | ||
} | ||
}; | ||
} |
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,3 +1,4 @@ | ||
/lib | ||
/.nyc_output | ||
/coverage | ||
/lib | ||
/fixtures |
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 was deleted.
Oops, something went wrong.
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,43 +1,64 @@ | ||
{ | ||
"name": "ava-vcr", | ||
"name": "ava-nock", | ||
"version": "0.0.0", | ||
"description": "", | ||
"keywords": ["testing", "http", "mocking", "vcr", "record", "playback"], | ||
"keywords": [ | ||
"ava", | ||
"testing", | ||
"http", | ||
"nock", | ||
"mocking", | ||
"vcr", | ||
"record", | ||
"playback" | ||
], | ||
"author": "Brian Beck <[email protected]>", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"files": ["lib", "yarn.lock"], | ||
"scripts": { | ||
"build": "npm run build:lib", | ||
"build:lib": "babel --out-dir lib src", | ||
"build:lib": "babel src --out-dir lib --ignore \"**/*.test.js\"", | ||
"clean": "rimraf lib", | ||
"format": "npm run format:js && npm run format:other", | ||
"format:js": "npm run lint:fix || true", | ||
"format:other": "prettier --write \"**/*.{json,md}\"", | ||
"format:other": "prettier --write \"*.md\" \"**/*.json\"", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"prepare": "npm run clean && npm run build", | ||
"test": "npm run lint && npm run test:coverage", | ||
"test:cache": "VCR_MODE=cache ava", | ||
"test:cache": "NOCK_MODE=cache ava", | ||
"test:coverage": "nyc ava", | ||
"test:live": "VCR_MODE=live ava", | ||
"test:live": "NOCK_MODE=live ava", | ||
"test:only": "ava", | ||
"test:play": "VCR_MODE=play ava", | ||
"test:preview": "VCR_MODE=preview ava", | ||
"test:record": "VCR_MODE=record ava" | ||
"test:play": "NOCK_MODE=play ava", | ||
"test:preview": "NOCK_MODE=preview ava", | ||
"test:record": "NOCK_MODE=record ava" | ||
}, | ||
"ava": { | ||
"require": ["./src/index"], | ||
"require": ["babel-register"], | ||
"files": ["src/**/*.test.js", "test"] | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true | ||
}, | ||
"peerDependencies": { | ||
"ava": "^0.23.0" | ||
}, | ||
"dependencies": { | ||
"debug": "^3.1.0", | ||
"fs-extra": "^4.0.2", | ||
"http-proxy": "^1.16.2", | ||
"json-parse-better-errors": "^1.0.1", | ||
"json-stable-stringify": "^1.0.1", | ||
"nock": "^9.1.0", | ||
"object-hash": "^1.2.0", | ||
"replay": "^2.1.4", | ||
"request": "^2.83.0", | ||
"temp-write": "^3.3.0" | ||
"temp-write": "^3.3.0", | ||
"tempy": "^0.2.1", | ||
"uuid": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.23.0", | ||
|
@@ -46,7 +67,7 @@ | |
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.26.0", | ||
"eslint": "^4.10.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-prettier": "^2.7.0", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.8.0", | ||
|
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,60 @@ | ||
import zlib from 'zlib' | ||
import createLogger from 'debug' | ||
|
||
const debug = createLogger('ava-nock:encoding') | ||
const decoders = new Map([['gzip', zlib.gunzip], ['deflate', zlib.inflate]]) | ||
|
||
function findLastEncoding(headers) { | ||
const reversedHeaders = headers.slice().reverse() | ||
const index = reversedHeaders.findIndex( | ||
(header, index) => header.toLowerCase() === 'content-encoding' && index % 2 | ||
) | ||
return index === -1 ? -1 : headers.length - index - 1 | ||
} | ||
|
||
export function decodeResponse(call) { | ||
if (Array.isArray(call.response)) { | ||
// Reverse the headers array, because Content-Encoding can appear multiple | ||
// times and we need to decode the response in the opposite order that it | ||
// was encoded. | ||
const encodingIndex = findLastEncoding(call.rawHeaders) | ||
if (encodingIndex !== -1) { | ||
const encodingValue = call.rawHeaders[encodingIndex + 1] | ||
if (decoders.has(encodingValue)) { | ||
debug(`Found encoding: ${encodingValue}, attempting to decode.`) | ||
const decode = decoders.get(encodingValue) | ||
const buffer = Buffer.concat( | ||
call.response.map(hexString => Buffer.from(hexString, 'hex')) | ||
) | ||
return new Promise((resolve, reject) => { | ||
decode(buffer, (err, outputBuffer) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
// Remove the Content-Encoding that was processed. | ||
const headers = call.rawHeaders.slice() | ||
headers.splice(encodingIndex, 2) | ||
const hasEncoding = findLastEncoding(headers) !== -1 | ||
// If there's another Content-Encoding, leave the response as an | ||
// array of hex-encoded buffers and call this function again. | ||
// Otherwise, convert it to a string. | ||
const outputCall = { | ||
...call, | ||
rawHeaders: headers, | ||
response: hasEncoding | ||
? [outputBuffer.toString('hex')] | ||
: outputBuffer.toString() | ||
} | ||
if (hasEncoding) { | ||
decodeResponse(outputCall).then(resolve, reject) | ||
} else { | ||
resolve(outputCall) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
return Promise.resolve(call) | ||
} |
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import parseJSON from 'json-parse-better-errors' | ||
import stringifyJSON from 'json-stable-stringify' | ||
import { ensureDir, move } from 'fs-extra' | ||
import tempWrite from 'temp-write' | ||
import createLogger from 'debug' | ||
|
||
const debug = createLogger('ava-nock:fixtures') | ||
|
||
export function readFixture(filename) { | ||
filename = path.relative(process.cwd(), filename) | ||
return new Promise((resolve, reject) => { | ||
debug(`Reading fixture: ${filename}`) | ||
fs.readFile(filename, 'utf8', (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(parseJSON(data)) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export function writeFixture(filename, data, options) { | ||
filename = path.relative(process.cwd(), filename) | ||
const output = stringifyJSON(data, { space: 2 }) + '\n' | ||
debug(`Writing fixture to temporary file.`) | ||
return tempWrite(output) | ||
.then(tempFile => { | ||
const dirname = path.dirname(filename) | ||
debug(`Ensuring fixture directory exists: ${dirname}`) | ||
return ensureDir(dirname).then(() => tempFile) | ||
}) | ||
.then(tempFile => { | ||
debug(`Moving fixture into place: ${filename}`) | ||
return move(tempFile, filename, options) | ||
}) | ||
} |
Oops, something went wrong.