Просмотр исходного кода

Merge remote-tracking branch 'remotes/origin/cookies-in-response'

Andrius Petrauskis 8 лет назад
Родитель
Сommit
968690155f

+ 19 - 0
src/JonnyW/PhantomJs/Http/Response.php

@@ -82,6 +82,14 @@ class Response
      */
     public $console;
 
+    /**
+     * Session cookies
+     *
+     * @var array
+     * @access public
+     */
+    public $cookies;
+
     /**
      * Import response data
      *
@@ -247,4 +255,15 @@ class Response
     {
         return $this->console;
     }
+
+    /**
+     * Get session cookies
+     *
+     * @access public
+     * @return array
+     */
+    public function getCookies()
+    {
+        return $this->cookies;
+    }
 }

+ 8 - 0
src/JonnyW/PhantomJs/Http/ResponseInterface.php

@@ -96,4 +96,12 @@ interface ResponseInterface
      * @return string
      */
     public function getTime();
+
+    /**
+     * Get session cookies
+     *
+     * @access public
+     * @return array
+     */
+    public function getCookies();
 }

+ 1 - 0
src/JonnyW/PhantomJs/Resources/procedures/phantom_on_error.partial

@@ -8,6 +8,7 @@ trace.forEach(function(t) {
 response.status  = 500;
 response.content = msg;
 response.console = stack;
+response.cookies = phantom.cookies;
 
 system.stdout.write(JSON.stringify(response, undefined, 4));
 phantom.exit(1);

+ 1 - 0
src/JonnyW/PhantomJs/Resources/procedures/procedure_capture.partial

@@ -20,6 +20,7 @@ if (status === 'success') {
 }
 
 response.console = logs;
+response.cookies = phantom.cookies;
 
 system.stderr.write(debug.join('\\n') + '\\n');
 system.stdout.write(JSON.stringify(response, undefined, 4));

+ 1 - 0
src/JonnyW/PhantomJs/Resources/procedures/procedure_default.partial

@@ -15,6 +15,7 @@ if (status === 'success') {
 }
 
 response.console = logs;
+response.cookies = phantom.cookies;
 
 system.stderr.write(debug.join('\\n') + '\\n');
 system.stdout.write(JSON.stringify(response, undefined, 4));

+ 1 - 0
src/JonnyW/PhantomJs/Resources/procedures/procedure_pdf.partial

@@ -17,6 +17,7 @@ if (status === 'success') {
 }
 
 response.console = logs;
+response.cookies = phantom.cookies;
 
 system.stderr.write(debug.join('\\n') + '\\n');
 system.stdout.write(JSON.stringify(response, undefined, 4));

+ 36 - 0
src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

@@ -359,6 +359,42 @@ EOF;
         $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 can load cookies from
+     * persistent cookie file
+     *
+     * @access public
+     * @return void
+     */
+    public function testCookiesPresentInResponse()
+    {
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $expireAt = strtotime('16-Nov-2020 00:00:00');
+
+        $request->setMethod('GET');
+        $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
+        $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi', true, false, ($expireAt * 1000));
+
+        $client->send($request, $response);
+
+        $cookies = $response->getCookies();
+        $this->assertEquals(array(
+            'domain' => '.jonnyw.kiwi',
+            'expires' => 'Mon, 16 Nov 2020 00:00:00 GMT',
+            'expiry' => '1605484800',
+            'httponly' => true,
+            'name' => 'test_cookie',
+            'path' => '/',
+            'secure' => false,
+            'value' => 'TESTING_COOKIES',
+        ), $cookies[0]);
+    }
+
     /**
      * Test response contains console error if a
      * javascript error exists on the page.

+ 19 - 0
src/JonnyW/PhantomJs/Tests/Unit/Http/ResponseTest.php

@@ -368,6 +368,25 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($response->isRedirect());
     }
 
+    /**
+     * Test if cookies can be parsed and imported
+     *
+     * @access public
+     * @return void
+     */
+    public function testCookiesCanBeImported()
+    {
+        $cookie = 'cookie=TESTING; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/';
+        $data = array(
+            'cookies' => array($cookie)
+        );
+
+        $response = $this->getResponse();
+        $response->import($data);
+
+        $this->assertContains($cookie, $response->getCookies());
+    }
+
 /** +++++++++++++++++++++++++++++++++++ **/
 /** ++++++++++ TEST ENTITIES ++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/