function _createAction($action, $base) { if (is_bool($action)) { return $base; } $url = new SimpleUrl($action); return $url->makeAbsolute($base); }
function testDefaultFrameTargetOnForm() { $tag = new SimpleFormTag(array('method' => 'GET', 'action' => 'here.php', 'id' => '33')); $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html')); $form->setDefaultTarget('frame'); $expected = new SimpleUrl('http://host/a/here.php'); $expected->setTarget('frame'); $this->assertEqual($form->getAction(), $expected); }
function testEncodedParameters() { $url = new SimpleUrl(""); $url->addRequestParameter("a", '?!"\'#~@[]{}:;<>,./|£$%^&*()_+-='); $this->assertIdentical($request = $url->getEncodedRequest(), "?a=%3F%21%22%27%23%7E%40%5B%5D%7B%7D%3A%3B%3C%3E%2C.%2F%7C%A3%24%25%5E%26%2A%28%29_%2B-%3D"); $url = new SimpleUrl("?a=%3F%21%22%27%23%7E%40%5B%5D%7B%7D%3A%3B%3C%3E%2C.%2F%7C%A3%24%25%5E%26%2A%28%29_%2B-%3D"); $request = $url->getRequest(); $this->assertEqual($request["a"], '?!"\'#~@[]{}:;<>,./|£$%^&*()_+-='); }
function testGetRequestWithoutIncidentGivesNoErrors() { $url = new SimpleUrl('http://*****:*****@this.com/page.html'); $url->addRequestParameters(array('a' => 'A', 'b' => 'B')); $agent =& new MockRequestUserAgent(); $agent->setReturnReference('_createHttpRequest', $this->_request); $agent->SimpleUserAgent(); $response =& $agent->fetchResponse(new SimpleUrl('http://*****:*****@this.com/page.html'), new SimpleGetEncoding(array('a' => 'A', 'b' => 'B'))); $this->assertFalse($response->isError()); }
function testDefaultFrameTargetOnForm() { $page =& new MockSimplePage(); $page->expectOnce('expandUrl', array(new SimpleUrl('here.php'))); $page->setReturnValue('expandUrl', new SimpleUrl('http://host/here.php')); $tag =& new SimpleFormTag(array('method' => 'GET', 'action' => 'here.php')); $form =& new SimpleForm($tag, $page); $form->setDefaultTarget('frame'); $expected = new SimpleUrl('http://host/here.php'); $expected->setTarget('frame'); $this->assertEqual($form->getAction(), $expected); }
/** * Combined action attribute with current location * to get an absolute form target. * @param string $action Action attribute from form tag. * @param SimpleUrl $base Page location. * @return SimpleUrl Absolute form target. */ function _createAction($action, $base) { if ($action === false) { return $base; } if ($action === true) { $url = new SimpleUrl(''); } else { $url = new SimpleUrl($action); } return $url->makeAbsolute($base); }
function _truncateHost($host) { $tlds = SimpleUrl::getAllTopLevelDomains(); if (preg_match('/[a-z\\-]+\\.(' . $tlds . ')$/i', $host, $matches)) { return $matches[0]; } elseif (preg_match('/[a-z\\-]+\\.[a-z\\-]+\\.[a-z\\-]+$/i', $host, $matches)) { return $matches[0]; } return false; }
/** * Expands expandomatic URLs into fully qualified * URLs. * @param SimpleUrl $url Relative URL. * @return SimpleUrl Absolute URL. * @access protected */ function _makeAbsolute($url) { if (!is_object($url)) { $url = new SimpleUrl($url); } return $url->makeAbsolute($this->getUrl()); }
/** * Turns an incoming URL string into a * URL object, filling the relative URL if * a base URL is present. * @param string $base_url Browser current URL. * @param string $raw_url Incoming URL. * @param hash $parameters Additional request, parameters. * @return SimpleUrl Absolute URL. * @access public * @static */ function createAbsoluteUrl($base_url, $raw_url, $parameters = false) { $url = new SimpleUrl($raw_url); if ($parameters) { $url->addRequestParameters($parameters); } $url->makeAbsolute($base_url); return $url; }
/** * Fetches a page or a single frame if that is the current * focus. * @param SimpleUrl $url Target to fetch. * @param SimpleEncoding $parameters GET/POST parameters. * @return string Raw content of page. * @access private */ function _load($url, $parameters) { $frame = $url->getTarget(); if (!$frame || !$this->_page->hasFrames() || strtolower($frame) == '_top') { return $this->_loadPage($url, $parameters); } return $this->_loadFrame(array($frame), $url, $parameters); }
/** * Expands expandomatic URLs into fully qualified * URLs. * @param SimpleUrl $url Relative URL. * @return SimpleUrl Absolute URL. * @access public */ function expandUrl($url) { if (!is_object($url)) { $url = new SimpleUrl($url); } $location = $this->getBaseUrl() ? $this->getBaseUrl() : new SimpleUrl(); return $url->makeAbsolute($location->makeAbsolute($this->getUrl())); }
function testReadFrameTaggedUrlsFromFrameInFocus() { $frame =& new MockSimplePage(); $by_label = new SimpleUrl('l'); $by_label->setTarget('L'); $frame->setReturnValue('getUrlsByLabel', array($by_label)); $by_id = new SimpleUrl('i'); $by_id->setTarget('I'); $frame->setReturnValue('getUrlById', $by_id); $frameset =& new SimpleFrameset(new MockSimplePage()); $frameset->addFrame($frame, 'A'); $frameset->setFrameFocus('A'); $this->assertIdentical($frameset->getUrlsByLabel('label'), array($by_label)); $this->assertIdentical($frameset->getUrlById(99), $by_id); }
function assertUrl($raw, $parts, $params = false) { if (!is_array($params)) { $params = array(); } $url = new SimpleUrl($raw); $this->assertIdentical($url->getScheme(), $parts[0], "[{$raw}] scheme->%s"); $this->assertIdentical($url->getUsername(), $parts[1], "[{$raw}] username->%s"); $this->assertIdentical($url->getPassword(), $parts[2], "[{$raw}] password->%s"); $this->assertIdentical($url->getHost(), $parts[3], "[{$raw}] host->%s"); $this->assertIdentical($url->getPort(), $parts[4], "[{$raw}] port->%s"); $this->assertIdentical($url->getPath(), $parts[5], "[{$raw}] path->%s"); $this->assertIdentical($url->getTld(), $parts[6], "[{$raw}] tld->%s"); $this->assertIdentical($url->getEncodedRequest(), $parts[7], "[{$raw}] encoded->%s"); $query = new SimpleQueryString(); foreach ($params as $key => $value) { $query->add($key, $value); } $this->assertIdentical($url->getRequest(), $query, "[{$raw}] request->%s"); $this->assertIdentical($url->getFragment(), $parts[8], "[{$raw}] fragment->%s"); }
/** * Actually opens the low level socket. * @param SimpleUrl $file SimpleUrl file target. * @param string $error Recipient of error message. * @param integer $timeout Maximum time to wait for connection. * @access protected */ protected function openFile($file, &$error) { return @fopen($file->asString(), 'r'); }
function testHead() { $headers =& new MockSimpleHttpHeaders($this); $headers->setReturnValue('getMimeType', 'text/html'); $headers->setReturnValue('getResponseCode', 200); $headers->setReturnValue('getNewCookies', array()); $response =& new MockSimpleHttpResponse($this); $response->setReturnValue('getContent', 'stuff'); $response->setReturnReference('getHeaders', $headers); $request =& new MockSimpleHttpRequest($this); $request->setReturnReference('fetch', $response); $url = new SimpleUrl('http://this.com/page.html'); $url->addRequestParameters(array('a' => 'A', 'b' => 'B')); $agent =& new MockRequestUserAgent($this); $agent->setReturnReference('_createHttpRequest', $request); $agent->expectOnce('_createHttpRequest', array('HEAD', new SimpleUrl('http://*****:*****@this.com/page.html?a=A&b=B'), array())); $agent->SimpleUserAgent(); $agent->fetchResponse('HEAD', new SimpleUrl('http://*****:*****@this.com/page.html'), array('a' => 'A', 'b' => 'B')); $agent->tally(); }
/** * Presents the appropriate headers for this location. * @param SimpleHttpRequest $request Request to modify. * @param SimpleUrl $url Base of realm. * @access public */ function addHeaders($request, $url) { if ($url->getUsername() && $url->getPassword()) { $username = $url->getUsername(); $password = $url->getPassword(); } elseif ($realm = $this->findRealmFromUrl($url)) { $username = $realm->getUsername(); $password = $realm->getPassword(); } else { return; } $this->addBasicHeaders($request, $username, $password); }
/** * Replaces unknown sections to turn a relative * URL into an absolute one. The base URL can * be either a string or a SimpleUrl object. * @param string/SimpleUrl $base Base URL. * @access public */ function makeAbsolute($base) { if (!is_object($base)) { $base = new SimpleUrl($base); } $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme(); $host = $this->getHost() ? $this->getHost() : $base->getHost(); if (substr($this->_path, 0, 1) == "/") { $path = $this->normalisePath($this->_path); } else { $path = $this->normalisePath($base->getBasePath() . $this->_path); } $identity = ''; if ($this->_username && $this->_password) { $identity = $this->_username . ':' . $this->_password . '@'; } $encoded = $this->getEncodedRequest(); $fragment = $this->getFragment() ? '#' . $this->getFragment() : ''; return new SimpleUrl("{$scheme}://{$identity}{$host}{$path}{$encoded}{$fragment}"); }
/** * Test to see if link is an absolute one. * @param string $url Url to test. * @return boolean True if absolute. * @access protected */ function _linkIsAbsolute($url) { $parsed = new SimpleUrl($url); return (bool) ($parsed->getScheme() && $parsed->getHost()); }
/** * Replaces unknown sections to turn a relative * URL into an absolute one. The base URL can * be either a string or a SimpleUrl object. * @param string/SimpleUrl $base Base URL. * @access public */ function makeAbsolute($base) { if (!is_object($base)) { $base = new SimpleUrl($base); } if ($this->getHost()) { $scheme = $this->getScheme(); $host = $this->getHost(); $port = $this->getPort() ? ':' . $this->getPort() : ''; $identity = $this->getIdentity() ? $this->getIdentity() . '@' : ''; if (!$identity) { $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; } } else { $scheme = $base->getScheme(); $host = $base->getHost(); $port = $base->getPort() ? ':' . $base->getPort() : ''; $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; } $path = $this->normalisePath($this->extractAbsolutePath($base)); $encoded = $this->getEncodedRequest(); $fragment = $this->getFragment() ? '#' . $this->getFragment() : ''; $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); return new SimpleUrl("{$scheme}://{$identity}{$host}{$port}{$path}{$encoded}{$fragment}{$coords}"); }
/** * Combined action attribute with current location * to get an absolute form target. * @param string $action Action attribute from form tag. * @param SimpleUrl $base Page location. * @return SimpleUrl Absolute form target. */ function _createAction($action, $base) { if ($action === '' || $action === false) { return $base; } $url = new SimpleUrl($action); return $url->makeAbsolute($base); }
public function testUsernameAndPasswordInUTF8() { $url = new SimpleUrl('http://pÈrick:penËt@www.lastcraft.com'); $this->assertEqual($url->getUsername(), 'pÈrick'); $this->assertEqual($url->getPassword(), 'penËt'); }
function testTargetAttachment() { $url = new SimpleUrl('http://www.site.com/home.html'); $this->assertIdentical($url->getTarget(), false); $url->setTarget('A frame'); $this->assertIdentical($url->getTarget(), 'A frame'); }
function testClickLinkWithUnknownFrameStillRequestsWholePage() { $agent = new MockSimpleUserAgent(); $agent->returns('fetchResponse', new MockSimpleHttpResponse()); $agent->expectAt(0, 'fetchResponse', array(new SimpleUrl('http://this.com/page.html'), new SimpleGetEncoding())); $target = new SimpleUrl('http://this.com/new.html'); $target->setTarget('missing'); $agent->expectAt(1, 'fetchResponse', array($target, new SimpleGetEncoding())); $agent->expectCallCount('fetchResponse', 2); $parsed_url = new SimpleUrl('http://this.com/new.html'); $parsed_url->setTarget('missing'); $page = new MockSimplePage(); $page->setReturnValue('getUrlsByLabel', array($parsed_url)); $page->setReturnValue('hasFrames', false); $page->expectOnce('getUrlsByLabel', array('New')); $page->setReturnValue('getRaw', 'A page'); $browser = $this->createBrowser($agent, $page); $browser->get('http://this.com/page.html'); $this->assertTrue($browser->clickLink('New')); }
/** * Extracts new cookies into the cookie jar. * @param SimpleUrl $url Target to fetch as url object. * @param array $cookies New cookies. * @access private */ function _addCookiesToJar($url, $cookies) { foreach ($cookies as $cookie) { if ($url->getHost()) { $cookie->setHost($url->getHost()); } $this->_cookie_jar->setCookie($cookie); } }
function testDOSDirnameAfterFile() { $url = new SimpleUrl('file://C:\\config.sys'); $this->assertEqual($url->getScheme(), 'file'); $this->assertIdentical($url->getHost(), false); $this->assertEqual($url->getPath(), '/C:/config.sys'); }
/** * Writes new cookies to the cookie jar. * @param SimpleCookieJar $jar Jar to write to. * @param SimpleUrl $url Host and path to write under. * @access public */ function writeCookiesToJar(&$jar, $url) { foreach ($this->_cookies as $cookie) { $jar->setCookie($cookie->getName(), $cookie->getValue(), $url->getHost(), $cookie->getPath(), $cookie->getExpiry()); } }
/** * Sends the headers. * @param SimpleSocket $socket Open socket. * @param string $method HTTP request method, * usually GET. * @param string $content Content to send with request. * @access protected */ function _dispatchRequest(&$socket, $method, $content) { parent::_dispatchRequest($socket, $method, SimpleUrl::encodeRequest($content)); }
/** * Replaces unknown sections to turn a relative * URL into an absolute one. The base URL can * be either a string or a SimpleUrl object. * @param string/SimpleUrl $base Base URL. * @access public */ function makeAbsolute($base) { if (!is_object($base)) { $base = new SimpleUrl($base); } $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme(); $host = $this->getHost() ? $this->getHost() : $base->getHost(); $port = $this->_extractAbsolutePort($base); $path = $this->normalisePath($this->_extractAbsolutePath($base)); $identity = $this->_getIdentity() ? $this->_getIdentity() . '@' : ''; $encoded = $this->getEncodedRequest(); $fragment = $this->getFragment() ? '#' . $this->getFragment() : ''; return new SimpleUrl("{$scheme}://{$identity}{$host}{$port}{$path}{$encoded}{$fragment}"); }
/** * Fetches the page until no longer redirected or * until the redirect limit runs out. * @param SimpleUrl $url Target to fetch. * @param SimpelFormEncoding $encoding Additional parameters for request. * @return SimpleHttpResponse Hopefully the target page. * @access private */ protected function fetchWhileRedirected($url, $encoding) { $redirects = 0; do { $response = $this->fetch($url, $encoding); if ($response->isError()) { return $response; } $headers = $response->getHeaders(); if ($this->cookies_enabled) { $headers->writeCookiesToJar($this->cookie_jar, $url); } if (!$headers->isRedirect()) { break; } $location = new SimpleUrl($headers->getLocation()); $url = $location->makeAbsolute($url); $encoding = new SimpleGetEncoding(); } while (!$this->isTooManyRedirects(++$redirects)); return $response; }
/** * Uses a URL to sift relevant cookies by host and * path. Results are list of strings of form "name=value". * @param SimpleUrl $url Url to select by. * @return array Valid name and value pairs. * @access public */ function selectAsPairs($url) { $pairs = array(); foreach ($this->cookies as $cookie) { if ($this->isMatch($cookie, $url->getHost(), $url->getPath(), $cookie->getName())) { $pairs[] = $cookie->getName() . '=' . $cookie->getValue(); } } return $pairs; }