コード例 #1
0
 /**
  * @test
  */
 public function deleteDocumentAndCreateNewMessageWhenDocumentIsMoved()
 {
     $redirect_url = "http://redirect.example.com";
     $message = new AMQPMessage();
     $message->body = json_encode(['url' => 'https://github.com', 'base_url' => 'https://github.com', 'blacklist' => [], 'metadata' => ['core' => 'core1'], 'whitelist' => []]);
     $crawlJob = new CrawlJob($redirect_url, 'https://github.com', [], ['core' => 'core1'], []);
     $queue = $this->getMockBuilder('Simgroep\\ConcurrentSpiderBundle\\Queue')->disableOriginalConstructor()->setMethods(['__destruct', 'listen', 'publishJob', 'rejectMessage'])->getMock();
     $queue->expects($this->once())->method('rejectMessage')->with($this->equalTo($message));
     $queue->expects($this->once())->method('publishJob')->with($this->equalTo($crawlJob));
     $indexer = $this->getMockBuilder('Simgroep\\ConcurrentSpiderBundle\\Indexer')->disableOriginalConstructor()->setMethods(['isUrlIndexedAndNotExpired', 'deleteDocument'])->getMock();
     $indexer->expects($this->once())->method('isUrlIndexedAndNotExpired')->with($this->equalTo('https://github.com'))->will($this->returnValue(false));
     $indexer->expects($this->once())->method('deleteDocument')->with($this->equalTo($message))->will($this->returnValue(false));
     $client = $this->getMockBuilder('Guzzle\\Http\\Client')->disableOriginalConstructor()->setMethods(['setUserAgent'])->getMock();
     $client->setConfig(new Collection());
     $requestHandler = $this->getMockBuilder('VDB\\Spider\\RequestHandler\\GuzzleRequestHandler')->disableOriginalConstructor()->setMethods(['getClient'])->getMock();
     $requestHandler->expects($this->exactly(2))->method('getClient')->will($this->returnValue($client));
     $response = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->setMethods(['getInfo', 'getStatusCode'])->getMock();
     $response->expects($this->once())->method('getStatusCode')->will($this->returnValue("301"));
     $response->expects($this->once())->method('getInfo')->with($this->equalTo('redirect_url'))->will($this->returnValue($redirect_url));
     $exception = new ClientErrorResponseException(sprintf("Page moved to %s", $redirect_url), 301);
     $exception->setResponse($response);
     $spider = $this->getMockBuilder('Simgroep\\ConcurrentSpiderBundle\\Spider')->setMethods(['getRequestHandler', 'crawl'])->disableOriginalConstructor()->getMock();
     $spider->expects($this->exactly(2))->method('getRequestHandler')->will($this->returnValue($requestHandler));
     $spider->expects($this->once())->method("crawl")->willThrowException($exception);
     $userAgent = 'I am some agent';
     $logger = $this->getMockBuilder('Monolog\\Logger')->disableOriginalConstructor()->setMethods(['info', 'warning', 'emergency'])->getMock();
     $command = $this->getMockBuilder('Simgroep\\ConcurrentSpiderBundle\\Command\\CrawlCommand')->setConstructorArgs([$queue, $indexer, $spider, $userAgent, $logger])->setMethods(null)->getMock();
     $command->crawlUrl($message);
 }
コード例 #2
0
ファイル: VaultSourceTest.php プロジェクト: SaveYa/Encryption
 /**
  * @expectedException \Guzzle\Http\Exception\ClientErrorResponseException
  */
 public function testGetWhereClientExceptionThrownButNot404()
 {
     $faker = Factory::create();
     $key = $faker->unique()->word;
     $valueField = $faker->unique()->word;
     $mount = $faker->unique()->word;
     $apiVersion = $faker->unique()->word;
     $exception = new ClientErrorResponseException();
     $exception->setResponse(\Mockery::mock('Guzzle\\Http\\Message\\Response')->shouldReceive('getStatusCode')->andReturn(400)->getMock());
     $client = \Mockery::mock('Guzzle\\Http\\Client')->shouldReceive('get')->with(sprintf('/%s/%s/%s', $apiVersion, $mount, $key))->andReturn(\Mockery::mock()->shouldReceive('send')->andThrow($exception)->getMock())->getMock();
     $source = new VaultSource($client, $mount, $valueField, $apiVersion);
     $source->get($key);
 }
