【问题标题】:Connecting to a PHP website with Node.js and keep session使用 Node.js 连接到 PHP 网站并保持会话
【发布时间】:2014-07-15 09:46:20
【问题描述】:

我正在使用 Test'em 和 Mocha(在 node.js 上运行)制作一个测试平台,以测试 PHP 网站。

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

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

问题是:

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

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

如果可能的话,我正在考虑在登录请求中获取 PHPSESSID。你觉得这是一个好的方向吗?

任何帮助将不胜感激。

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

迈克尔

【问题讨论】:

    标签: node.js authentication session-cookies mocha.js testem


    【解决方案1】:

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

    hyubs 也谢谢你。

    最后我继续使用 Mocha + Request。

    基本上我所做的是:

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

    2. 在下一个针对您必须记录的 URL 的请求的标头中传递 cookie。

    这是我的代码:

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

    它对我来说就像一个魅力。

    如果您发现有问题或可以优化的地方,请告诉我 :)

    迈克尔

    【讨论】:

      【解决方案2】:

      不要使用请求模块,而是使用无头浏览器,例如 PhantomJSzombie.js。您甚至可以模拟用户与这些的交互。

      【讨论】:

        【解决方案3】:

        如果您在options 中设置jar: true 或使用您自己的custom cookie jar,那么request 将记住服务器设置的cookie,以便您可以在请求之间保持会话。

        【讨论】:

        • 在我的例子中,除了 jar: true 之外,我还必须设置 followAllRedirects: true 才能让身份验证通过后续请求
        猜你喜欢
        • 2011-03-31
        • 1970-01-01
        • 2012-07-21
        • 2012-12-09
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-15
        相关资源
        最近更新 更多