使用Node.js连接到PHP网站并保持会话
为了测试一个PHP网站,我正在用Test'em和Mocha(在node.js上运行)制作一个测试台 我想要的是请求一些URL(例如)并获取http状态码以及返回的内容 我正在使用node.jsRequest模块进行此操作 问题是:使用Node.js连接到PHP网站并保持会话,node.js,authentication,session-cookies,mocha.js,testem,Node.js,Authentication,Session Cookies,Mocha.js,Testem,为了测试一个PHP网站,我正在用Test'em和Mocha(在node.js上运行)制作一个测试台 我想要的是请求一些URL(例如)并获取http状态码以及返回的内容 我正在使用node.jsRequest模块进行此操作 问题是: var params = { email: 'your_username', password: 'your_password' }; var paramsString = JSON.stringify(params); // Login to th
var params = {
email: 'your_username',
password: 'your_password'
};
var paramsString = JSON.stringify(params);
// Login to the application
request.post('http://localhost/biings/front-end/rest/auth',
{
headers: {
"Content-Type" : "application/json",
'Content-Length' : paramsString.length
},
body: paramsString,
},function (error, response, body) {
// get the PHPSESSID (the last one) that is returned in the header. Sometimes more than one is returned
var sessionCookie = response.headers['set-cookie'][response.headers['set-cookie'].length - 1];
sessionCookie = sessionCookie.split(';');
sessionCookie = sessionCookie[0];
// Write it in a file (this is a quick trick to access it globally)
// e.g.: PHPSESSID=ao9a1j0timv9nmuj2ntt363d92 (write it simply as a string)
fs.writeFile('sessionCookie.txt', sessionCookie, function (err)
{
if(err)
{
return console.log(err);
}
});
});
// don't care about this it() function (it's for Mocha)
it("test 1", function(done)
{
// Get the cookie
fs.readFile('sessionCookie.txt','utf8', function (err, data)
{
if(err)
{
throw err;
}
else
{
// Launch a request that includes the cookie in the header
request.get('http://localhost/biings/front-end/rest/group',
{
headers: {"Cookie" : data},
}, function (error, response, body) {
// Check your request reaches the right page
expect(response.statusCode).equals(200);
console.log(body);
done();
});
}
});
});
我需要通过身份验证才能访问此页面,否则我将
已重定向到登录页面
那么,是否存在一种通过Node.js登录我的应用程序并保持会话打开的方法,以便能够在我想要的任何页面上链接测试
如果可能的话,我想在登录请求时获取PHPSESSID。你认为这是一个好的方向吗
任何帮助都将不胜感激
谢谢,祝你今天愉快:)
Michaël如果您在自己的应用程序中设置了
jar:true
,或者使用自己的,那么request
将记住服务器设置的cookies,以便您可以在请求之间保持会话。使用和等无头浏览器,而不是使用请求模块。您甚至可以使用这些工具模拟用户交互。msdex感谢您的回答!但不幸的是,这对我不起作用:/
hyubs也感谢你
最后,我继续使用Mocha+请求
基本上我做的是:
var params = {
email: 'your_username',
password: 'your_password'
};
var paramsString = JSON.stringify(params);
// Login to the application
request.post('http://localhost/biings/front-end/rest/auth',
{
headers: {
"Content-Type" : "application/json",
'Content-Length' : paramsString.length
},
body: paramsString,
},function (error, response, body) {
// get the PHPSESSID (the last one) that is returned in the header. Sometimes more than one is returned
var sessionCookie = response.headers['set-cookie'][response.headers['set-cookie'].length - 1];
sessionCookie = sessionCookie.split(';');
sessionCookie = sessionCookie[0];
// Write it in a file (this is a quick trick to access it globally)
// e.g.: PHPSESSID=ao9a1j0timv9nmuj2ntt363d92 (write it simply as a string)
fs.writeFile('sessionCookie.txt', sessionCookie, function (err)
{
if(err)
{
return console.log(err);
}
});
});
// don't care about this it() function (it's for Mocha)
it("test 1", function(done)
{
// Get the cookie
fs.readFile('sessionCookie.txt','utf8', function (err, data)
{
if(err)
{
throw err;
}
else
{
// Launch a request that includes the cookie in the header
request.get('http://localhost/biings/front-end/rest/group',
{
headers: {"Cookie" : data},
}, function (error, response, body) {
// Check your request reaches the right page
expect(response.statusCode).equals(200);
console.log(body);
done();
});
}
});
});
var params = {
email: 'your_username',
password: 'your_password'
};
var paramsString = JSON.stringify(params);
// Login to the application
request.post('http://localhost/biings/front-end/rest/auth',
{
headers: {
"Content-Type" : "application/json",
'Content-Length' : paramsString.length
},
body: paramsString,
},function (error, response, body) {
// get the PHPSESSID (the last one) that is returned in the header. Sometimes more than one is returned
var sessionCookie = response.headers['set-cookie'][response.headers['set-cookie'].length - 1];
sessionCookie = sessionCookie.split(';');
sessionCookie = sessionCookie[0];
// Write it in a file (this is a quick trick to access it globally)
// e.g.: PHPSESSID=ao9a1j0timv9nmuj2ntt363d92 (write it simply as a string)
fs.writeFile('sessionCookie.txt', sessionCookie, function (err)
{
if(err)
{
return console.log(err);
}
});
});
// don't care about this it() function (it's for Mocha)
it("test 1", function(done)
{
// Get the cookie
fs.readFile('sessionCookie.txt','utf8', function (err, data)
{
if(err)
{
throw err;
}
else
{
// Launch a request that includes the cookie in the header
request.get('http://localhost/biings/front-end/rest/group',
{
headers: {"Cookie" : data},
}, function (error, response, body) {
// Check your request reaches the right page
expect(response.statusCode).equals(200);
console.log(body);
done();
});
}
});
});
它对我来说很有魅力
如果发现问题或可以优化,请告诉我:)
Michaël在我的例子中,除了设置
jar:true
之外,我还必须设置followAllRedirects:true
,以便将身份验证传递给后续请求