コード例 #3
0
 /**
  * @expectedException PayPalRestApiClient\Exception\CallException
  * @expectedExceptionMessage Error requesting token: response status 401 Unauthorized,  reason: error: invalid_client, error_description: Client secret does not match for this client
  */
 public function testWrongResponseGivenShouldThrowPayPalCallException()
 {
     $clientId = 'example';
     $secret = 'example';
     $expectedRequest = $this->getMockBuilder('Guzzle\\Http\\Message\\Request')->disableOriginalConstructor()->getMock();
     $expectedResponse = $this->getMockBuilder('Guzzle\\Http\\Message\\Response')->disableOriginalConstructor()->setMethods(array('getStatusCode', 'getBody', 'getReasonPhrase'))->getMock();
     $expectedResponse->expects($this->any())->method('getReasonPhrase')->will($this->returnValue('Unauthorized'));
     $expectedResponse->expects($this->any())->method('getStatusCode')->will($this->returnValue(401));
     $expectedResponse->expects($this->any())->method('getBody')->will($this->returnValue('{"error":"invalid_client","error_description":"Client secret does not match for this client"}'));
     $this->client = $this->getMockBuilder('Guzzle\\Http\\Client')->disableOriginalConstructor()->setMethods(array('createRequest', 'send'))->getMock();
     $this->client->expects($this->once())->method('createRequest')->with('POST', 'https://api.sandbox.paypal.com/v1/oauth2/token', array('Accept' => 'application/json', 'Accept-Language' => 'en_US', 'Content-Type' => 'application/x-www-form-urlencoded'), 'grant_type=client_credentials', array('auth' => array($clientId, $secret), 'debug' => true))->will($this->returnValue($expectedRequest));
     $exception = new ClientErrorResponseException();
     $exception->setResponse($expectedResponse);
     $this->client->expects($this->once())->method('send')->with($expectedRequest)->will($this->throwException($exception));
     $repo = new AccessTokenRepository($this->client, 'https://api.sandbox.paypal.com', true);
     $token = $repo->getAccessToken($clientId, $secret);
 }
コード例 #4
0
 public function testIsAuthenticationValidInvalidCredentials()
 {
     $that = $this;
     $request = $this->createMockRequest(function ($request) use($that) {
         return $that->throwException(ClientErrorResponseException::factory($request, $that->createClientErrorResponse(400)));
     });
     $this->getMockHttpClient()->expects($this->once())->method('post')->will($this->returnValue($request));
     $this->assertFalse($this->resource->isAuthenticationValid('foo', 'bar'));
 }
コード例 #5
0
 public function testGetUserByUsernameNotFound()
 {
     $that = $this;
     $this->getMockHttpClient()->expects($this->once())->method('get')->will($this->returnValue($this->createMockRequest(function ($request) use($that) {
         return $that->throwException(ClientErrorResponseException::factory($request, $that->createClientErrorResponse(404)));
     })));
     $this->setExpectedException('Seiffert\\CrowdRestBundle\\Exception\\UserNotFoundException');
     $this->resource->getUserByUsername('test');
 }
コード例 #6
0
 /**
  * 
  * @param \Guzzle\Http\Exception\ClientErrorResponseException $e
  * @throws Exception\NotFoundException
  * @throws \Guzzle\Http\Exception\ClientErrorResponseException
  */
 protected function handleResponseException(ClientErrorResponseException $e)
 {
     switch ($e->getResponse()->getStatusCode()) {
         case 400:
             throw new Exception\InvalidRequestException('Invalid request', 400, $e);
         case 403:
             throw new Exception\PermissionDeniedException('Permission denied', 403, $e);
         case 404:
             throw new Exception\NotFoundException('Item does not exist', 404, $e);
         default:
             throw $e;
     }
 }
コード例 #7
0
 /**
  * @param $e
  * @return bool
  */
 public function handleException(\Guzzle\Http\Exception\ClientErrorResponseException $e)
 {
     $response = $e->getResponse();
     $this->errorQueue->enqueue($response->getBody(true));
     return false;
 }
コード例 #8
0
 /**
  * @param ClientErrorResponseException $e
  */
 protected function handleNotFoundException(ClientErrorResponseException $e)
 {
     if ($e->getResponse()->getStatusCode() !== 404) {
         throw $e;
     }
 }
コード例 #9
0
ファイル: Api.php プロジェクト: ftdysa/insight
 private function processClientError(ClientErrorResponseException $e)
 {
     $statusCode = $e->getResponse()->getStatusCode();
     $reasonPhrase = $e->getResponse()->getReasonPhrase();
     $error = null;
     $message = sprintf('Your request in not valid (status code: "%d", reason phrase: "%s").', $statusCode, $reasonPhrase);
     if (400 == $statusCode) {
         $error = $this->parser->parseError((string) $e->getResponse()->getBody());
         $message .= 'See $error attached to the exception';
     }
     throw new ApiClientException($message, $error, 0, $e);
 }
