Comparing version 0.3.8 to 0.3.9
{ | ||
"name": "popsicle", | ||
"main": "popsicle.js", | ||
"version": "0.3.8", | ||
"version": "0.3.9", | ||
"homepage": "https://github.com/blakeembrey/popsicle", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "popsicle", | ||
"version": "0.3.8", | ||
"version": "0.3.9", | ||
"description": "Simple HTTP requests for node and the browser", | ||
@@ -5,0 +5,0 @@ "main": "popsicle.js", |
@@ -648,2 +648,5 @@ (function () { | ||
this.timeout = options.timeout; | ||
// Node specific options. | ||
this.jar = options.jar; | ||
this.withCredentials = options.withCredentials === true; | ||
@@ -847,3 +850,3 @@ this.rejectUnauthorized = options.rejectUnauthorized !== false; | ||
if (!this._promise) { | ||
// If already aborted, set the promise to be rejected. | ||
// If already aborted, create a rejected promise. | ||
if (this.aborted) { | ||
@@ -943,3 +946,3 @@ this._promise = Promise.reject(abortError(this)); | ||
var request = require('request'); | ||
var pkg = require('./package.json'); | ||
var version = require('./package.json').version; | ||
@@ -957,6 +960,7 @@ /** | ||
request.method = self.method; | ||
request.jar = self.jar; | ||
// Set a default user-agent. | ||
request.headers = assign(self.headers, { | ||
'User-Agent': 'node-popsicle/' + pkg.version | ||
'User-Agent': 'node-popsicle/' + version | ||
}); | ||
@@ -1005,9 +1009,9 @@ | ||
function onRequest () { | ||
var write = request.req.write; | ||
function onRequest (request) { | ||
var write = request.write; | ||
self.uploadTotal = num(request.headers['content-length']); | ||
self.uploadTotal = num(request.getHeader('Content-Length')); | ||
// Override `Request.prototype.write` to track amount of sent data. | ||
request.req.write = function (data) { | ||
request.write = function (data) { | ||
self._setUploadSize(self.uploadSize + byteLength(data)); | ||
@@ -1020,2 +1024,3 @@ | ||
function onResponse (response) { | ||
response.on('data', onResponseData); | ||
self.downloadTotal = num(response.headers['content-length']); | ||
@@ -1030,20 +1035,8 @@ self._uploadFinished(); | ||
function onResponseEnd () { | ||
removeListeners(); | ||
self._downloadFinished(); | ||
} | ||
request.on('redirect', function () { | ||
console.log(request.redirects); | ||
}); | ||
function removeListeners () { | ||
request.removeListener('request', onRequest); | ||
request.removeListener('response', onResponse); | ||
request.removeListener('data', onResponseData); | ||
request.removeListener('end', onResponseEnd); | ||
request.removeListener('error', removeListeners); | ||
} | ||
request.on('request', onRequest); | ||
request.on('response', onResponse); | ||
request.on('data', onResponseData); | ||
request.on('end', onResponseEnd); | ||
request.on('error', removeListeners); | ||
}; | ||
@@ -1089,2 +1082,6 @@ | ||
var req = request(opts, function (err, response) { | ||
// Clean up listeners. | ||
delete self._request; | ||
self._downloadFinished(); | ||
if (err) { | ||
@@ -1247,4 +1244,3 @@ // Node.js core error (ECONNRESET, EPIPE). | ||
// Clean up listeners. | ||
delete xhr.onreadystatechange; | ||
delete xhr.upload.onprogress; | ||
delete self._xhr; | ||
self._downloadFinished(); | ||
@@ -1364,2 +1360,17 @@ | ||
/** | ||
* Support cookie jars (on Node). | ||
* | ||
* @return {Object} | ||
*/ | ||
if (isNode) { | ||
popsicle.jar = function () { | ||
return request.jar(); | ||
}; | ||
} else { | ||
popsicle.jar = function () { | ||
throw new Error('Cookie jars are not supported on browsers'); | ||
}; | ||
} | ||
/** | ||
* Alias `Request` and `Response` constructors. | ||
@@ -1366,0 +1377,0 @@ */ |
@@ -72,2 +72,11 @@ # ![Popsicle](https://cdn.rawgit.com/blakeembrey/popsicle/master/logo.svg) | ||
**Node only** | ||
* **jar** An instance of a cookie jar (default: `null`) | ||
* **rejectUnauthorized** Reject invalid SSL certificates (default: `true`) | ||
**Browser only** | ||
* **withCredentials** Send cookies with CORS requests (default: `false`) | ||
#### Automatically Serializing Body | ||
@@ -92,3 +101,3 @@ | ||
You can manually create a form data instance by calling `popsicle.form`. When you pass a form data instance, it'll automatically set the correct `Content-Type` too - complete with the boundary. | ||
You can manually create a form data instance by calling `popsicle.form`. When you pass a form data instance, it'll automatically set the correct `Content-Type` - complete with boundary. | ||
@@ -197,2 +206,16 @@ ```javascript | ||
#### Cookie Jar (Node only) | ||
You can create a reusable cookie jar instance for requests by calling `popsicle.jar`. | ||
```javascript | ||
var jar = request.jar(); | ||
request({ | ||
method: 'POST', | ||
url: '/users', | ||
jar: jar | ||
}); | ||
``` | ||
### Response Objects | ||
@@ -230,2 +253,3 @@ | ||
* [Server](https://github.com/blakeembrey/popsicle-server) - Automatically mount servers with each request for testing | ||
* [Status](https://github.com/blakeembrey/popsicle-status) - Reject responses on HTTP failure status codes | ||
@@ -232,0 +256,0 @@ * [No Cache](https://github.com/blakeembrey/popsicle-no-cache) - Prevent caching of HTTP requests |
@@ -548,2 +548,26 @@ var isNode = typeof window === 'undefined'; | ||
}); | ||
if (isNode) { | ||
describe('cookie jar', function () { | ||
it('should work with a cookie jar', function () { | ||
var jar = popsicle.jar(); | ||
return popsicle({ | ||
url: REMOTE_URL + '/cookie', | ||
jar: jar | ||
}) | ||
.then(function (res) { | ||
expect(res.get('Set-Cookie')).to.exist; | ||
return popsicle({ | ||
url: REMOTE_URL + '/echo', | ||
jar: jar | ||
}); | ||
}) | ||
.then(function (res) { | ||
expect(res.get('Cookie')).to.deep.equal('hello=world'); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
@@ -45,2 +45,9 @@ var express = require('express'); | ||
app.get('/cookie', function (req, res) { | ||
var expires = new Date(Date.now() + 10 * 60 * 60).toGMTString(); | ||
res.set('set-cookie', 'hello=world; expires=' + expires + '; path=/'); | ||
res.sendStatus(200); | ||
}); | ||
app.get('/not-found', function (req, res) { | ||
@@ -47,0 +54,0 @@ res.sendStatus(404); |
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
72044
1937
297