diff --git a/src/JonnyW/PhantomJs/Http/Response.php b/src/JonnyW/PhantomJs/Http/Response.php index 9ec5291..ea36cef 100644 --- a/src/JonnyW/PhantomJs/Http/Response.php +++ b/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 @@ public function getConsole() { return $this->console; } + + /** + * Get session cookies + * + * @access public + * @return array + */ + public function getCookies() + { + return $this->cookies; + } } diff --git a/src/JonnyW/PhantomJs/Http/ResponseInterface.php b/src/JonnyW/PhantomJs/Http/ResponseInterface.php index ea693dd..1908ce7 100644 --- a/src/JonnyW/PhantomJs/Http/ResponseInterface.php +++ b/src/JonnyW/PhantomJs/Http/ResponseInterface.php @@ -96,4 +96,12 @@ public function isRedirect(); * @return string */ public function getTime(); + + /** + * Get session cookies + * + * @access public + * @return array + */ + public function getCookies(); } diff --git a/src/JonnyW/PhantomJs/Resources/procedures/phantom_on_error.partial b/src/JonnyW/PhantomJs/Resources/procedures/phantom_on_error.partial index 2d1191c..13ba163 100644 --- a/src/JonnyW/PhantomJs/Resources/procedures/phantom_on_error.partial +++ b/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); diff --git a/src/JonnyW/PhantomJs/Resources/procedures/procedure_capture.partial b/src/JonnyW/PhantomJs/Resources/procedures/procedure_capture.partial index 5450e9d..fab1e67 100644 --- a/src/JonnyW/PhantomJs/Resources/procedures/procedure_capture.partial +++ b/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)); diff --git a/src/JonnyW/PhantomJs/Resources/procedures/procedure_default.partial b/src/JonnyW/PhantomJs/Resources/procedures/procedure_default.partial index 3b2fde3..c30bffe 100644 --- a/src/JonnyW/PhantomJs/Resources/procedures/procedure_default.partial +++ b/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)); diff --git a/src/JonnyW/PhantomJs/Resources/procedures/procedure_pdf.partial b/src/JonnyW/PhantomJs/Resources/procedures/procedure_pdf.partial index ac830f5..c0783ab 100644 --- a/src/JonnyW/PhantomJs/Resources/procedures/procedure_pdf.partial +++ b/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)); diff --git a/src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php b/src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php index 40f1480..2b9af50 100644 --- a/src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php +++ b/src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php @@ -359,6 +359,42 @@ public function testCanDeleteAllCookiesFromPersistentCookieFile() $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. diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Http/ResponseTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Http/ResponseTest.php index 05cad4a..8a7c3cb 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Http/ResponseTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Http/ResponseTest.php @@ -368,6 +368,25 @@ public function testIsNotRedirectIfStatusCodeIsNotRedirect() $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 ++++++++++ **/ /** +++++++++++++++++++++++++++++++++++ **/