使用Node.js连接到PHP网站并保持会话状态 [英] Connecting to a PHP website with Node.js and keep session

查看:90
本文介绍了使用Node.js连接到PHP网站并保持会话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Test'em和Mocha(运行在node.js上)进行测试,以测试PHP网站.

I'm making a testbench with Test'em and Mocha (that run on node.js) in order to test a PHP website.

我想要的是请求一些URL(例如, http://www.my-website/test. php )并获取http状态代码以及返回的内容.

What I want is to request some URL (e.g http://www.my-website/test.php) and get the http status code as well as the content returned.

我正在使用node.js Request 模块.

I'm doing it with the node.js Request module.

问题是:

我需要通过身份验证才能访问此页面,否则我 重定向到登录页面.

I need to be authenticated to access this page, otherwise I'm redirected to the login page.

那么,是否存在一种通过Node.js登录我的应用程序并保持会话打开以能够在我想要的任何页面上进行链式测试的方法?

So, does it exist a way to log in my application through Node.js and keep the session open to be able to chain tests on any pages I want?

我在考虑是否可以在登录请求时获取PHPSESSID.你觉得这是个好方向吗?

I was thinking on get the PHPSESSID on login request if it is possible. Do you thing it is a good direction ?

任何帮助将不胜感激.

谢谢,祝你有美好的一天:)

Thank you, have a nice day :)

Michaël

推荐答案

mscdex,谢谢您的回答!但不幸的是,它对我不起作用:/

mscdex thanks for your answer! But unfortunately it did not work for me :/

也感谢你.

最后,我继续使用Mocha + Request.

Finally I carried on to use Mocha + Request.

我所做的基本上是:

  1. 通过POST请求连接到登录页面,并获取在响应标头中返回的PHPSESSID cookie.

  1. Connect through a POST request to the login page and get the PHPSESSID cookie that is returned in the response header.

在下一个针对您必须登录的URL的下一个请求中,在标题中传递cookie.

Pass the cookie in the header in the next requests that target a URL where you have to be logged.

这是我的代码:

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();
         });
        }
    }); 
});

对我来说,它就像是一种魅力.

It works like a charm for me.

如果您发现有问题或可以对其进行优化,请告诉我:)

Tell me if you see something wrong or which could be optimized :)

Michaël

这篇关于使用Node.js连接到PHP网站并保持会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