nodemailer-fetch
Advanced tools
Comparing version 1.6.0 to 2.0.0
@@ -21,3 +21,3 @@ 'use strict'; | ||
'no-unused-vars': 2, | ||
'no-undef': 2, | ||
'no-undefined': 2, | ||
'handle-callback-err': 2, | ||
@@ -43,16 +43,30 @@ 'no-new': 2, | ||
'no-throw-literal': 2, | ||
'no-useless-call': 2, | ||
'no-useless-concat': 2, | ||
'no-void': 2, | ||
yoda: 2, | ||
'no-undef': 2, | ||
'global-require': 2, | ||
'no-var': 2, | ||
'no-bitwise': 2, | ||
'no-lonely-if': 2, | ||
'no-mixed-spaces-and-tabs': 2, | ||
'no-console': 0 | ||
'arrow-body-style': [2, 'as-needed'], | ||
'arrow-parens': [2, 'as-needed'], | ||
'prefer-arrow-callback': 2, | ||
'object-shorthand': 2, | ||
'prefer-spread': 2 | ||
}, | ||
env: { | ||
es6: false, | ||
es6: true, | ||
node: true | ||
}, | ||
extends: 'eslint:recommended', | ||
globals: { | ||
it: true, | ||
describe: true, | ||
beforeEach: true, | ||
afterEach: true | ||
}, | ||
fix: true | ||
}; |
# Changelog | ||
## v2.0.0 2016-09-29 | ||
* Start using ES6 syntax, drop support for older Node.js versions | ||
## v1.6.0 2016-08-18 | ||
@@ -4,0 +8,0 @@ |
@@ -8,3 +8,3 @@ 'use strict'; | ||
eslint: { | ||
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js', '.eslintrc.js'] | ||
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js'] | ||
}, | ||
@@ -11,0 +11,0 @@ |
@@ -5,8 +5,6 @@ 'use strict'; | ||
var urllib = require('url'); | ||
const urllib = require('url'); | ||
var SESSION_TIMEOUT = 1800; // 30 min | ||
const SESSION_TIMEOUT = 1800; // 30 min | ||
module.exports = Cookies; | ||
/** | ||
@@ -18,260 +16,263 @@ * Creates a biskviit cookie jar for managing cookie values in memory | ||
*/ | ||
function Cookies(options) { | ||
this.options = options || {}; | ||
this.cookies = []; | ||
} | ||
class Cookies { | ||
constructor(options) { | ||
this.options = options || {}; | ||
this.cookies = []; | ||
} | ||
/** | ||
* Stores a cookie string to the cookie storage | ||
* | ||
* @param {String} cookieStr Value from the 'Set-Cookie:' header | ||
* @param {String} url Current URL | ||
*/ | ||
Cookies.prototype.set = function (cookieStr, url) { | ||
var urlparts = urllib.parse(url || ''); | ||
var cookie = this.parse(cookieStr); | ||
var domain; | ||
/** | ||
* Stores a cookie string to the cookie storage | ||
* | ||
* @param {String} cookieStr Value from the 'Set-Cookie:' header | ||
* @param {String} url Current URL | ||
*/ | ||
set(cookieStr, url) { | ||
let urlparts = urllib.parse(url || ''); | ||
let cookie = this.parse(cookieStr); | ||
let domain; | ||
if (cookie.domain) { | ||
domain = cookie.domain.replace(/^\./, ''); | ||
if (cookie.domain) { | ||
domain = cookie.domain.replace(/^\./, ''); | ||
// do not allow cross origin cookies | ||
if ( | ||
// can't be valid if the requested domain is shorter than current hostname | ||
urlparts.hostname.length < domain.length || | ||
// do not allow cross origin cookies | ||
if ( | ||
// can't be valid if the requested domain is shorter than current hostname | ||
urlparts.hostname.length < domain.length || | ||
// prefix domains with dot to be sure that partial matches are not used | ||
('.' + urlparts.hostname).substr(-domain.length + 1) !== ('.' + domain)) { | ||
// prefix domains with dot to be sure that partial matches are not used | ||
('.' + urlparts.hostname).substr(-domain.length + 1) !== ('.' + domain)) { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
} else { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
} else { | ||
cookie.domain = urlparts.hostname; | ||
} | ||
if (!cookie.path) { | ||
cookie.path = this.getPath(urlparts.pathname); | ||
if (!cookie.path) { | ||
cookie.path = this.getPath(urlparts.pathname); | ||
} | ||
// if no expire date, then use sessionTimeout value | ||
if (!cookie.expires) { | ||
cookie.expires = new Date(Date.now() + (Number(this.options.sessionTimeout || SESSION_TIMEOUT) || SESSION_TIMEOUT) * 1000); | ||
} | ||
return this.add(cookie); | ||
} | ||
// if no expire date, then use sessionTimeout value | ||
if (!cookie.expires) { | ||
cookie.expires = new Date(Date.now() + (Number(this.options.sessionTimeout || SESSION_TIMEOUT) || SESSION_TIMEOUT) * 1000); | ||
/** | ||
* Returns cookie string for the 'Cookie:' header. | ||
* | ||
* @param {String} url URL to check for | ||
* @returns {String} Cookie header or empty string if no matches were found | ||
*/ | ||
get(url) { | ||
return this.list(url).map(cookie => | ||
cookie.name + '=' + cookie.value).join('; '); | ||
} | ||
return this.add(cookie); | ||
}; | ||
/** | ||
* Lists all valied cookie objects for the specified URL | ||
* | ||
* @param {String} url URL to check for | ||
* @returns {Array} An array of cookie objects | ||
*/ | ||
list(url) { | ||
let result = []; | ||
let i; | ||
let cookie; | ||
/** | ||
* Returns cookie string for the 'Cookie:' header. | ||
* | ||
* @param {String} url URL to check for | ||
* @returns {String} Cookie header or empty string if no matches were found | ||
*/ | ||
Cookies.prototype.get = function (url) { | ||
return this.list(url).map(function (cookie) { | ||
return cookie.name + '=' + cookie.value; | ||
}).join('; '); | ||
}; | ||
for (i = this.cookies.length - 1; i >= 0; i--) { | ||
cookie = this.cookies[i]; | ||
/** | ||
* Lists all valied cookie objects for the specified URL | ||
* | ||
* @param {String} url URL to check for | ||
* @returns {Array} An array of cookie objects | ||
*/ | ||
Cookies.prototype.list = function (url) { | ||
var result = []; | ||
var i; | ||
var cookie; | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, i); | ||
continue; | ||
} | ||
for (i = this.cookies.length - 1; i >= 0; i--) { | ||
cookie = this.cookies[i]; | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, i); | ||
continue; | ||
if (this.match(cookie, url)) { | ||
result.unshift(cookie); | ||
} | ||
} | ||
if (this.match(cookie, url)) { | ||
result.unshift(cookie); | ||
} | ||
return result; | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Parses cookie string from the 'Set-Cookie:' header | ||
* | ||
* @param {String} cookieStr String from the 'Set-Cookie:' header | ||
* @returns {Object} Cookie object | ||
*/ | ||
parse(cookieStr) { | ||
let cookie = {}; | ||
/** | ||
* Parses cookie string from the 'Set-Cookie:' header | ||
* | ||
* @param {String} cookieStr String from the 'Set-Cookie:' header | ||
* @returns {Object} Cookie object | ||
*/ | ||
Cookies.prototype.parse = function (cookieStr) { | ||
var cookie = {}; | ||
(cookieStr || '').toString().split(';').forEach(cookiePart => { | ||
let valueParts = cookiePart.split('='); | ||
let key = valueParts.shift().trim().toLowerCase(); | ||
let value = valueParts.join('=').trim(); | ||
let domain; | ||
(cookieStr || '').toString().split(';').forEach(function (cookiePart) { | ||
var valueParts = cookiePart.split('='); | ||
var key = valueParts.shift().trim().toLowerCase(); | ||
var value = valueParts.join('=').trim(); | ||
var domain; | ||
if (!key) { | ||
// skip empty parts | ||
return; | ||
} | ||
if (!key) { | ||
// skip empty parts | ||
return; | ||
} | ||
switch (key) { | ||
switch (key) { | ||
case 'expires': | ||
value = new Date(value); | ||
// ignore date if can not parse it | ||
if (value.toString() !== 'Invalid Date') { | ||
cookie.expires = value; | ||
} | ||
break; | ||
case 'expires': | ||
value = new Date(value); | ||
// ignore date if can not parse it | ||
if (value.toString() !== 'Invalid Date') { | ||
cookie.expires = value; | ||
} | ||
break; | ||
case 'path': | ||
cookie.path = value; | ||
break; | ||
case 'path': | ||
cookie.path = value; | ||
break; | ||
case 'domain': | ||
domain = value.toLowerCase(); | ||
if (domain.length && domain.charAt(0) !== '.') { | ||
domain = '.' + domain; // ensure preceeding dot for user set domains | ||
} | ||
cookie.domain = domain; | ||
break; | ||
case 'domain': | ||
domain = value.toLowerCase(); | ||
if (domain.length && domain.charAt(0) !== '.') { | ||
domain = '.' + domain; // ensure preceeding dot for user set domains | ||
} | ||
cookie.domain = domain; | ||
break; | ||
case 'max-age': | ||
cookie.expires = new Date(Date.now() + (Number(value) || 0) * 1000); | ||
break; | ||
case 'max-age': | ||
cookie.expires = new Date(Date.now() + (Number(value) || 0) * 1000); | ||
break; | ||
case 'secure': | ||
cookie.secure = true; | ||
break; | ||
case 'secure': | ||
cookie.secure = true; | ||
break; | ||
case 'httponly': | ||
cookie.httponly = true; | ||
break; | ||
case 'httponly': | ||
cookie.httponly = true; | ||
break; | ||
default: | ||
if (!cookie.name) { | ||
cookie.name = key; | ||
cookie.value = value; | ||
} | ||
} | ||
}); | ||
default: | ||
if (!cookie.name) { | ||
cookie.name = key; | ||
cookie.value = value; | ||
} | ||
} | ||
}); | ||
return cookie; | ||
} | ||
return cookie; | ||
}; | ||
/** | ||
* Checks if a cookie object is valid for a specified URL | ||
* | ||
* @param {Object} cookie Cookie object | ||
* @param {String} url URL to check for | ||
* @returns {Boolean} true if cookie is valid for specifiec URL | ||
*/ | ||
match(cookie, url) { | ||
let urlparts = urllib.parse(url || ''); | ||
/** | ||
* Checks if a cookie object is valid for a specified URL | ||
* | ||
* @param {Object} cookie Cookie object | ||
* @param {String} url URL to check for | ||
* @returns {Boolean} true if cookie is valid for specifiec URL | ||
*/ | ||
Cookies.prototype.match = function (cookie, url) { | ||
var urlparts = urllib.parse(url || ''); | ||
// check if hostname matches | ||
// .foo.com also matches subdomains, foo.com does not | ||
if (urlparts.hostname !== cookie.domain && (cookie.domain.charAt(0) !== '.' || ('.' + urlparts.hostname).substr(-cookie.domain.length) !== cookie.domain)) { | ||
return false; | ||
} | ||
// check if hostname matches | ||
// .foo.com also matches subdomains, foo.com does not | ||
if (urlparts.hostname !== cookie.domain && (cookie.domain.charAt(0) !== '.' || ('.' + urlparts.hostname).substr(-cookie.domain.length) !== cookie.domain)) { | ||
return false; | ||
} | ||
// check if path matches | ||
let path = this.getPath(urlparts.pathname); | ||
if (path.substr(0, cookie.path.length) !== cookie.path) { | ||
return false; | ||
} | ||
// check if path matches | ||
var path = this.getPath(urlparts.pathname); | ||
if (path.substr(0, cookie.path.length) !== cookie.path) { | ||
return false; | ||
} | ||
// check secure argument | ||
if (cookie.secure && urlparts.protocol !== 'https:') { | ||
return false; | ||
} | ||
// check secure argument | ||
if (cookie.secure && urlparts.protocol !== 'https:') { | ||
return false; | ||
return true; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Adds (or updates/removes if needed) a cookie object to the cookie storage | ||
* | ||
* @param {Object} cookie Cookie value to be stored | ||
*/ | ||
add(cookie) { | ||
let i; | ||
let len; | ||
/** | ||
* Adds (or updates/removes if needed) a cookie object to the cookie storage | ||
* | ||
* @param {Object} cookie Cookie value to be stored | ||
*/ | ||
Cookies.prototype.add = function (cookie) { | ||
var i; | ||
var len; | ||
// nothing to do here | ||
if (!cookie || !cookie.name) { | ||
return false; | ||
} | ||
// nothing to do here | ||
if (!cookie || !cookie.name) { | ||
return false; | ||
} | ||
// overwrite if has same params | ||
for (i = 0, len = this.cookies.length; i < len; i++) { | ||
if (this.compare(this.cookies[i], cookie)) { | ||
// overwrite if has same params | ||
for (i = 0, len = this.cookies.length; i < len; i++) { | ||
if (this.compare(this.cookies[i], cookie)) { | ||
// check if the cookie needs to be removed instead | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, 1); // remove expired/unset cookie | ||
return false; | ||
} | ||
// check if the cookie needs to be removed instead | ||
if (this.isExpired(cookie)) { | ||
this.cookies.splice(i, 1); // remove expired/unset cookie | ||
return false; | ||
this.cookies[i] = cookie; | ||
return true; | ||
} | ||
} | ||
this.cookies[i] = cookie; | ||
return true; | ||
// add as new if not already expired | ||
if (!this.isExpired(cookie)) { | ||
this.cookies.push(cookie); | ||
} | ||
return true; | ||
} | ||
// add as new if not already expired | ||
if (!this.isExpired(cookie)) { | ||
this.cookies.push(cookie); | ||
/** | ||
* Checks if two cookie objects are the same | ||
* | ||
* @param {Object} a Cookie to check against | ||
* @param {Object} b Cookie to check against | ||
* @returns {Boolean} True, if the cookies are the same | ||
*/ | ||
compare(a, b) { | ||
return a.name === b.name && a.path === b.path && a.domain === b.domain && a.secure === b.secure && a.httponly === a.httponly; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Checks if a cookie is expired | ||
* | ||
* @param {Object} cookie Cookie object to check against | ||
* @returns {Boolean} True, if the cookie is expired | ||
*/ | ||
isExpired(cookie) { | ||
return (cookie.expires && cookie.expires < new Date()) || !cookie.value; | ||
} | ||
/** | ||
* Checks if two cookie objects are the same | ||
* | ||
* @param {Object} a Cookie to check against | ||
* @param {Object} b Cookie to check against | ||
* @returns {Boolean} True, if the cookies are the same | ||
*/ | ||
Cookies.prototype.compare = function (a, b) { | ||
return a.name === b.name && a.path === b.path && a.domain === b.domain && a.secure === b.secure && a.httponly === a.httponly; | ||
}; | ||
/** | ||
* Returns normalized cookie path for an URL path argument | ||
* | ||
* @param {String} pathname | ||
* @returns {String} Normalized path | ||
*/ | ||
getPath(pathname) { | ||
let path = (pathname || '/').split('/'); | ||
path.pop(); // remove filename part | ||
path = path.join('/').trim(); | ||
/** | ||
* Checks if a cookie is expired | ||
* | ||
* @param {Object} cookie Cookie object to check against | ||
* @returns {Boolean} True, if the cookie is expired | ||
*/ | ||
Cookies.prototype.isExpired = function (cookie) { | ||
return (cookie.expires && cookie.expires < new Date()) || !cookie.value; | ||
}; | ||
// ensure path prefix / | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
/** | ||
* Returns normalized cookie path for an URL path argument | ||
* | ||
* @param {String} pathname | ||
* @returns {String} Normalized path | ||
*/ | ||
Cookies.prototype.getPath = function (pathname) { | ||
var path = (pathname || '/').split('/'); | ||
path.pop(); // remove filename part | ||
path = path.join('/').trim(); | ||
// ensure path suffix / | ||
if (path.substr(-1) !== '/') { | ||
path += '/'; | ||
} | ||
// ensure path prefix / | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
return path; | ||
} | ||
} | ||
// ensure path suffix / | ||
if (path.substr(-1) !== '/') { | ||
path += '/'; | ||
} | ||
return path; | ||
}; | ||
module.exports = Cookies; |
'use strict'; | ||
var http = require('http'); | ||
var https = require('https'); | ||
var urllib = require('url'); | ||
var zlib = require('zlib'); | ||
var PassThrough = require('stream').PassThrough; | ||
var Cookies = require('./cookies'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
const urllib = require('url'); | ||
const zlib = require('zlib'); | ||
const PassThrough = require('stream').PassThrough; | ||
const Cookies = require('./cookies'); | ||
var MAX_REDIRECTS = 5; | ||
const MAX_REDIRECTS = 5; | ||
@@ -27,3 +27,3 @@ module.exports = function (url, options) { | ||
if (options.cookie) { | ||
[].concat(options.cookie || []).forEach(function (cookie) { | ||
[].concat(options.cookie || []).forEach(cookie => { | ||
options.cookies.set(cookie, url); | ||
@@ -34,16 +34,16 @@ }); | ||
var fetchRes = options.fetchRes; | ||
var parsed = urllib.parse(url); | ||
var method = (options.method || '').toString().trim().toUpperCase() || 'GET'; | ||
var finished = false; | ||
var cookies; | ||
var body; | ||
let fetchRes = options.fetchRes; | ||
let parsed = urllib.parse(url); | ||
let method = (options.method || '').toString().trim().toUpperCase() || 'GET'; | ||
let finished = false; | ||
let cookies; | ||
let body; | ||
var handler = parsed.protocol === 'https:' ? https : http; | ||
let handler = parsed.protocol === 'https:' ? https : http; | ||
var headers = { | ||
let headers = { | ||
'accept-encoding': 'gzip,deflate' | ||
}; | ||
Object.keys(options.headers || {}).forEach(function (key) { | ||
Object.keys(options.headers || {}).forEach(key => { | ||
headers[key.toLowerCase().trim()] = options.headers[key]; | ||
@@ -73,3 +73,3 @@ }); | ||
body = options.body; | ||
body.on('error', function (err) { | ||
body.on('error', err => { | ||
if (finished) { | ||
@@ -85,4 +85,4 @@ return; | ||
} else if (typeof options.body === 'object') { | ||
body = new Buffer(Object.keys(options.body).map(function (key) { | ||
var value = options.body[key].toString().trim(); | ||
body = new Buffer(Object.keys(options.body).map(key => { | ||
let value = options.body[key].toString().trim(); | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(value); | ||
@@ -101,9 +101,9 @@ }).join('&')); | ||
var req; | ||
var reqOptions = { | ||
method: method, | ||
let req; | ||
let reqOptions = { | ||
method, | ||
host: parsed.hostname, | ||
path: parsed.path, | ||
port: parsed.port ? parsed.port : (parsed.protocol === 'https:' ? 443 : 80), | ||
headers: headers, | ||
headers, | ||
rejectUnauthorized: false, | ||
@@ -114,3 +114,3 @@ agent: false | ||
if (options.tls) { | ||
Object.keys(options.tls).forEach(function (key) { | ||
Object.keys(options.tls).forEach(key => { | ||
reqOptions[key] = options.tls[key]; | ||
@@ -124,3 +124,3 @@ }); | ||
finished = true; | ||
setImmediate(function () { | ||
setImmediate(() => { | ||
fetchRes.emit('error', E); | ||
@@ -132,3 +132,3 @@ }); | ||
if (options.timeout) { | ||
req.setTimeout(options.timeout, function () { | ||
req.setTimeout(options.timeout, () => { | ||
if (finished) { | ||
@@ -143,3 +143,3 @@ return; | ||
req.on('error', function (err) { | ||
req.on('error', err => { | ||
if (finished) { | ||
@@ -152,4 +152,4 @@ return; | ||
req.on('response', function (res) { | ||
var inflate; | ||
req.on('response', res => { | ||
let inflate; | ||
@@ -168,3 +168,3 @@ if (finished) { | ||
if (res.headers['set-cookie']) { | ||
[].concat(res.headers['set-cookie'] || []).forEach(function (cookie) { | ||
[].concat(res.headers['set-cookie'] || []).forEach(cookie => { | ||
options.cookies.set(cookie, url); | ||
@@ -174,3 +174,3 @@ }); | ||
if ([301, 302, 303, 307, 308].indexOf(res.statusCode) >= 0 && res.headers.location) { | ||
if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) { | ||
// redirect | ||
@@ -194,3 +194,3 @@ options.redirects++; | ||
res.on('error', function (err) { | ||
res.on('error', err => { | ||
if (finished) { | ||
@@ -206,3 +206,3 @@ return; | ||
res.pipe(inflate).pipe(fetchRes); | ||
inflate.on('error', function (err) { | ||
inflate.on('error', err => { | ||
if (finished) { | ||
@@ -220,3 +220,3 @@ return; | ||
setImmediate(function () { | ||
setImmediate(() => { | ||
if (body) { | ||
@@ -223,0 +223,0 @@ try { |
{ | ||
"name": "nodemailer-fetch", | ||
"version": "1.6.0", | ||
"description": "GET HTTP contents", | ||
"main": "lib/fetch.js", | ||
"scripts": { | ||
"test": "grunt mochaTest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nodemailer/nodemailer-fetch.git" | ||
}, | ||
"keywords": [ | ||
"nodemailer", | ||
"http" | ||
], | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"grunt": "^1.0.1", | ||
"grunt-eslint": "^19.0.0", | ||
"grunt-mocha-test": "^0.12.7", | ||
"mocha": "^3.0.2" | ||
}, | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nodemailer/nodemailer-fetch/issues" | ||
}, | ||
"homepage": "https://github.com/nodemailer/nodemailer-fetch#readme" | ||
"name": "nodemailer-fetch", | ||
"version": "2.0.0", | ||
"description": "GET HTTP contents", | ||
"main": "lib/fetch.js", | ||
"scripts": { | ||
"test": "grunt mochaTest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nodemailer/nodemailer-fetch.git" | ||
}, | ||
"keywords": [ | ||
"nodemailer", | ||
"http" | ||
], | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"grunt": "^1.0.1", | ||
"grunt-cli": "^1.2.0", | ||
"grunt-eslint": "^19.0.0", | ||
"grunt-mocha-test": "^0.13.2", | ||
"mocha": "^3.1.0" | ||
}, | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nodemailer/nodemailer-fetch/issues" | ||
}, | ||
"homepage": "https://github.com/nodemailer/nodemailer-fetch#readme" | ||
} |
@@ -1,2 +0,2 @@ | ||
/* eslint no-unused-expressions:0 */ | ||
/* eslint no-unused-expressions:0, prefer-arrow-callback: 0 */ | ||
/* globals beforeEach, describe, it */ | ||
@@ -6,7 +6,7 @@ | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
//var http = require('http'); | ||
var Cookies = require('../lib/cookies'); | ||
//let http = require('http'); | ||
const Cookies = require('../lib/cookies'); | ||
@@ -16,3 +16,3 @@ chai.config.includeStack = true; | ||
describe('Cookies Unit Tests', function () { | ||
var biskviit; | ||
let biskviit; | ||
@@ -178,3 +178,3 @@ beforeEach(function () { | ||
it('should check if a cookie matches particular domain and path', function () { | ||
var cookie = { | ||
let cookie = { | ||
name: 'zzz', | ||
@@ -193,3 +193,3 @@ value: 'abc', | ||
it('should check if a cookie matches particular domain and path', function () { | ||
var cookie = { | ||
let cookie = { | ||
name: 'zzz', | ||
@@ -208,3 +208,3 @@ value: 'abc', | ||
it('should check if a cookie is secure', function () { | ||
var cookie = { | ||
let cookie = { | ||
name: 'zzz', | ||
@@ -211,0 +211,0 @@ value: 'abc', |
@@ -1,2 +0,2 @@ | ||
/* eslint no-unused-expressions:0 */ | ||
/* eslint no-unused-expressions:0, prefer-arrow-callback:0 */ | ||
/* globals afterEach, beforeEach, describe, it */ | ||
@@ -6,18 +6,18 @@ | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
//var http = require('http'); | ||
var fetch = require('../lib/fetch'); | ||
var http = require('http'); | ||
var https = require('https'); | ||
var zlib = require('zlib'); | ||
var PassThrough = require('stream').PassThrough; | ||
//let http = require('http'); | ||
const fetch = require('../lib/fetch'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
const zlib = require('zlib'); | ||
const PassThrough = require('stream').PassThrough; | ||
chai.config.includeStack = true; | ||
var HTTP_PORT = 9998; | ||
var HTTPS_PORT = 9993; | ||
const HTTP_PORT = 9998; | ||
const HTTPS_PORT = 9993; | ||
var httpsOptions = { | ||
const httpsOptions = { | ||
key: '-----BEGIN RSA PRIVATE KEY-----\n' + | ||
@@ -70,3 +70,3 @@ 'MIIEpAIBAAKCAQEA6Z5Qqhw+oWfhtEiMHE32Ht94mwTBpAfjt3vPpX8M7DMCTwHs\n' + | ||
describe('fetch tests', function () { | ||
var httpServer, httpsServer; | ||
let httpServer, httpsServer; | ||
@@ -128,12 +128,13 @@ beforeEach(function (done) { | ||
case '/gzip': | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain', | ||
'Content-Encoding': 'gzip' | ||
}); | ||
{ | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain', | ||
'Content-Encoding': 'gzip' | ||
}); | ||
var stream = zlib.createGzip(); | ||
stream.pipe(res); | ||
stream.end('Hello World HTTP\n'); | ||
break; | ||
let stream = zlib.createGzip(); | ||
stream.pipe(res); | ||
stream.end('Hello World HTTP\n'); | ||
break; | ||
} | ||
case '/invalid': | ||
@@ -168,18 +169,19 @@ res.writeHead(500, { | ||
case '/post': | ||
var body = []; | ||
req.on('readable', function () { | ||
var chunk; | ||
while ((chunk = req.read()) !== null) { | ||
body.push(chunk); | ||
} | ||
}); | ||
req.on('end', function () { | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain' | ||
{ | ||
let body = []; | ||
req.on('readable', function () { | ||
let chunk; | ||
while ((chunk = req.read()) !== null) { | ||
body.push(chunk); | ||
} | ||
}); | ||
res.end(Buffer.concat(body)); | ||
}); | ||
req.on('end', function () { | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(Buffer.concat(body)); | ||
}); | ||
break; | ||
break; | ||
} | ||
default: | ||
@@ -212,4 +214,4 @@ res.writeHead(200, { | ||
it('should fetch HTTP data', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT); | ||
var buf = []; | ||
let req = fetch('http://localhost:' + HTTP_PORT); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -225,4 +227,4 @@ buf.push(chunk); | ||
it('should fetch HTTPS data', function (done) { | ||
var req = fetch('https://localhost:' + HTTPS_PORT); | ||
var buf = []; | ||
let req = fetch('https://localhost:' + HTTPS_PORT); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -238,4 +240,4 @@ buf.push(chunk); | ||
it('should fetch HTTP data with redirects', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/redirect3'); | ||
var buf = []; | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/redirect3'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -251,4 +253,4 @@ buf.push(chunk); | ||
it('should return error for too many redirects', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/redirect6'); | ||
var buf = []; | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/redirect6'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -265,6 +267,6 @@ buf.push(chunk); | ||
it('should fetch HTTP data with custom redirect limit', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/redirect3', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/redirect3', { | ||
maxRedirects: 3 | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -280,6 +282,6 @@ buf.push(chunk); | ||
it('should return error for custom redirect limit', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/redirect3', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/redirect3', { | ||
maxRedirects: 2 | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -296,6 +298,6 @@ buf.push(chunk); | ||
it('should return disable redirects', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/redirect1', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/redirect1', { | ||
maxRedirects: 0 | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -312,4 +314,4 @@ buf.push(chunk); | ||
it('should unzip compressed HTTP data', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/gzip'); | ||
var buf = []; | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/gzip'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -325,4 +327,4 @@ buf.push(chunk); | ||
it('should return error for unresolved host', function (done) { | ||
var req = fetch('http://asfhaskhhgbjdsfhgbsdjgk'); | ||
var buf = []; | ||
let req = fetch('http://asfhaskhhgbjdsfhgbsdjgk'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -339,4 +341,4 @@ buf.push(chunk); | ||
it('should return error for invalid status', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/invalid'); | ||
var buf = []; | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/invalid'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -353,4 +355,4 @@ buf.push(chunk); | ||
it('should return error for invalid url', function (done) { | ||
var req = fetch('http://localhost:99999999/'); | ||
var buf = []; | ||
let req = fetch('http://localhost:99999999/'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -367,6 +369,6 @@ buf.push(chunk); | ||
it('should return timeout error', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/forever', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/forever', { | ||
timeout: 1000 | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -383,4 +385,4 @@ buf.push(chunk); | ||
it('should handle basic HTTP auth', function (done) { | ||
var req = fetch('http://user:pass@localhost:' + HTTP_PORT + '/auth'); | ||
var buf = []; | ||
let req = fetch('http://user:pass@localhost:' + HTTP_PORT + '/auth'); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -398,4 +400,4 @@ buf.push(chunk); | ||
it('should return error for invalid protocol', function (done) { | ||
var req = fetch('http://localhost:' + HTTPS_PORT); | ||
var buf = []; | ||
let req = fetch('http://localhost:' + HTTPS_PORT); | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -413,6 +415,6 @@ buf.push(chunk); | ||
it('should set cookie value', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/cookie', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/cookie', { | ||
cookie: 'test=pest' | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -428,6 +430,6 @@ buf.push(chunk); | ||
it('should set user agent', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/ua', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/ua', { | ||
userAgent: 'nodemailer-fetch' | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -443,3 +445,3 @@ buf.push(chunk); | ||
it('should post data', function (done) { | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/post', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/post', { | ||
method: 'post', | ||
@@ -451,3 +453,3 @@ body: { | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -463,10 +465,10 @@ buf.push(chunk); | ||
it('should post stream data', function (done) { | ||
var body = new PassThrough(); | ||
var data = new Buffer('hello=world%20%F0%9F%98%AD&another=value'); | ||
let body = new PassThrough(); | ||
let data = new Buffer('hello=world%20%F0%9F%98%AD&another=value'); | ||
var req = fetch('http://localhost:' + HTTP_PORT + '/post', { | ||
let req = fetch('http://localhost:' + HTTP_PORT + '/post', { | ||
method: 'post', | ||
body: body | ||
body | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -480,8 +482,8 @@ buf.push(chunk); | ||
var pos = 0; | ||
var writeNext = function () { | ||
let pos = 0; | ||
let writeNext = function () { | ||
if (pos >= data.length) { | ||
return body.end(); | ||
} | ||
var char = data.slice(pos++, pos); | ||
let char = data.slice(pos++, pos); | ||
body.write(char); | ||
@@ -495,3 +497,3 @@ setImmediate(writeNext); | ||
it('should return error for invalid cert', function (done) { | ||
var req = fetch('https://localhost:' + HTTPS_PORT, { | ||
let req = fetch('https://localhost:' + HTTPS_PORT, { | ||
tls: { | ||
@@ -501,3 +503,3 @@ rejectUnauthorized: true | ||
}); | ||
var buf = []; | ||
let buf = []; | ||
req.on('data', function (chunk) { | ||
@@ -504,0 +506,0 @@ buf.push(chunk); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51565
1307
6