Skip to content

Commit

Permalink
chore: nock tests so they run faster and more reliably
Browse files Browse the repository at this point in the history
  • Loading branch information
garbados committed Nov 25, 2019
1 parent 6bd49c6 commit 0554d2e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions .nock/dat-librarian/should%20exist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"scope":"http://pfrazee.hashbase.io:80","method":"GET","path":"/","body":"","status":301,"response":"<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx/1.14.0</center>\r\n</body>\r\n</html>\r\n","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:31 GMT","Content-Type","text/html","Content-Length","185","Connection","close","Location","https://pfrazee.hashbase.io/"],"responseIsBinary":false},{"scope":"https://pfrazee.hashbase.io:443","method":"GET","path":"/","body":"","status":200,"response":["1f8b0800000000000003bc57db72db36107df7576cf8a2178194dc4e937a24a64e9ad4cea489274e9af4c903922b113108b0002859f9fa2e4052d6c5f2a4ada61edb1c2ece620ff60a4e9efcfafee5c73faf5e41e92a999e4cda073d9117e909d0cfa442c7212fb9b1e8a651e366ec59b4b9543a5733fcab118b69f4857d3a672f75557327328911e45a3954a477f96a8ac51cb73415af701a2d042e6b6ddc0678290a574e0b5c881c5978198250c2092e99cdb9c4e978088d4513de38599a2a4d5b87bdc3bf60c4092731bde28d84d7867f439c24ada8c5ed3129d0e646d44e68b541e65acfdc921b1c428139490c97e21bf7a021d4481c9c66fe0976651d5676085c15506983f19a526bea096360754ea780a5d6e18fb17d87b8a5700ecd59ce4db1c1c33655c5cdea467233c71b51f107bdd92b5be136ddff4b3d0b1e78442337c89d36dfa7a4e767c1951be80d3f3facf0b07b1fc6b6c7bb47f924b36749d2318a4b6ecb8c5b8c854eb8a5ccb489a8e649813362e16e5cd95459fcb59e3f72de239a686d48a16ec1a09c4622f7472c0dcea6d1a6ee8c2ffc525cab35b1a01490839ec08c18d978aef55c22af858d735d25b9b5cf67bc127235bd54b487d5923b7ef6e368347c3a1a0d82dd81752b89b644740370ab1aa7038777ceeb0ed25d8ef7d8a8c5463d76973989928a0b15fb35ea1149db1c4e26992e56fde10bb1805c920655afe1758da63be1eeaa57de5a0c00f20e58936fbbab8fc4bdbbd6f8729c4e78cf33daae719e12c5f18e4223b7055b9d628d92621f15167a5b7d8ce682e29f85c8ac8ba49579fbfb1b270fedfc1fec7749bc4da013fe3f0c32e4b76832a397b6e511a57ba27fcb6492ec866b52f7f993091dedab5f523735b7a015f0eda6dcb1019a13128bef3ac68b20f2dc63da578a05d2f081f3c63a7a7cfc12da7bb0c61d59cbfdb453abdec00bd920bcf555f696673686cf08a609ac9a4c8abc9b1468fc683b486aa3f544e945f71208edb8a93ee425134af5601093196211df55324a3f5c5fc36b7adb8bd5d6ee93844af8e182cea45e77b3cdb0dd8377e33db1e4b15ebde034a8d2d3d1f86778438e1afd3449fc72ba41d65b481ab50cde62731a8ba8a2f45327887f0b82331aad4d4deb4d0dfe4c336de0eaf4aa1fb8d492ecd611b7d3f09f927d06ef7307a7a707c82ecb15cbb42b2911a9264b569fd651fab95c412b042f0cec96252a58e90628b9a404458100a761de70c3692e2134342f2b7c7e74eaa3f123d4892ea3694d598a9671d51e84ae802ac7f6189e7a0f800e002de0a84cafb186d3431951d2d43296ae6654b3e74ae946e542cde1622d3e2a159f9ce3d1012a84233f152c78c450e95de825141a484ee50cfe3266a9895073e6e4fb10fd0e7adcc8be21e9e887831524bc45ba43875e644bf256604dd1634bcc2ccb24cf6f2971efd0fafaeae0700f0fbd8fe0d4d6b281858087803fe2319ec23bbd38dc082ce68d4126fde784b1aca0db1233fefb8384b5d17ac6e8d7f7e728bd0e50e8a014103570d061610b7b54fae7cdfc70aa284ddf35159595ac4b1ea5efe8f58fdfaf21bc9e81333466e85e6601ef88bcbf2b839ef503c3fa01e737f87a5c7ffbdc3ed8cb0cfa9b327d7e51b6f84ce96625e37406a6d079f7314d09f3e11e1872e46f000000ffff","ac98dd4fc23014c5ff95abcf56dd04d4c498e826f1237c24ecc5c7ee2332c1cdb041d2ffde73db0e21587ce913e4de414e6ed7f33b6d875e098b20fb204d665e958fa4721b8461fb56f0e5f9f59e5118ced3a39589b65769c322750f556e6508fc84b99fef698b65fb9c24d3d8bf2297ef4b3678b8585aaf5b6127c7d0842c7676441deed8a161efa3873ca3cfc10d4ebb79b1f4fb4aa21adeb81855a41cbbea0a07b746288424f851b4ad9cd17b7de251cd80e2223bb296d9bc2c36bc94fa6e0203eb0a34e302a747de0ff1eff501280fc5de2506ce81f1d2968ddebf0c770c90418e552d1bad8d810e453e6934d0361edc3a7db0155f455ee2bdd309ba64e68ceb96465ca4a82b7a557434b401cd6b244a6529ce2ecc6b698a88225dd1ab226645e822f6818185dde964c7b542ef7a9cec3ad013fca127204163b9293ff0a736f9805a2fd3e14ca707389b47c17d1060e52600de7326bca6aa58c15cf335827862020c8335e18e7f3dfd23e1fab39e57951219f2bd28aa6ca5be5b13aa5f758322c929e5c974fc4beb39a4f12e44de60c8f34c4eefa3aee07d483dade4ca49a2a5acc4422ad8955c2eb00713fea054d1033af426d57f5accfdc1ce99d57e45c9de59dd5d989bee1f000000ffff","0300de9097be01170000"],"rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:31 GMT","Content-Type","text/html","Connection","close","Vary","Accept-Encoding","X-Powered-By","Express","X-RateLimit-Limit","100","X-RateLimit-Remaining","96","X-RateLimit-Reset","1574693980","Access-Control-Allow-Origin","*","Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept","Cache-Control","public, max-age: 60","ETag","W/block-799","Content-Encoding","gzip"],"responseIsBinary":false},{"scope":"https://cloudflare-dns.com:443","method":"GET","path":"/dns-query?name=pfrazee.hashbase.io.&type=TXT","body":"","status":200,"response":{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"pfrazee.hashbase.io.","type":16}],"Authority":[{"name":"hashbase.io.","type":6,"TTL":3601,"data":"dns1.registrar-servers.com. hostmaster.registrar-servers.com. 2019090905 43200 3600 604800 3601"}]},"rawHeaders":["Date","Mon, 25 Nov 2019 14:59:31 GMT","Content-Type","application/dns-json","Content-Length","301","Connection","close","Access-Control-Allow-Origin","*","cache-control","max-age=3601","Expect-CT","max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","Server","cloudflare","CF-RAY","53b47caad9228ce5-PDX"],"responseIsBinary":false},{"scope":"https://pfrazee.hashbase.io:443","method":"GET","path":"/.well-known/dat","body":"","status":200,"response":"dat://88c835ba8b03a71eb5f27843331e28f2004884347412a1956a339355a6affb29/\nTTL=3600","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:32 GMT","Content-Length","80","Connection","close","X-Powered-By","Express","X-RateLimit-Limit","100","X-RateLimit-Remaining","95","X-RateLimit-Reset","1574693980"],"responseIsBinary":false}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"scope":"http://garbados.hashbase.io:80","method":"GET","path":"/","body":"","status":301,"response":"<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx/1.14.0</center>\r\n</body>\r\n</html>\r\n","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:29 GMT","Content-Type","text/html","Content-Length","185","Connection","close","Location","https://garbados.hashbase.io/"],"responseIsBinary":false},{"scope":"https://garbados.hashbase.io:443","method":"GET","path":"/","body":"","status":200,"response":["1f8b0800000000000003a493cf729b3010c6ef7e8a0d33b94526d809d324c07426eeb93df4d2a30c0b6c23248fb4fed7731fa06fd057ec235488b8d834b71c80e5fb2ddfee2229bb5a7d7efefaedcb2768b953c52c3b3d5056c50c20eb902594adb40e398fb65c8b0fd108b4ec308f7684fb8db11c416934a3f6897baab8cd2bdc518922bcdc006962924ab8522acc931b70ad25fd22d8889a38d7c61bf7ce5742805c9b2d8310837256ab42575ada30197d56eed9ec1d488bc02d8223e545f8beb5475f33486c7d6130357452bf90aee6ffcd20b7dc1a7b66b9821569035923ed5a56c67d6c3a496a5e9aae38ebb3967e42a3ddbf56959f082caa3ceaf5085a8bf510bbf83579ee6f515cbc95cdc78def853ad960bcd1cd9b9f8b243d24e93c60473fd0e55150de63b95c1c968b0bcba0bcc7f2213d3c5c761994deb2f764628545674c160fe12c8b874d97ad4d750c752bda81e3a3df2cc37e7a84e4f6f6fa29acdd25663cb0908a1afd08a55f3eb4a72c9fd726c59fdfbf7e9e2e5f2719d962ca16235b4ed9726477537637b2fb29bb1f593a65e9eb34b11f274c3d04593cfc069fd19fc8bf000000ffff","03009bf69017a8030000"],"rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:30 GMT","Content-Type","text/html","Connection","close","Vary","Accept-Encoding","X-Powered-By","Express","X-RateLimit-Limit","100","X-RateLimit-Remaining","99","X-RateLimit-Reset","1574693980","Access-Control-Allow-Origin","*","Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept","Cache-Control","public, max-age: 60","ETag","W/block-9","Content-Encoding","gzip"],"responseIsBinary":false},{"scope":"https://cloudflare-dns.com:443","method":"GET","path":"/dns-query?name=garbados.hashbase.io.&type=TXT","body":"","status":200,"response":{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"garbados.hashbase.io.","type":16}],"Authority":[{"name":"hashbase.io.","type":6,"TTL":3601,"data":"dns1.registrar-servers.com. hostmaster.registrar-servers.com. 2019090905 43200 3600 604800 3601"}]},"rawHeaders":["Date","Mon, 25 Nov 2019 14:59:30 GMT","Content-Type","application/dns-json","Content-Length","302","Connection","close","Access-Control-Allow-Origin","*","cache-control","max-age=3601","Expect-CT","max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","Server","cloudflare","CF-RAY","53b47ca2ba408d27-PDX"],"responseIsBinary":false},{"scope":"https://garbados.hashbase.io:443","method":"GET","path":"/.well-known/dat","body":"","status":200,"response":"dat://c33bc8d7c32a6e905905efdbf21efea9ff23b00d1c3ee9aea80092eaba6c4957/\nTTL=3600","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:30 GMT","Content-Length","80","Connection","close","X-Powered-By","Express","X-RateLimit-Limit","100","X-RateLimit-Remaining","98","X-RateLimit-Reset","1574693980"],"responseIsBinary":false}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"scope":"http://garbados.hashbase.io:80","method":"GET","path":"/","body":"","status":301,"response":"<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx/1.14.0</center>\r\n</body>\r\n</html>\r\n","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:30 GMT","Content-Type","text/html","Content-Length","185","Connection","close","Location","https://garbados.hashbase.io/"],"responseIsBinary":false}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"scope":"http://garbados.hashbase.io:80","method":"GET","path":"/","body":"","status":301,"response":"<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx/1.14.0</center>\r\n</body>\r\n</html>\r\n","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:32 GMT","Content-Type","text/html","Content-Length","185","Connection","close","Location","https://garbados.hashbase.io/"],"responseIsBinary":false}]
1 change: 1 addition & 0 deletions .nock/dat-librarian/should%20remove%20the%20archive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"scope":"http://garbados.hashbase.io:80","method":"GET","path":"/","body":"","status":301,"response":"<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx/1.14.0</center>\r\n</body>\r\n</html>\r\n","rawHeaders":["Server","nginx/1.14.0","Date","Mon, 25 Nov 2019 14:59:32 GMT","Content-Type","text/html","Content-Length","185","Connection","close","Location","https://garbados.hashbase.io/"],"responseIsBinary":false}]
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"rimraf": "^3.0.0"
},
"devDependencies": {
"standard": "^14.3.1",
"mocha": "^6.2.2"
"mocha": "^6.2.2",
"nock": "^11.7.0",
"standard": "^14.3.1"
},
"author": "Diana Thayer <garbados@gmail.com>",
"license": "Apache-2.0",
Expand Down
73 changes: 65 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,82 @@
/* global describe, it, before, after */
/* global describe, it, before, after, beforeEach, afterEach */

