Connecting to a PHP website with Node.js and keep session

3.4k views Asked by At

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

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.

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

The problem is:

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

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?

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

Any help would be much appreciated.

Thank you, have a nice day :)

Michaël

3

There are 3 answers

0
Michaël On BEST ANSWER

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

hyubs thanks to you too.

Finally I carried on to use Mocha + Request.

Basically what I did is:

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

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

Here is my code :

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

1
mscdex On

If you set jar: true in your options or use your own custom cookie jar, then request will remember cookies set by the server so that you can keep your session between requests.

0
hyubs On

Instead of using the request module, use headless browsers like PhantomJS and zombie.js. You can even emulate user interaction with these.