1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow http request node to change cookie value encoding

This commit is contained in:
Hiroki Uchikawa 2019-01-30 19:33:23 +09:00
parent 2037741b54
commit 7c6eb7c794
2 changed files with 128 additions and 7 deletions

View File

@ -161,7 +161,9 @@ module.exports = function(RED) {
// This case clears a cookie for HTTP In/Response nodes. // This case clears a cookie for HTTP In/Response nodes.
// Ignore for this node. // Ignore for this node.
} else if (typeof msg.cookies[name] === 'object') { } else if (typeof msg.cookies[name] === 'object') {
opts.jar.setCookie(cookie.serialize(name, msg.cookies[name].value), url); // If the encode option is specified, pass it.
var options = msg.cookies[name].encode ? {encode: msg.cookies[name].encode} : undefined;
opts.jar.setCookie(cookie.serialize(name, msg.cookies[name].value, options), url);
} else { } else {
opts.jar.setCookie(cookie.serialize(name, msg.cookies[name]), url); opts.jar.setCookie(cookie.serialize(name, msg.cookies[name]), url);
} }

View File

@ -120,7 +120,7 @@ describe('HTTP Request Node', function() {
before(function(done) { before(function(done) {
testApp = express(); testApp = express();
testApp.use(bodyParser.raw({type:"*/*"})); testApp.use(bodyParser.raw({type:"*/*"}));
testApp.use(cookieParser()); testApp.use(cookieParser(undefined,{decode:String}));
testApp.get('/statusCode204', function(req,res) { res.status(204).end();}); testApp.get('/statusCode204', function(req,res) { res.status(204).end();});
testApp.get('/text', function(req, res){ res.send('hello'); }); testApp.get('/text', function(req, res){ res.send('hello'); });
testApp.get('/redirectToText', function(req, res){ res.status(302).set('Location', getTestURL('/text')).end(); }); testApp.get('/redirectToText', function(req, res){ res.status(302).set('Location', getTestURL('/text')).end(); });
@ -138,8 +138,7 @@ describe('HTTP Request Node', function() {
}, 50); }, 50);
}); });
testApp.get('/checkCookie', function(req, res){ testApp.get('/checkCookie', function(req, res){
var value = req.cookies.data; res.send(req.cookies);
res.send(value);
}); });
testApp.get('/setCookie', function(req, res){ testApp.get('/setCookie', function(req, res){
res.cookie('data','hello'); res.cookie('data','hello');
@ -1001,7 +1000,7 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload','abc'); msg.payload.should.have.property('data','abc');
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
done(); done();
} catch(err) { } catch(err) {
@ -1012,6 +1011,26 @@ describe('HTTP Request Node', function() {
}); });
}); });
it('should send multiple cookies with string', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.payload.should.have.property('data','abc');
msg.payload.should.have.property('foo','bar');
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{data:'abc',foo:'bar'}});
});
});
it('should send cookie with object data', function(done) { it('should send cookie with object data', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
@ -1020,7 +1039,7 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload','abc'); msg.payload.should.have.property('data','abc');
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
done(); done();
} catch(err) { } catch(err) {
@ -1031,6 +1050,86 @@ describe('HTTP Request Node', function() {
}); });
}); });
it('should send multiple cookies with object data', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.payload.should.have.property('data','abc');
msg.payload.should.have.property('foo','bar');
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{data:{value:'abc'},foo:{value:'bar'}}});
});
});
it('should encode cookie value', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var value = ';,/?:@ &=+$#';
n2.on("input", function(msg) {
try {
msg.payload.should.have.property('data',encodeURIComponent(value));
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{data:value}});
});
});
it('should encode cookie object', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var value = ';,/?:@ &=+$#';
n2.on("input", function(msg) {
try {
msg.payload.should.have.property('data',encodeURIComponent(value));
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{data:{value:value}}});
});
});
it('should encode cookie by specified function', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var value = ',/?:@ &+$#';
n2.on("input", function(msg) {
try {
msg.payload.should.have.property('data',encodeURI(value));
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{data:{value:value,encode:encodeURI}}});
});
});
it('should send cookie by msg.headers', function(done) { it('should send cookie by msg.headers', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
@ -1039,7 +1138,7 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload','abc'); msg.payload.should.have.property('data','abc');
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
done(); done();
} catch(err) { } catch(err) {
@ -1050,6 +1149,26 @@ describe('HTTP Request Node', function() {
}); });
}); });
it('should send multiple cookies by msg.headers', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.payload.should.have.property('data','abc');
msg.payload.should.have.property('foo','bar');
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{boo:'123'}, headers:{'cookie':'data=abc; foo=bar;'}});
});
});
it('should convert all HTTP headers into lower case', function(done) { it('should convert all HTTP headers into lower case', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];