/** * Test that the default privacy setting is used when an existing * bookmark is updated with edit.php. */ public function testDefaultPrivacyEdit() { $this->setUnittestConfig(array('defaults' => array('privacy' => 2))); list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1'); $cookies = $req->getCookieJar(); $req->setMethod(HTTP_Request2::METHOD_POST); $req->addPostParameter('url', 'http://www.example.org/testdefaultprivacyposts_edit'); $req->addPostParameter('description', 'Test bookmark 2 for default privacy.'); $req->addPostParameter('status', '0'); $res = $req->send(); $this->assertEquals(200, $res->getStatus(), 'Adding bookmark failed: ' . $res->getBody()); $bms = $this->bs->getBookmarks(0, null, $uId); $bm = reset($bms['bookmarks']); $bmId = $bm['bId']; $reqUrl = $GLOBALS['unittestUrl'] . 'edit.php/' . $bmId . '?unittestMode=1'; $req2 = new HTTP_Request2($reqUrl, HTTP_Request2::METHOD_POST); $req2->setCookieJar($cookies); $req2->addPostParameter('address', 'http://www.example.org/testdefaultprivacyposts_edit'); $req2->addPostParameter('title', 'Test bookmark 2 for default privacy.'); $req2->addPostParameter('submitted', '1'); $res = $req2->send(); $this->assertEquals(302, $res->getStatus(), 'Editing bookmark failed'); $bm = $this->bs->getBookmark($bmId); $this->assertEquals('2', $bm['bStatus']); }
public function testCookieJar() { $this->request->setUrl($this->baseUrl . 'setcookie.php?name=cookie_name&value=cookie_value'); $req2 = clone $this->request; $this->request->setCookieJar()->send(); $jar = $this->request->getCookieJar(); $jar->store(array('name' => 'foo', 'value' => 'bar'), $this->request->getUrl()); $response = $req2->setUrl($this->baseUrl . 'cookies.php')->setCookieJar($jar)->send(); $this->assertEquals(serialize(array('cookie_name' => 'cookie_value', 'foo' => 'bar')), $response->getBody()); }
/** * Creates a user and a HTTP_Request2 object, does a normal login * and prepares the cookies for the HTTP GET request object so that * the user is seen as logged in when requesting any HTML page. * * Useful for testing HTML pages or ajax URLs. * * @param string $urlSuffix Suffix for the URL * @param mixed $auth If user authentication is needed (true/false) * or array with username and password * @param boolean $privateKey True if to add user with private key * * @return array(HTTP_Request2, integer) HTTP request object and user id * * @uses getRequest() */ protected function getLoggedInRequest($urlSuffix = null, $auth = true, $privateKey = null) { if (is_array($auth)) { list($username, $password) = $auth; } else { $username = '******'; $password = '******'; } $uid = $this->addUser($username, $password, $privateKey); $req = new HTTP_Request2($GLOBALS['unittestUrl'] . '/login.php?unittestMode=1', HTTP_Request2::METHOD_POST); $cookies = $req->setCookieJar()->getCookieJar(); $req->addPostParameter('username', $username); $req->addPostParameter('password', $password); $req->addPostParameter('submitted', 'Log In'); $res = $req->send(); //after login, we normally get redirected $this->assertEquals(302, $res->getStatus(), 'Login failure'); $req = $this->getRequest($urlSuffix); $req->setCookieJar($cookies); return array($req, $uid); }
public function testAddCookieToJar() { $req = new HTTP_Request2(); $req->setCookieJar(); try { $req->addCookie('foo', 'bar'); $this->fail('Expected HTTP_Request2_Exception was not thrown'); } catch (HTTP_Request2_LogicException $e) { } $req->setUrl('http://example.com/path/file.php'); $req->addCookie('foo', 'bar'); $this->assertArrayNotHasKey('cookie', $req->getHeaders()); $cookies = $req->getCookieJar()->getAll(); $this->assertEquals(array('name' => 'foo', 'value' => 'bar', 'domain' => 'example.com', 'path' => '/path/', 'expires' => null, 'secure' => false), $cookies[0]); }
<?php require 'HTTP/Request2.php'; $r = new HTTP_Request2(); $r->setCookieJar(true); // log in $r->setUrl('https://bank.example.com/login.php?user=donald&password=b1gmoney$'); $page = $r->send()->getBody(); // retrieve account balance $r->setUrl('http://bank.example.com/balance.php?account=checking'); $page = $r->send()->getBody(); // make a deposit $r->setUrl('http://bank.example.com/deposit.php'); $r->setMethod(HTTP_Request2::METHOD_POST)->addPostParameter('account', 'checking')->addPostParameter('amount', '122.44'); $page = $r->send()->getBody();