-
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.
Rewrite using natively supported ES6 features
- Loading branch information
Showing
5 changed files
with
80 additions
and
71 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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"extends": "airbnb-base/legacy", | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"sourceType": "module" | ||
}, | ||
"env": { | ||
"es6": true, | ||
"mocha": true | ||
}, | ||
"rules": { | ||
"arrow-body-style": 0 | ||
"arrow-body-style": 0, | ||
"new-cap": 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 |
---|---|---|
|
@@ -2,4 +2,4 @@ language: node_js | |
node_js: | ||
- "4" | ||
- "5" | ||
script: npm run prepublish | ||
- "6" |
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,57 +1,63 @@ | ||
import fetch from 'node-fetch'; | ||
import http from 'http'; | ||
import debug from 'debug'; | ||
var debug = require('debug'); | ||
var http = require('http'); | ||
var nodeFetch = require('node-fetch'); | ||
|
||
const log = debug('fetch-test-server'); | ||
var log = debug('fetch-test-server'); | ||
|
||
export default class TestServer { | ||
constructor(app) { | ||
this.server = http.createServer(app); | ||
|
||
['delete', 'get', 'head', 'options', 'patch', 'post', 'put'].forEach((method) => { | ||
this[method] = (path, options) => | ||
this.fetch(path, Object.assign({}, options, { method: method.toUpperCase() })); | ||
}); | ||
function TestServer(app) { | ||
if (!(this instanceof TestServer)) { | ||
return new TestServer; | ||
} | ||
|
||
listen() { | ||
if (!this.listener) { | ||
this.listener = new Promise((resolve, reject) => { | ||
this.server.listen(0, () => resolve()) | ||
.on('error', (err) => reject(err)); | ||
}); | ||
} | ||
this.server = http.createServer(app); | ||
|
||
return this.listener; | ||
} | ||
['delete', 'get', 'head', 'options', 'patch', 'post', 'put'].forEach((method) => { | ||
this[method] = (path, options) => | ||
this.fetch(path, Object.assign({}, options, { method: method.toUpperCase() })); | ||
}); | ||
|
||
close() { | ||
this.listener = null; | ||
Object.defineProperty(this, 'address', { | ||
get: function address() { | ||
var port = this.server.address().port; | ||
return `http://localhost:${port}`; | ||
} | ||
}); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
this.server.close((err) => (err ? reject(err) : resolve())); | ||
TestServer.prototype.listen = function listen() { | ||
if (!this.listener) { | ||
this.listener = new Promise((resolve, reject) => { | ||
this.server.listen(0, () => resolve()) | ||
.on('error', (err) => reject(err)); | ||
}); | ||
} | ||
|
||
get address() { | ||
const { port } = this.server.address(); | ||
return `http://localhost:${port}`; | ||
} | ||
return this.listener; | ||
}; | ||
|
||
fetch(path, opts) { | ||
return this.listen().then(() => { | ||
const options = Object.assign({ headers: {} }, opts); | ||
TestServer.prototype.close = function close() { | ||
this.listener = null; | ||
|
||
// automatic JSON encoding | ||
if (typeof options.body === 'object') { | ||
options.headers['Content-Type'] = 'application/json'; | ||
options.body = JSON.stringify(options.body); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.server.close((err) => (err ? reject(err) : resolve())); | ||
}); | ||
}; | ||
|
||
const url = `${this.address}${path}`; | ||
log(url, options); | ||
TestServer.prototype.fetch = function fetch(path, opts) { | ||
return this.listen().then(() => { | ||
var url = `${this.address}${path}`; | ||
var options = Object.assign({ headers: {} }, opts); | ||
|
||
return fetch(url, options); | ||
}); | ||
} | ||
} | ||
// automatic JSON encoding | ||
if (typeof options.body === 'object') { | ||
options.headers['Content-Type'] = 'application/json'; | ||
options.body = JSON.stringify(options.body); | ||
} | ||
|
||
log(url, options); | ||
|
||
return nodeFetch(url, options); | ||
}); | ||
}; | ||
|
||
module.exports = TestServer; |
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