/** * @param RequestInterface $actual * @param RequestInterface $expected * @throws FailedRequestExpectationException */ public static function checkRequest(RequestInterface $actual, RequestInterface $expected) { self::checkIsEqual($actual->getHost(), $expected->getHost(), 'host'); self::checkIsEqual($actual->getPath(), $expected->getPath(), 'url path'); self::checkIsEqual($actual->getMethod(), $expected->getMethod(), 'http method'); self::checkRequestQuery($actual, $expected); self::checkContentType($actual, $expected); self::checkRequestBody($actual, $expected); }
/** * @param RequestInterface $request * @return array ['query' => ..., 'request' => ...] */ protected function getRequestAndQuery(RequestInterface $request) { $query = []; foreach ($request->getQuery() as $param => $val) { $query[$param] = $val; } $requestInfo = ['url' => $request->getUrl(), 'path' => $request->getPath(), 'queryString' => (string) $request->getQuery(), 'method' => $request->getMethod(), 'hostname' => $request->getHost(), 'port' => $request->getPort(), 'resource' => $request->getResource()]; return ['query' => $query, 'request' => $requestInfo]; }
function let(SiteConfigBuilder $siteConfigBuilder, SiteConfig $siteConfig, Factory $authenticatorFactory, ClientInterface $guzzle, Emitter $emitter, BeforeEvent $beforeEvent, CompleteEvent $completeEvent, RequestInterface $request, ResponseInterface $response, Factory $authenticatorFactory) { $siteConfig->getHost()->willReturn('example.com'); $siteConfigBuilder->buildForHost('example.com')->willReturn($siteConfig); $guzzle->getEmitter()->willReturn($emitter); $request->getHost()->willReturn('example.com'); $beforeEvent->getRequest()->willReturn($request); $beforeEvent->getClient()->willReturn($guzzle); $response->getBody()->willReturn('<html></html>'); $completeEvent->getResponse()->willReturn($response); $completeEvent->getRequest()->willReturn($request); $completeEvent->getClient()->willReturn($guzzle); $this->beConstructedWith($siteConfigBuilder, $authenticatorFactory); }
public function signRequest(RequestInterface $request, CredentialsInterface $credentials) { /** @var PostBodyInterface $body */ $body = $request->getBody(); $body->setField('Timestamp', gmdate('c')); $body->setField('SignatureVersion', '2'); $body->setField('SignatureMethod', 'HmacSHA256'); $body->setField('AWSAccessKeyId', $credentials->getAccessKeyId()); if ($token = $credentials->getSecurityToken()) { $body->setField('SecurityToken', $token); } // build string to sign $sign = $request->getMethod() . "\n" . $request->getHost() . "\n" . '/' . "\n" . $this->getCanonicalizedParameterString($body); $request->getConfig()->set('aws.signature', $sign); $body->setField('Signature', base64_encode(hash_hmac('sha256', $sign, $credentials->getSecretKey(), true))); }
private function add_cookies(RequestInterface $request, $value) { if ($value === true) { static $cookie = null; if (!$cookie) { $cookie = new Cookie(); } $request->getEmitter()->attach($cookie); } elseif (is_array($value)) { $request->getEmitter()->attach(new Cookie(CookieJar::fromArray($value, $request->getHost()))); } elseif ($value instanceof CookieJarInterface) { $request->getEmitter()->attach(new Cookie($value)); } elseif ($value !== false) { throw new \InvalidArgumentException('cookies must be an array, ' . 'true, or a CookieJarInterface object'); } }
public function addCookieHeader(RequestInterface $request) { $values = array(); $scheme = $request->getScheme(); $host = $request->getHost(); $path = $request->getPath(); foreach ($this->cookies as $cookie) { if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme == 'https')) { $values[] = $cookie->getName() . '=' . self::getCookieValue($cookie->getValue()); } } if ($values) { $request->setHeader('Cookie', implode('; ', $values)); } }
/** * @param RequestInterface $request */ protected function extractCookiesArgument(RequestInterface $request) { $listeners = $request->getEmitter()->listeners('before'); foreach ($listeners as $listener) { if ($listener[0] instanceof Cookie) { $values = []; $scheme = $request->getScheme(); $host = $request->getHost(); $path = $request->getPath(); /** @var SetCookie $cookie */ foreach ($listener[0]->getCookieJar() as $cookie) { if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme == 'https')) { $values[] = $cookie->getName() . '=' . CookieJar::getCookieValue($cookie->getValue()); } } if ($values) { $this->addOption('b', escapeshellarg(implode('; ', $values))); } } } }
/** * Get context fields to add to the time-line entry. * * @param RequestInterface $request * @param ResponseInterface $response * @param Exception $error * * @return array */ protected function getParameters(RequestInterface $request, ResponseInterface $response = null, Exception $error = null) { $params = []; $keys = array_intersect($this->context, $this->availableParameters); foreach ($keys as $key) { switch ($key) { case 'request': $result = $request; break; case 'response': $result = $response; break; case 'method': $result = $request->getMethod(); break; case 'url': $result = $request->getUrl(); break; case 'resource': $result = $request->getResource(); break; case 'request_version': $result = $request->getProtocolVersion(); break; case 'response_version': $result = $response ? $response->getProtocolVersion() : 'NULL'; break; case 'host': $result = $request->getHost(); break; case 'hostname': $result = gethostname(); break; case 'status_code': $result = $response ? $response->getStatusCode() : 'NULL'; break; case 'phrase': $result = $response ? $response->getReasonPhrase() : 'NULL'; break; case 'error': $result = $error ? $error->getMessage() : 'NULL'; break; } $params[$key] = (string) $result ?: ''; } return $params; }
/** * Save data * * @author Florian Preusner * @version 2.1 * @since 2015-05 * * @param RequestInterface $request */ public function save(RequestInterface $request) { $this->setHost($request->getHost()); }
/** * @return \BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig|false */ private function buildSiteConfig(RequestInterface $request) { return $this->configBuilder->buildForHost($request->getHost()); }
/** * Collect & sanitize data about a Guzzle request. * * @param RequestInterface $request Guzzle request. * @return array */ private function collectRequest(RequestInterface $request) { $query = $request->getQuery(); return ['headers' => $request->getHeaders(), 'method' => $request->getMethod(), 'scheme' => $request->getScheme(), 'host' => $request->getHost(), 'path' => $request->getPath(), 'query' => (string) $query, 'queryParams' => $query->toArray(), 'body' => (string) $request->getBody()]; }