public function testAuthRequestFailure() { // create a client with invalid credentials $chargify = new ChargifyV2(['api_id' => 'fdsafdsaf', 'api_password' => 'fgsdgfdsg', 'api_secret' => 'fdsfdsaf']); $direct = $chargify->direct(); // set a fake redirect URL. Chargify will 500 on us if we don't have a redirect URL $direct->setRedirect('http://localhost'); // set mock response $mock = new MockHandler([Psr7\parse_response(MockResponse::read('v2.authTest.error'))]); $handler = HandlerStack::create($mock); $chargify->getHttpClient()->getConfig('handler')->setHandler($handler); $utilityAuthRequest = new AuthRequest($direct); $success = $utilityAuthRequest->test(); $response = $utilityAuthRequest->getLastResponse(); // test should have failed $this->assertFalse($success); // status code should be 200 $this->assertEquals(200, $response->getStatusCode()); // chargify should not redirect us $locationHeader = $response->getHeader('Location'); $this->assertTrue(empty($locationHeader)); // body should contain 'Incorrect signature' $bodyIsInvalid = 0 === strcasecmp('Incorrect signature', trim((string) $response->getBody())); $this->assertTrue($bodyIsInvalid); }
/** * Get a Chargify client instance * * @param string $mockResponseFile Filename containing mocked response * @param string $env (test|dev) * * @return Chargify */ public static function getInstance($mockResponseFile = null, $env = 'test') { $config = array(); switch ($env) { case 'test': $config = (require dirname(__DIR__) . '/configs/ClientConfig.Test.php'); break; case 'dev': $config = (require dirname(__DIR__) . '/configs/ClientConfig.Dev.php'); break; } $chargify = new Chargify($config); if (!empty($mockResponseFile)) { $mock = new MockHandler([Psr7\parse_response(MockResponse::read($mockResponseFile))]); $handler = HandlerStack::create($mock); // $logger = new Logger('Logger'); // $logger->pushHandler(new StreamHandler(dirname(__DIR__) . '/artifacts/logs/guzzle.log', Logger::DEBUG)); // // $middleware = new LoggerMiddleware($logger); // $template = MessageFormatter::DEBUG; // $middleware->setFormatter(new MessageFormatter($template)); // // $handler->push($middleware); $chargify->getHttpClient()->getConfig('handler')->setHandler($handler); } return $chargify; }
/** * {@inheritdoc} */ public function fetch(RequestInterface $request) { $path = $this->getPath($request); if (!file_exists($path)) { throw new \RuntimeException('Record not found.'); } return Psr7\parse_response(file_get_contents($path)); }
protected function getFixture($file) { if (!$this->rootFixturesDir) { throw new \RuntimeException('Root fixtures dir not set'); } $path = $this->rootFixturesDir . '/Fixtures/' . $file . '.resp'; if (!file_exists($path)) { throw new \RuntimeException(sprintf("%s does not exist", $path)); } return parse_response(file_get_contents($path)); }
/** * {@inheritdoc} */ public function fetch(RequestInterface $request) { $path = $this->getPath($request); if (!file_exists($path)) { // Try to find file without host (for BC) $path = $this->getPath($request, false); if (!file_exists($path)) { throw new \RuntimeException('Record not found.'); } } return Psr7\parse_response(file_get_contents($path)); }
/** * Get a Chargify client instance * * @param string $mockResponseFile Filename containing mocked response * @param string $env (test|dev) * * @return ChargifyV2 */ public static function getInstance($mockResponseFile = null, $env = 'test') { $config = array(); switch ($env) { case 'test': $config = (require dirname(__DIR__) . '/configs/ClientV2Config.Test.php'); break; case 'dev': $config = (require dirname(__DIR__) . '/configs/ClientV2Config.Dev.php'); break; } $chargify = new ChargifyV2($config); if (!empty($mockResponseFile)) { $mock = new MockHandler([Psr7\parse_response(MockResponse::read($mockResponseFile))]); $handler = HandlerStack::create($mock); $chargify->getHttpClient()->getConfig('handler')->setHandler($handler); } return $chargify; }
public function __invoke(RequestInterface $request, array $options) { if (isset($options['delay'])) { usleep($options['delay'] * 1000); } if (file_exists($this->getFullFilePath($request))) { $responseData = file_get_contents($this->getFullFilePath($request)); $response = Psr7\parse_response($responseData); } else { $curlHandler = new CurlHandler(); $response = $curlHandler->__invoke($request, $options); } $response = $response instanceof \Exception ? new RejectedPromise($response) : \GuzzleHttp\Promise\promise_for($response); return $response->then(function ($value) use($request, $options) { // record the response $this->record($request, $value, null, $options); return $value; }, function ($reason) use($request, $options) { // record the response $this->record($request, null, $reason, $options); return $reason; }); }
protected function parseResponse($data) { $psrResponse = gPsr\parse_response($data); $headers = array_map(function ($val) { if (1 === count($val)) { $val = $val[0]; } return $val; }, $psrResponse->getHeaders()); $factory = $this->getResponseFactory(); $response = $factory('HTTP', $psrResponse->getProtocolVersion(), $psrResponse->getStatusCode(), $psrResponse->getReasonPhrase(), $headers); return array($response, $psrResponse->getBody()); }
/** * @expectedException \InvalidArgumentException */ public function testValidatesResponseMessages() { Psr7\parse_response("GET / HTTP/1.1\r\n\r\n"); }
/** * We need to set the body again after deserialization because it was a stream that didn't get serialized */ public function __wakeup() { $this->response = Psr7\parse_response($this->body); }