public function testClientTriesToGetAccessTokenWhenNoneGiven() { $connection = $this->getConnectionForTesting(); $connection->setAccessToken(null); $contact = new \Picqer\Financials\Moneybird\Entities\Contact($connection); $contact->get(); $this->assertEquals('POST', $this->container[0]['request']->getMethod()); Psr7\rewind_body($this->container[0]['request']); $this->assertEquals("redirect_uri=testRedirectUrl&grant_type=authorization_code&client_id=testClientId&client_secret=testClientSecret&code=testAuthorizationCode", $this->container[0]['request']->getBody()->getContents()); }
/** * @param RequestInterface $request * @param array $options * @param ResponseInterface|PromiseInterface $response * * @return ResponseInterface|PromiseInterface */ public function processResponse(RequestInterface $request, array $options, ResponseInterface $response) { $response = $response->withHeader('X-Blackfire-Profile-Uuid', $request->getHeader('X-Blackfire-Profile-Uuid')); if (!$response->hasHeader('X-Blackfire-Response')) { return $response; } parse_str($response->getHeader('X-Blackfire-Response')[0], $values); if (!isset($values['continue']) || 'true' !== $values['continue']) { return $response; } Psr7\rewind_body($request); /* @var PromiseInterface|ResponseInterface $promise */ return $this($request, $options); }
/** * @expectedException \RuntimeException */ public function testThrowsWhenBodyCannotBeRewound() { $body = Psr7\stream_for('abc'); $body->read(1); $body = FnStream::decorate($body, ['rewind' => function () { throw new \RuntimeException('a'); }]); $res = new Psr7\Response(200, [], $body); Psr7\rewind_body($res); }
private function createRequestFromFollow(Step $step, ValueBag $values, RequestInterface $request = null, ResponseInterface $response = null, Crawler $crawler = null) { if (null === $request || null === $response) { throw new CrawlException('Unable to follow when no previous page.'); } if ('3' !== substr($response->getStatusCode(), 0, 1) || !$response->hasHeader('Location')) { throw new CrawlException('Unable to follow when no previous page is not a redirect.'); } // logic from Guzzle\RedirectMiddleware // Request modifications to apply. $modify = []; // Use a GET request if this is an entity enclosing request and we are // not forcing RFC compliance, but rather emulating what all browsers // would do. $statusCode = $response->getStatusCode(); if ($statusCode == 303 || $statusCode <= 302 && $request->getBody()) { $modify['method'] = 'GET'; $modify['body'] = ''; } $modify['uri'] = Psr7\Uri::resolve($request->getUri(), $response->getHeaderLine('Location')); Psr7\rewind_body($request); // Add the Referer header only if we are not redirecting from https to http if ($modify['uri']->getScheme() === $request->getUri()->getScheme()) { $modify['set_headers']['Referer'] = (string) $request->getUri()->withUserInfo('', ''); } else { $modify['remove_headers'][] = 'Referer'; } // Remove Authorization header if host is different if ($request->getUri()->getHost() !== $modify['uri']->getHost()) { $modify['remove_headers'][] = 'Authorization'; } return Psr7\modify_request($request, $modify); }
/** * Parse the reponse in the Exception to return the Exact error messages * @param Exception $e * @throws ApiException */ private function parseExceptionForErrorMessages(Exception $e) { if (!$e instanceof BadResponseException) { throw new ApiException($e->getMessage()); } $response = $e->getResponse(); Psr7\rewind_body($response); $responseBody = $response->getBody()->getContents(); $decodedResponseBody = json_decode($responseBody, true); if (!is_null($decodedResponseBody) && isset($decodedResponseBody['error']['message']['value'])) { $errorMessage = $decodedResponseBody['error']['message']['value']; } else { $errorMessage = $responseBody; } throw new ApiException('Error ' . $response->getStatusCode() . ': ' . $errorMessage); }
/** * @param RequestInterface $request * @param array $options * @param ResponseInterface $response * * @return RequestInterface */ public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response) { // Request modifications to apply. $modify = []; $protocols = $options['allow_redirects']['protocols']; // Use a GET request if this is an entity enclosing request and we are // not forcing RFC compliance, but rather emulating what all browsers // would do. $statusCode = $response->getStatusCode(); if ($statusCode == 303 || $statusCode <= 302 && $request->getBody() && !$options['allow_redirects']['strict']) { $modify['method'] = 'GET'; $modify['body'] = ''; } $modify['uri'] = $this->redirectUri($request, $response, $protocols); Psr7\rewind_body($request); // Add the Referer header if it is told to do so and only // add the header if we are not redirecting from https to http. if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) { $uri = $request->getUri()->withUserInfo('', ''); $modify['set_headers']['Referer'] = (string) $uri; } else { $modify['remove_headers'][] = 'Referer'; } return Psr7\modify_request($request, $modify); }