Skip to content

Commit

Permalink
(#6814) - Add support to store cookies on Node.js
Browse files Browse the repository at this point in the history
* (#6814) - Add support to store cookies on Node.js

On Node.js, PouchDB does not hold a cookie jar for cookies received
from server during authentication. It uses the request Node.js module
to make HTTP requests but it does not configure it correctly to hold
a cookie jar.

This adds the minimal required configuration for the request module.

* (#6814) - Add a test for support for cookies on Node.js

* (#6814) - Fix and move test for support for cookies on Node.js
  • Loading branch information
ptitjes authored and daleharvey committed Nov 14, 2017
1 parent 200cc2f commit 17db84f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/node_modules/pouchdb-ajax/src/request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// May seem redundant, but this is to allow switching with
// request-browser.js.
export default require('request');
export default require('request').defaults({
jar: true
});
39 changes: 39 additions & 0 deletions tests/component/test.ajax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

var http = require('http');
var PouchDB = require('../../packages/node_modules/pouchdb-for-coverage');
var should = require("chai").should();

describe('test.ajax.js', function () {

it('#6815 ajax uses cookies', function (done) {
var server = http.createServer(function (req, res) {
if (req.url === '/install-cookie') {
res.writeHead(200, {'Set-Cookie': 'Test=test; Version=1; Path=/; HttpOnly'});
res.end(JSON.stringify({ok: true}));
} else if (req.url === '/check-cookie') {
res.end(JSON.stringify({ok: req.headers.cookie === 'Test=test'}));
}
});
server.listen(6000, function () {
PouchDB.ajax({
method: 'GET',
url: 'http://127.0.0.1:6000/install-cookie',
timeout: 10
}, function (err, res) {
should.equal(res.ok, true, "Server not responding");

PouchDB.ajax({
method: 'GET',
url: 'http://127.0.0.1:6000/check-cookie',
timeout: 10
}, function (err, res) {
server.close();

should.equal(res.ok, true, "Cookie not set");
done();
});
});
});
});
});

0 comments on commit 17db84f

Please sign in to comment.