فهرست منبع

Added support for cookie management. Can now add and delete cookies - https://github.com/jonnnnyw/php-phantomjs/issues/124

Jonny Wenmoth 9 سال پیش
والد
کامیت
78887bafdd

+ 70 - 0
src/JonnyW/PhantomJs/Http/AbstractRequest.php

@@ -36,6 +36,14 @@ abstract class AbstractRequest
      */
     protected $settings;
 
+    /**
+     * Cookies
+     *
+     * @var array
+     * @access protected
+     */
+    protected $cookies;
+
     /**
      * Request data
      *
@@ -118,6 +126,11 @@ abstract class AbstractRequest
         $this->viewportWidth   = 0;
         $this->viewportHeight  = 0;
 
+        $this->cookies = array(
+            'add'    => array(),
+            'delete' => array()
+        );
+
         $this->setMethod($method);
         $this->setTimeout($timeout);
 
@@ -419,6 +432,63 @@ abstract class AbstractRequest
         return $this->settings;
     }
 
+    /**
+     * Add cookie.
+     *
+     * @access public
+     * @param  string                                 $name
+     * @param  mixed                                  $value
+     * @param  string                                 $path
+     * @param  string                                 $domain
+     * @param  bool                                   $httpOnly (default: true)
+     * @param  bool                                   $secure   (default: false)
+     * @param  int                                    $expires  (default: null)
+     * @return \JonnyW\PhantomJs\Http\AbstractRequest
+     */
+    public function addCookie($name, $value, $path, $domain, $httpOnly = true, $secure = false, $expires = null)
+    {
+        $filter = function ($value) {
+            return !is_null($value);
+        };
+
+        $this->cookies['add'][] = array_filter(array(
+            'name'     => $name,
+            'value'    => $value,
+            'path'     => $path,
+            'domain'   => $domain,
+            'httponly' => $httpOnly,
+            'secure'   => $secure,
+            'expires'  => $expires
+        ), $filter);
+
+        return $this;
+    }
+
+    /**
+     * Delete cookie.
+     *
+     * @access public
+     * @param  string                                 $name
+     * @return \JonnyW\PhantomJs\Http\AbstractRequest
+     */
+    public function deleteCookie($name)
+    {
+        $this->cookies['delete'][] = $name;
+
+        return $this;
+    }
+
+    /**
+     * Get cookies
+     *
+     * @access public
+     * @return array
+     */
+    public function getCookies()
+    {
+        return $this->cookies;
+    }
+
     /**
      * Set body styles
      *

+ 16 - 0
src/JonnyW/PhantomJs/Http/RequestInterface.php

@@ -184,6 +184,22 @@ interface RequestInterface
      */
     public function getHeaders();
 
+    /**
+     * Get settings
+     *
+     * @access public
+     * @return array|string
+     */
+    public function getSettings();
+
+    /**
+     * Get cookies
+     *
+     * @access public
+     * @return array|string
+     */
+    public function getCookies();
+
     /**
      * Set body styles
      *

+ 5 - 1
src/JonnyW/PhantomJs/Resources/procedures/http_default.proc

@@ -32,7 +32,6 @@ var page       = require('webpage').create(),
  */
 [[ engine.load('page_viewport_size') ]]
 
-
 /**
  * Define custom headers.
  */
@@ -43,6 +42,11 @@ var page       = require('webpage').create(),
  */
 [[ engine.load('page_settings') ]]
 
+/**
+ * Page cookies
+ */
+[[ engine.load('page_cookies') ]]
+
 /**
  * On resource timeout
  */

+ 5 - 1
src/JonnyW/PhantomJs/Resources/procedures/http_lazy.proc

@@ -34,7 +34,6 @@ var page       = require('webpage').create(),
  */
 [[ engine.load('page_viewport_size') ]]
 
-
 /**
  * Define custom headers.
  */
@@ -45,6 +44,11 @@ var page       = require('webpage').create(),
  */
 [[ engine.load('page_settings') ]]
 
+/**
+ * Page cookies
+ */
+[[ engine.load('page_cookies') ]]
+
 /**
  * On resource timeout
  */

+ 23 - 0
src/JonnyW/PhantomJs/Resources/procedures/page_cookies.partial

