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

Merge pull request #2044 from node-red-hitachi/cookie_encoding

Allow http request node to avoid encoding cookie
This commit is contained in:
Nick O'Leary 2019-02-04 20:39:05 +00:00 committed by GitHub
commit 044ad77a4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 17 deletions

View File

@ -148,16 +148,9 @@ module.exports = function(RED) {
};
}
if (opts.headers.hasOwnProperty('cookie')) {
var cookies = cookie.parse(opts.headers.cookie);
var cookies = cookie.parse(opts.headers.cookie, {decode:String});
for (var name in cookies) {
if (cookies.hasOwnProperty(name)) {
if (cookies[name] === null) {
// This case clears a cookie for HTTP In/Response nodes.
// Ignore for this node.
} else {
opts.jar.setCookie(name + '=' + cookies[name], url);
}
}
opts.jar.setCookie(cookie.serialize(name, cookies[name], {encode:String}), url);
}
delete opts.headers.cookie;
}
@ -168,9 +161,15 @@ module.exports = function(RED) {
// This case clears a cookie for HTTP In/Response nodes.
// Ignore for this node.
} else if (typeof msg.cookies[name] === 'object') {
opts.jar.setCookie(name + '=' + msg.cookies[name].value, url);
if(msg.cookies[name].encode === false){
// If the encode option is false, the value is not encoded.
opts.jar.setCookie(cookie.serialize(name, msg.cookies[name].value, {encode: String}), url);
} else {
// The value is encoded by encodeURIComponent().
opts.jar.setCookie(cookie.serialize(name, msg.cookies[name].value), url);
}
} else {
opts.jar.setCookie(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) {
testApp = express();
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('/text', function(req, res){ res.send('hello'); });
testApp.get('/redirectToText', function(req, res){ res.status(302).set('Location', getTestURL('/text')).end(); });
@ -138,8 +138,7 @@ describe('HTTP Request Node', function() {
}, 50);
});
testApp.get('/checkCookie', function(req, res){
var value = req.cookies.data;
res.send(value);
res.send(req.cookies);
});
testApp.get('/setCookie', function(req, res){
res.cookie('data','hello');
@ -1001,7 +1000,7 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('payload','abc');
msg.payload.should.have.property('data','abc');
msg.should.have.property('statusCode',200);
done();
} 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) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/checkCookie')},
{id:"n2", type:"helper"}];
@ -1020,7 +1039,7 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('payload','abc');
msg.payload.should.have.property('data','abc');
msg.should.have.property('statusCode',200);
done();
} 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, encode:true}}});
});
});
it('should not encode cookie when encode option is false', 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',value);
msg.should.have.property('statusCode',200);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"foo", cookies:{data:{value:value, encode:false}}});
});
});
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')},
{id:"n2", type:"helper"}];
@ -1039,7 +1138,7 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('payload','abc');
msg.payload.should.have.property('data','abc');
msg.should.have.property('statusCode',200);
done();
} 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) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}];