'use strict'

const assert = require('assert')
const DatLibrarian = require('.')
const fs = require('fs')
const mkdirp = require('mkdirp')
const nock = require('nock')
const path = require('path')
const rimraf = require('rimraf')
const { name, version } = require('./package.json')

describe([name, version].join(' @ '), function () {
const DatLibrarian = require('.')
const { name } = require('./package.json')

const NOCK_DIR = '.nock'
const RECORD_TESTS = !!process.env.RECORD_TESTS

const recordOrLoadNocks = function () {
const titles = []
let test = this.currentTest
while (test.parent) {
titles.unshift(test.title)
if (test.parent) { test = test.parent }
}
const dir = path.join(NOCK_DIR, ...titles.slice(0, -1))
const name = `${titles.slice(-1)[0]}.json`
this._currentNock = { titles, dir, name }
if (RECORD_TESTS) {
nock.recorder.rec({
output_objects: true,
dont_print: true
})
} else {
try {
nock.load(path.join(dir, encodeURIComponent(name)))
} catch (error) {
if (error.code === 'ENOENT') {
// no nock
} else {
throw error
}
}
}
}

const concludeNocks = function () {
if (RECORD_TESTS) {
// save http requests for future nocking
const { dir, name } = this._currentNock
const fixturePath = path.join(dir, encodeURIComponent(name))
const nockCallObjects = nock.recorder.play()
mkdirp.sync(dir)
fs.writeFileSync(fixturePath, JSON.stringify(nockCallObjects), 'utf8')
nock.restore()
nock.recorder.clear()
}
}

describe(name, function () {
this.timeout(0) // haha, THE NETWORK
const dir = 'fixtures'
const link = 'garbados.hashbase.io'

before(function () {
nock.disableNetConnect()
this.librarian = new DatLibrarian({ dir })
})

after(function () {
return this.librarian.close().then(() => {
rimraf.sync(dir)
})
after(async function () {
await this.librarian.close()
rimraf.sync(dir)
})

beforeEach(function () {
recordOrLoadNocks.call(this)
})

afterEach(async function () {
concludeNocks.call(this)
})

it('should exist', function () {
Expand Down

0 comments on commit 0554d2e

Please sign in to comment.