@@ -0,0 +1,23 @@
+
+var cookies = {{ input.getCookies()|json_encode() }};
+
+cookies.delete.forEach(function(name) {
+    
+    if(name == '*') {
+        page.clearCookies();
+        debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Deleted all cookies');
+    } else {
+        
+        if(page.deleteCookie(name)) {
+            debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Deleted cookie ' + name);
+        }
+    }
+});
+
+
+cookies.add.forEach(function(cookie) {
+    
+    if(page.addCookie(cookie)) {
+        debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Added cookie ' + cookie.name + '=' + cookie.value);
+    }
+});

+ 147 - 28
src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

@@ -157,7 +157,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
 
         $client->send($request, $response);
 
@@ -179,7 +179,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->setRequestData(array(
             'test1' => 'http://test.com',
             'test2' => 'A string with an \' ) / # some other invalid [ characters.'
@@ -205,7 +205,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
 
         $client->send($request, $response);
 
@@ -226,7 +226,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->addSetting('userAgent', 'PhantomJS TEST');
 
         $client->send($request, $response);
@@ -234,6 +234,125 @@ EOF;
         $this->assertContains('userAgent=PhantomJS TEST', $response->getContent());
     }
 
+    /**
+     * Test can add cookies to request
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanAddCookiesToRequest()
+    {
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi');
+
+        $client->send($request, $response);
+
+        $this->assertContains('cookie_test_cookie=TESTING_COOKIES', $response->getContent());
+    }
+
+    /**
+     * Test can load cookies from
+     * persistent cookie file
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanLoadCookiesFromPersistentCookieFile()
+    {
+        $this->filename = 'cookies.txt';
+        $file = ($this->directory . '/' . $this->filename);
+
+        $client = $this->getClient();
+        $client->getEngine()->addOption('--cookies-file=' . $file);
+
+        $request  = $client->getMessageFactory()->createRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi', true, false, ((new \DateTime('16-Nov-2020 00:00:00'))->getTimestamp() * 1000));
+
+        $client->send($request, $response);
+
+        $this->assertContains('test_cookie=TESTING_COOKIES; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
+    }
+
+    /**
+     * Test can delete cookie from
+     * persistent cookie file
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanDeleteCookieFromPersistentCookieFile()
+    {
+        $this->filename = 'cookies.txt';
+        $file = ($this->directory . '/' . $this->filename);
+
+        $client = $this->getClient();
+        $client->getEngine()->addOption('--cookies-file=' . $file);
+
+        $request  = $client->getMessageFactory()->createRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi', true, false, ((new \DateTime('16-Nov-2020 00:00:00'))->getTimestamp() * 1000));
+
+        $client->send($request, $response);
+
+        $request = $client->getMessageFactory()->createRequest();
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->deleteCookie('test_cookie');
+
+        $client->send($request, $response);
+
+        $this->assertNotContains('test_cookie=TESTING_COOKIES; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
+    }
+
+    /**
+     * Test can delete all cookies from
+     * persistent cookie file
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanDeleteAllCookiesFromPersistentCookieFile()
+    {
+        $this->filename = 'cookies.txt';
+        $file = ($this->directory . '/' . $this->filename);
+
+        $client = $this->getClient();
+        $client->getEngine()->addOption('--cookies-file=' . $file);
+
+        $request  = $client->getMessageFactory()->createRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->addCookie('test_cookie_1', 'TESTING_COOKIES_1', '/', '.jonnyw.kiwi', true, false, ((new \DateTime('16-Nov-2020 00:00:00'))->getTimestamp() * 1000));
+        $request->addCookie('test_cookie_2', 'TESTING_COOKIES_2', '/', '.jonnyw.kiwi', true, false, ((new \DateTime('16-Nov-2020 00:00:00'))->getTimestamp() * 1000));
+
+        $client->send($request, $response);
+
+        $request = $client->getMessageFactory()->createRequest();
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->deleteCookie('*');
+
+        $client->send($request, $response);
+
+        $this->assertNotContains('test_cookie_1=TESTING_COOKIES_1; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
+        $this->assertNotContains('test_cookie_2=TESTING_COOKIES_2; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
+    }
+
     /**
      * Test response contains console error if a
      * javascript error exists on the page.
@@ -249,7 +368,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-console-error.php');
 
         $client->send($request, $response);
 
@@ -274,7 +393,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-console-error.php');
 
         $client->send($request, $response);
 
@@ -297,7 +416,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-console-error.php');
 
         $client->send($request, $response);
 
@@ -340,7 +459,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('POST');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-post.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-post.php');
         $request->setRequestData(array(
             'test1' => 'http://test.com',
             'test2' => 'A string with an \' ) / # some other invalid [ characters.'
@@ -370,7 +489,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
 
         $client->send($request, $response);
@@ -399,7 +518,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setCaptureDimensions($width, $height);
 
@@ -429,7 +548,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
 
         $client->send($request, $response);
@@ -458,7 +577,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setPaperSize(sprintf('%scm', $width), sprintf('%scm', $height));
         $request->setMargin('0cm');
@@ -492,7 +611,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setFormat('A4');
         $request->setMargin('0cm');
@@ -526,7 +645,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setFormat('A4');
         $request->setOrientation('landscape');
@@ -561,7 +680,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setFormat('A4');
         $request->setOrientation('landscape');
@@ -597,7 +716,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setOutputFile($file);
         $request->setFormat('A4');
         $request->setOrientation('landscape');
@@ -634,7 +753,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->setViewportsize($width, $height);
 
         $client->send($request, $response);
@@ -665,7 +784,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->setViewportsize($width, $height);
 
         $client->send($request, $response);
@@ -694,7 +813,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->setDelay($delay);
 
         $client->send($request, $response);
@@ -723,7 +842,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->setDelay($delay);
 
         $client->send($request, $response);
@@ -752,7 +871,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
         $request->setDelay($delay);
 
         $client->send($request, $response);
@@ -785,7 +904,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setDelay($delay);
 
         $client->send($request, $response);
@@ -814,7 +933,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setDelay($delay);
 
         $client->send($request, $response);
@@ -843,7 +962,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setDelay($delay);
 
         $client->send($request, $response);
@@ -875,7 +994,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-lazy.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-lazy.php');
         $request->setTimeout(5000);
 
         $client->send($request, $response);
@@ -900,7 +1019,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-lazy.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-lazy.php');
         $request->setTimeout(1000);
 
         $client->send($request, $response);
@@ -924,7 +1043,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
 
         $client->send($request, $response);
 
@@ -949,7 +1068,7 @@ EOF;
         $response = $client->getMessageFactory()->createResponse();
 
         $request->setMethod('GET');
-        $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
         $request->setBodyStyles(array('backgroundColor' => 'red'));
         $request->setOutputFile($file);
 

+ 72 - 0
src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php

@@ -435,6 +435,78 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($expected, $request->getSettings());
     }
 
+    /**
+     * Test can add cookies.
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanAddCookies()
+    {
+        $name     = 'test_cookie';
+        $value    = 'TESTING_COOKIES';
+        $path     = '/';
+        $domain   = 'localhost';
+        $httpOnly =  false;
+        $secure   = true;
+        $expires  = time() + 3600;
+
+        $request = $this->getRequest();
+        $request->addCookie(
+            $name,
+            $value,
+            $path,
+            $domain,
+            $httpOnly,
+            $secure,
+            $expires
+        );
+
+        $expected = array(
+            'name'     => $name,
+            'value'    => $value,
+            'path'     => $path,
+            'domain'   => $domain,
+            'httponly' => $httpOnly,
+            'secure'   => $secure,
+            'expires'  => $expires
+        );
+
+        $this->assertEquals(array($expected), $request->getCookies()['add']);
+    }
+
+    /**
+     * Test can delete cookies.
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanDeleteCookies()
+    {
+        $name     = 'test_cookie';
+        $value    = 'TESTING_COOKIES';
+        $path     = '/';
+        $domain   = 'localhost';
+        $httpOnly =  false;
+        $secure   = true;
+        $expires  = time() + 3600;
+
+        $request = $this->getRequest();
+        $request->addCookie(
+            $name,
+            $value,
+            $path,
+            $domain,
+            $httpOnly,
+            $secure,
+            $expires
+        );
+
+        $request->deleteCookie($name);
+
+        $this->assertEquals(array($name), $request->getCookies()['delete']);
+    }
+
     /**
      * Test can set viewport width.
      *