コード例 #10
0
 /**
  * Function that crawls one webpage based on the give url.
  *
  * @param \Simgroep\ConcurrentSpiderBundle\CrawlJob $crawlJob
  */
 public function crawl(CrawlJob $crawlJob)
 {
     $this->currentCrawlJob = $crawlJob;
     $resource = $this->requestHandler->request(new Uri($crawlJob->getUrl()));
     if ($resource->getResponse()->getStatusCode() == 301) {
         $exception = new ClientErrorResponseException(sprintf("Page moved to %s", $resource->getResponse()->getInfo('redirect_url')), 301);
         $exception->setResponse($resource->getResponse());
         throw $exception;
     }
     $uris = [];
     $this->eventDispatcher->dispatch(SpiderEvents::SPIDER_CRAWL_PRE_DISCOVER);
     $baseUrl = $resource->getUri()->toString();
     $crawler = $resource->getCrawler()->filterXPath('//a');
     foreach ($crawler as $node) {
         try {
             if ($node->getAttribute("rel") === "nofollow") {
                 continue;
             }
             $href = $node->getAttribute('href');
             $uri = new Uri($href, $baseUrl);
             $uris[] = $uri;
         } catch (UriSyntaxException $e) {
             //too bad
         }
     }
     $crawler = $resource->getCrawler()->filterXPath('//loc');
     foreach ($crawler as $node) {
         try {
             $href = $node->nodeValue;
             $uri = new Uri($href, $baseUrl);
             $uris[] = $uri;
         } catch (UriSyntaxException $e) {
             //too bad
         }
     }
     $this->eventDispatcher->dispatch(SpiderEvents::SPIDER_CRAWL_POST_DISCOVER, new GenericEvent($this, ['uris' => $uris]));
     $this->persistenceHandler->persist($resource, $crawlJob);
 }
コード例 #11
0
 /**
  * @param \Guzzle\Http\Exception\ClientErrorResponseException $exception
  * @throws \Searchperience\Common\Http\Exception\InternalServerErrorException
  * @throws \Searchperience\Common\Http\Exception\ForbiddenException
  * @throws \Searchperience\Common\Http\Exception\ClientErrorResponseException
  * @throws \Searchperience\Common\Http\Exception\DocumentNotFoundException
  * @throws \Searchperience\Common\Http\Exception\UnauthorizedException
  * @throws \Searchperience\Common\Http\Exception\MethodNotAllowedException
  * @throws \Searchperience\Common\Http\Exception\RequestEntityTooLargeException
  * @return void
  */
 protected function transformStatusCodeToClientErrorResponseException(\Guzzle\Http\Exception\ClientErrorResponseException $exception)
 {
     switch ($exception->getResponse()->getStatusCode()) {
         case 401:
             $e = new \Searchperience\Common\Http\Exception\UnauthorizedException($exception->getMessage(), 1353574907, $exception);
             $e->setResponse($exception->getResponse());
             throw $e;
             break;
         case 403:
             $e = new \Searchperience\Common\Http\Exception\ForbiddenException($exception->getMessage(), 1353574915, $exception);
             $e->setResponse($exception->getResponse());
             throw $e;
             break;
         case 404:
             $e = new \Searchperience\Common\Http\Exception\EntityNotFoundException($exception->getMessage(), 1353574919, $exception);
             $e->setResponse($exception->getResponse());
             throw $e;
             break;
         case 405:
             $e = new \Searchperience\Common\Http\Exception\MethodNotAllowedException($exception->getMessage(), 1353574923, $exception);
             $e->setResponse($exception->getResponse());
             throw $e;
             break;
         case 413:
             $e = new \Searchperience\Common\Http\Exception\RequestEntityTooLargeException($exception->getMessage(), 1353574956, $exception);
             $e->setResponse($exception->getResponse());
             throw $e;
             break;
         default:
             $e = new \Searchperience\Common\Http\Exception\ClientErrorResponseException($exception->getMessage(), 1353574962, $exception);
             $e->setResponse($exception->getResponse());
             throw $e;
     }
 }
コード例 #12
0
ファイル: RequestTest.php プロジェクト: allegro/toper
 /**
  * @param GuzzleResponse $response
  * @return ClientErrorResponseException
  */
 private function createGuzzleClientException(GuzzleResponse $response)
 {
     $e = new ClientErrorResponseException();
     $e->setResponse($response);
     return $e;
 }
コード例 #13
0
 public function __construct(FormError $form, $message = '', $code = 0, \Exception $previous = null)
 {
     $this->form = $form;
     parent::__construct($message, $code, $previous);
 }