Calls a URI.
public request ( string $method, string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, boolean $changeHistory = true ) : |
||
$method | string | The request method |
$uri | string | The URI to fetch |
$parameters | array | The Request parameters |
$files | array | The files |
$server | array | The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does) |
$content | string | The raw body data |
$changeHistory | boolean | Whether to update the history or not (only used internally for back(), forward(), and reload()) |
리턴 |
/** * @return AuthenticateResponse */ public function authenticate() { $this->client->request('POST', self::SING_IN_URI, ['username' => $this->username, 'password' => $this->password], [], ['HTTP_ACCEPT' => 'application/json']); $loginResponse = json_decode($this->client->getResponse()->getContent()); $authenticateHeaders = ['HTTP_TOKEN' => isset($loginResponse->Token) ? $loginResponse->Token : null, 'HTTP_EXPIREAT' => isset($loginResponse->ExpireAt) ? $loginResponse->ExpireAt : null, 'HTTP_USERNAME' => isset($loginResponse->Username) ? $loginResponse->Username : null]; return new AuthenticateResponse([], $authenticateHeaders); }
/** * @param $uri * @param $format * @return \PHPExcel */ protected function getDocument($uri, $format = 'xlsx') { // generate source static::$client->request('GET', $uri); $source = static::$client->getResponse()->getContent(); // create source directory if necessary if (!file_exists(__DIR__ . static::$TEMP_PATH)) { mkdir(__DIR__ . static::$TEMP_PATH); } // save source file_put_contents(__DIR__ . static::$TEMP_PATH . 'simple' . '.' . $format, $source); // load source switch ($format) { case 'ods': $reader = new PHPExcel_Reader_OOCalc(); break; case 'xls': $reader = new PHPExcel_Reader_Excel5(); break; case 'xlsx': $reader = new PHPExcel_Reader_Excel2007(); break; default: throw new InvalidArgumentException(); } return $reader->load(__DIR__ . static::$TEMP_PATH . 'simple' . '.' . $format); }
public function testGetCategories() { $this->client->request('GET', '/search/categories'); $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); $result = json_decode($response->getContent(), true); $this->assertContains('test', $result); }
public function testGetIndexes() { $this->client->request('GET', '/search/indexes'); $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); $result = json_decode($response->getContent(), true); $this->assertEquals('product', $result[0]['indexName']); }
protected function execute($method = 'GET', $url, $parameters = array(), $files = array()) { foreach ($this->headers as $header => $val) { $header = str_replace('-', '_', strtoupper($header)); $this->client->setServerParameter("HTTP_{$header}", $val); # Issue #827 - symfony foundation requires 'CONTENT_TYPE' without HTTP_ if ($this->isFunctional and $header == 'CONTENT_TYPE') { $this->client->setServerParameter($header, $val); } } // allow full url to be requested $url = (strpos($url, '://') === false ? $this->config['url'] : '') . $url; $parameters = $this->encodeApplicationJson($method, $parameters); if (is_array($parameters) || $method == 'GET') { if (!empty($parameters) && $method == 'GET') { $url .= '?' . http_build_query($parameters); } if ($method == 'GET') { $this->debugSection("Request", "{$method} {$url}"); } else { $this->debugSection("Request", "{$method} {$url} " . json_encode($parameters)); } $this->client->request($method, $url, $parameters, $files); } else { $this->debugSection("Request", "{$method} {$url} " . $parameters); $this->client->request($method, $url, array(), $files, array(), $parameters); } $this->response = $this->client->getInternalResponse()->getContent(); $this->debugSection("Response", $this->response); if (count($this->client->getInternalRequest()->getCookies())) { $this->debugSection('Cookies', $this->client->getInternalRequest()->getCookies()); } $this->debugSection("Headers", $this->client->getInternalResponse()->getHeaders()); $this->debugSection("Status", $this->client->getInternalResponse()->getStatus()); }
protected function execute($method = 'GET', $url, $parameters = array(), $files = array()) { foreach ($this->headers as $header => $val) { $this->client->setServerParameter("HTTP_{$header}", $val); } // allow full url to be requested $url = (strpos($url, '://') === false ? $this->config['url'] : '') . $url; if (is_array($parameters) || $parameters instanceof \ArrayAccess) { $parameters = $this->scalarizeArray($parameters); if (array_key_exists('Content-Type', $this->headers) && $this->headers['Content-Type'] === 'application/json' && $method != 'GET') { $parameters = json_encode($parameters); } } if (is_array($parameters) || $method == 'GET') { if ($method == 'GET' && !empty($parameters)) { $url .= '?' . http_build_query($parameters); $this->debugSection("Request", "{$method} {$url}"); } else { $this->debugSection("Request", "{$method} {$url}?" . http_build_query($parameters)); } $this->client->request($method, $url, $parameters, $files); } else { $this->debugSection("Request", "{$method} {$url} " . $parameters); $this->client->request($method, $url, array(), $files, array(), $parameters); } $this->response = $this->client->getResponse()->getContent(); $this->debugSection("Response", $this->response); }
protected function clientRequest($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) { if ($this instanceof Framework) { if (preg_match('#^(//|https?://(?!localhost))#', $uri)) { $hostname = parse_url($uri, PHP_URL_HOST); if (!$this->isInternalDomain($hostname)) { throw new ExternalUrlException(get_class($this) . " can't open external URL: " . $uri); } } if ($method !== 'GET' && $content === null && !empty($parameters)) { $content = http_build_query($parameters); } } if (!ReflectionHelper::readPrivateProperty($this->client, 'followRedirects')) { $result = $this->client->request($method, $uri, $parameters, $files, $server, $content, $changeHistory); $this->debugResponse($uri); return $result; } else { $maxRedirects = ReflectionHelper::readPrivateProperty($this->client, 'maxRedirects', 'Symfony\\Component\\BrowserKit\\Client'); $this->client->followRedirects(false); $result = $this->client->request($method, $uri, $parameters, $files, $server, $content, $changeHistory); $this->debugResponse($uri); return $this->redirectIfNecessary($result, $maxRedirects, 0); } }
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) { $parameters = array_merge($this->parameters, $parameters); $crawler = parent::request($method, $uri, $parameters, $files, $server, $content, $changeHistory); if ($crawler->getNode(0) === null) { return $this->response->getContent(); } else { return $crawler; } }
/** * testing savePerDiem action. */ public function testSavePerDiemAction() { // Empty request $crawler = $this->client->request('POST', '/secured/travel/admin/save/perdiem'); $this->assertJson($this->client->getResponse()->getContent(), 'testSavePerDiemAction: The response\'s content is not a JSON object.'); $this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'application/json'), 'testSavePerDiemAction: The content-type is not a json.'); // Filled up request $crawler = $this->client->request('POST', '/secured/travel/admin/save/perdiem', array('perdiem' => array(0 => array('id' => 1, 'hours' => 14, 'amount' => 24), 1 => array('id' => null, 'hours' => 12, 'amount' => 12)))); $this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'text/html; charset=UTF-8'), 'testSavePerDiemAction: The content-type is not html.'); }
protected function login(Client $client, $username = '******', $password = '******') { $client->restart(); $crawler = $client->request('GET', '/login'); $this->assertTrue($client->getResponse()->isSuccessful(), 'Response should be successful'); $form = $crawler->selectButton('Login')->form(); $client->submit($form, array('_username' => $username, '_password' => $password)); $this->assertTrue($client->getResponse()->isRedirect(), 'Response should be redirect'); $crawler = $client->followRedirect(); $this->assertGreaterThan(0, $crawler->filter('html:contains("Benvenuto")')->count()); }
protected function processRequest($action, $body) { $this->client->request('POST', $this->config['endpoint'], array(), array(), array( "HTTP_Content-Type" => "text/xml; charset=UTF-8", 'HTTP_Content-Length' => strlen($body), 'HTTP_SOAPAction' => $action), $body ); }
private function addToCart($mealName, $quantity, Crawler $crawler, Client $client) { $titles = $crawler->filter('h4')->reduce(function ($crawler) use($mealName) { return false !== strpos($crawler->text(), $mealName); }); if (count($titles) !== 1) { throw new \RuntimeException(sprintf('Expected 1 title containing "%s", found %s.', $mealName, count($titles))); } $link = $titles->eq(0)->parents()->first()->filter('input[data-meal]'); $mealId = $link->attr('data-meal'); $client->request('POST', '/cart', array('meal' => $mealId, 'mode' => 'add', 'quantity' => $quantity)); $this->assertEquals(200, $client->getResponse()->getStatusCode()); }
protected function execute($method = 'GET', $url, $parameters = [], $files = []) { $this->debugSection("Request headers", $this->headers); foreach ($this->headers as $header => $val) { $header = str_replace('-', '_', strtoupper($header)); $this->client->setServerParameter("HTTP_{$header}", $val); // Issue #1650 - Symfony BrowserKit changes HOST header to request URL if ($header === 'HOST') { $this->client->setServerParameter("HTTP_ HOST", $val); } // Issue #827 - symfony foundation requires 'CONTENT_TYPE' without HTTP_ if ($this->isFunctional && $header === 'CONTENT_TYPE') { $this->client->setServerParameter($header, $val); } } // allow full url to be requested if (strpos($url, '://') === false) { $url = $this->config['url'] . $url; } $this->params = $parameters; $parameters = $this->encodeApplicationJson($method, $parameters); if (is_array($parameters) || $method === 'GET') { if (!empty($parameters) && $method === 'GET') { $url .= '?' . http_build_query($parameters); } if ($method == 'GET') { $this->debugSection("Request", "{$method} {$url}"); } else { $this->debugSection("Request", "{$method} {$url} " . json_encode($parameters)); } $this->client->request($method, $url, $parameters, $files); } else { $requestData = $parameters; if (!ctype_print($requestData) && false === mb_detect_encoding($requestData, mb_detect_order(), true)) { // if the request data has non-printable bytes and it is not a valid unicode string, reformat the // display string to signify the presence of request data $requestData = '[binary-data length:' . strlen($requestData) . ' md5:' . md5($requestData) . ']'; } $this->debugSection("Request", "{$method} {$url} " . $requestData); $this->client->request($method, $url, [], $files, [], $parameters); } $this->response = (string) $this->connectionModule->_getResponseContent(); $this->debugSection("Response", $this->response); if (count($this->client->getInternalRequest()->getCookies())) { $this->debugSection('Cookies', $this->client->getInternalRequest()->getCookies()); } $this->debugSection("Headers", $this->client->getInternalResponse()->getHeaders()); $this->debugSection("Status", $this->client->getInternalResponse()->getStatus()); }
/** * Sends a XMLRPC method call to remote XMLRPC-server. * * @param string $methodName * @param array $parameters */ public function sendXMLRPCMethodCall($methodName, $parameters = array()) { if (!array_key_exists('Content-Type', $this->headers)) { $this->headers['Content-Type'] = 'text/xml'; } foreach ($this->headers as $header => $val) { $this->client->setServerParameter("HTTP_{$header}", $val); } $url = $this->config['url']; if (is_array($parameters)) { $parameters = $this->scalarizeArray($parameters); } $requestBody = xmlrpc_encode_request($methodName, array_values($parameters)); $this->debugSection('Request', $url . PHP_EOL . $requestBody); $this->client->request('POST', $url, array(), array(), array(), $requestBody); $this->response = $this->client->getInternalResponse()->getContent(); $this->debugSection('Response', $this->response); }
protected function execute($method = 'GET', $url, $parameters = [], $files = []) { $this->debugSection("Request headers", $this->headers); if ($parameters instanceof \JsonSerializable) { $parameters = $parameters->jsonSerialize(); } foreach ($this->headers as $header => $val) { $header = str_replace('-', '_', strtoupper($header)); $this->client->setServerParameter("HTTP_{$header}", $val); // Issue #1650 - Symfony BrowserKit changes HOST header to request URL if (strtolower($header) == 'host') { $this->client->setServerParameter("HTTP_ HOST", $val); } // Issue #827 - symfony foundation requires 'CONTENT_TYPE' without HTTP_ if ($this->isFunctional and $header == 'CONTENT_TYPE') { $this->client->setServerParameter($header, $val); } } // allow full url to be requested $url = (strpos($url, '://') === false ? $this->config['url'] : '') . $url; $this->params = $parameters; $parameters = $this->encodeApplicationJson($method, $parameters); if (is_array($parameters) || $method == 'GET') { if (!empty($parameters) && $method == 'GET') { $url .= '?' . http_build_query($parameters); } if ($method == 'GET') { $this->debugSection("Request", "{$method} {$url}"); } else { $this->debugSection("Request", "{$method} {$url} " . json_encode($parameters)); } $this->client->request($method, $url, $parameters, $files); } else { $this->debugSection("Request", "{$method} {$url} " . $parameters); $this->client->request($method, $url, [], $files, [], $parameters); } $this->response = (string) $this->client->getInternalResponse()->getContent(); $this->debugSection("Response", $this->response); if (count($this->client->getInternalRequest()->getCookies())) { $this->debugSection('Cookies', $this->client->getInternalRequest()->getCookies()); } $this->debugSection("Headers", $this->client->getInternalResponse()->getHeaders()); $this->debugSection("Status", $this->client->getInternalResponse()->getStatus()); }
protected function clientRequest($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) { $this->debugSection("Request Headers", $this->headers); foreach ($this->headers as $header => $val) { // moved from REST module if (!$val) { continue; } $header = str_replace('-', '_', strtoupper($header)); $server["HTTP_{$header}"] = $val; // Issue #827 - symfony foundation requires 'CONTENT_TYPE' without HTTP_ if ($this instanceof Framework && $header === 'CONTENT_TYPE') { $server[$header] = $val; } } $server['REQUEST_TIME'] = time(); $server['REQUEST_TIME_FLOAT'] = microtime(true); if ($this instanceof Framework) { if (preg_match('#^(//|https?://(?!localhost))#', $uri)) { $hostname = parse_url($uri, PHP_URL_HOST); if (!$this->isInternalDomain($hostname)) { throw new ExternalUrlException(get_class($this) . " can't open external URL: " . $uri); } } if ($method !== 'GET' && $content === null && !empty($parameters)) { $content = http_build_query($parameters); } } if (!ReflectionHelper::readPrivateProperty($this->client, 'followRedirects')) { $result = $this->client->request($method, $uri, $parameters, $files, $server, $content, $changeHistory); $this->debugResponse($uri); return $result; } $maxRedirects = ReflectionHelper::readPrivateProperty($this->client, 'maxRedirects', 'Symfony\\Component\\BrowserKit\\Client'); $this->client->followRedirects(false); $result = $this->client->request($method, $uri, $parameters, $files, $server, $content, $changeHistory); $this->debugResponse($uri); return $this->redirectIfNecessary($result, $maxRedirects, 0); }
/** * testing deleteGroup adction. */ public function testDeleteGroupAction() { $crawler = $this->client->request('POST', '/secured/admin/groups/delete', array('id' => $this->group->getId())); $this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'text/html; charset=UTF-8'), 'testDeleteGroupAction: The content-type is not html.'); }
/** * test version action */ public function testVersionAction() { $crawler = $this->client->request('GET', '/secured/opithrm/versions'); $this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'text/html; charset=UTF-8'), 'testVersionAction: The content-type is not a html file.'); }
private function createSession(Client $client) { return $client->request('GET', '/js/sglivechat-tracker/status.html'); }
/** * @param $client * @param $position 0 based index position * @return mixed */ private function getPageEditUriFromListByPosition(Client $client, $position) { $crawler = $client->getCrawler(); if (strpos($client->getRequest()->getUri(), '/admin/cmf/page/page/list') === false) { $crawler = $client->request('GET', '/admin/cmf/page/page/list'); } $this->assertTrue($client->getResponse()->isSuccessful()); $rows = $crawler->filter('table tbody tr'); return $rows->eq($position)->filter('td')->eq(1)->filter('a')->attr('href'); }
/** * If your page triggers an ajax request, you can perform it manually. * This action sends an ajax request with specified method and params. * * Example: * * You need to perform an ajax request specifying the HTTP method. * * ``` php * <?php * $I->sendAjaxRequest('PUT', /posts/7', array('title' => 'new title'); * * ``` * * @param $method * @param $uri * @param $params */ public function sendAjaxRequest($method, $uri, $params = array()) { $this->client->request($method, $uri, $params, array(), array('HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')); $this->debugResponse(); }
/** * testing the resetPassword action. * * This should be the last method, because this is reset the password for the admin. */ public function testResetPasswordAction() { $crawler = $this->client->request('POST', '/secured/user/password/reset', array('id' => $this->user->getId())); $this->assertJson($this->client->getResponse()->getContent(), 'testResetPasswordAction: The response\'s content is not a JSON object.'); $this->assertTrue($this->client->getResponse()->headers->contains('Content-Type', 'application/json'), 'testResetPasswordAction: The content-type is not a json.'); }
private function createSession(Client $client) { $client->request('POST', '/sglivechat', array('name' => 'Ismael', 'email' => '*****@*****.**', 'question' => 'This is my comment')); }
protected function createNote(Client $client, $message) { $client->request('POST', '/notes.json', array('note' => array('message' => $message))); $response = $client->getResponse(); $this->assertJsonResponse($response, Response::HTTP_CREATED); }
/** * @param $url * @param $data * @return Response */ protected function makeRequest($url, $data) { $this->client->request('POST', $url, [], [], [], json_encode($data)); return Response::jsonUnserialize($this->client->getResponse()->getContent()); }
/** * If your page triggers an ajax request, you can perform it manually. * This action sends an ajax request with specified method and params. * * Example: * * You need to perform an ajax request specifying the HTTP method. * * ``` php * <?php * $I->sendAjaxRequest('PUT', '/posts/7', array('title' => 'new title')); * * ``` * * @param $method * @param $uri * @param $params */ public function sendAjaxRequest($method, $uri, $params = []) { $this->client->request($method, $uri, $params, [], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); $this->debugResponse(); }