Ejemplo n.º 1
0
 public function testFetch()
 {
     $url = 'http://google.com';
     $redirect = 'https://www.google.com';
     $ua = 'IO Crawler/1.0';
     $body = 'test';
     $headers = ['content-length' => [1234], 'content-type' => ['text/plain']];
     $response = new GuzzleResponse(200, $headers, $body);
     $this->guzzle->expects($this->once())->method('request')->with('GET', $url, $this->callback(function (array $options) use($ua) {
         $this->assertArraySubset(['headers' => ['User-Agent' => $ua]], $options);
         return true;
     }))->will($this->returnCallback(function () use($url, $redirect, $response) {
         $this->client->setEffectiveUri($url, $redirect);
         return $response;
     }));
     $result = $this->client->fetch($url, $ua);
     $this->assertInternalType('array', $result);
     $this->assertCount(2, $result);
     /** @var ResponseInterface $response */
     list($effectiveUrl, $response) = $result;
     $this->assertEquals($redirect, $effectiveUrl);
     $this->assertInstanceOf(ResponseInterface::class, $response);
     $this->assertArraySubset($headers, $response->getHeaders());
     $this->assertEquals($body, $response->getBody()->getContents());
 }
Ejemplo n.º 2
0
 public function testDeleteIndexRequestContent()
 {
     $request = new DeleteIndexRequest();
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('DELETE'), $this->equalTo('/search/index/index-name'))->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willReturn($httpResponse);
     $request->name = 'index-name';
     $this->assertInstanceOf('Riak\\Client\\Core\\Message\\Search\\DeleteIndexResponse', $this->instance->send($request));
 }
Ejemplo n.º 3
0
 /**
  * @expectedException Riak\Client\Core\Transport\RiakTransportException
  * @expectedExceptionMessage Unexpected status code : "555"
  */
 public function testUnexpectedHttpStatusCode()
 {
     $request = new MapReduceRequest();
     $httpQuery = $this->getMock('GuzzleHttp\\Query');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $this->client->expects($this->once())->method('createRequest')->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->willReturn($httpResponse);
     $httpRequest->expects($this->once())->method('getQuery')->willReturn($httpQuery);
     $httpQuery->expects($this->once())->method('add')->willReturn($httpRequest);
     $httpResponse->expects($this->any())->method('getStatusCode')->willReturn(555);
     $request->request = '';
     $this->instance->send($request);
 }
Ejemplo n.º 4
0
 public function testGetSchemaRequestContent()
 {
     $request = new GetSchemaRequest();
     $body = Stream::factory('schema-content');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('GET'), $this->equalTo('/search/schema/schema-name'))->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willReturn($httpResponse);
     $httpResponse->expects($this->once())->method('getBody')->willReturn($body);
     $request->name = 'schema-name';
     $response = $this->instance->send($request);
     $this->assertInstanceOf('Riak\\Client\\Core\\Message\\Search\\GetSchemaResponse', $response);
     $this->assertEquals((string) $body, $response->content);
     $this->assertEquals('schema-name', $response->name);
 }
Ejemplo n.º 5
0
 public function testGetIndexRequestContent()
 {
     $request = new GetIndexRequest();
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('GET'), $this->equalTo('/search/index/index-name'))->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willReturn($httpResponse);
     $httpResponse->expects($this->once())->method('json')->willReturn(['n_val' => 3, 'name' => 'index-name', 'schema' => 'schema-name']);
     $request->name = 'index-name';
     $response = $this->instance->send($request);
     $this->assertInstanceOf('Riak\\Client\\Core\\Message\\Search\\GetIndexResponse', $response);
     $this->assertEquals('schema-name', $response->schema);
     $this->assertEquals('index-name', $response->name);
     $this->assertEquals(3, $response->nVal);
 }
Ejemplo n.º 6
0
 /**
  * @expectedException Riak\Client\Core\Transport\RiakTransportException
  * @expectedExceptionMessage Not Found
  * @expectedExceptionCode 404
  */
 public function testWrappedExceptioThrownByGuzzle()
 {
     $request = new GetRequest();
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $httpQuery = $this->getMock('GuzzleHttp\\Query');
     $request->notfoundOk = false;
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('GET'), $this->equalTo('/types/default/buckets/test_bucket/keys/1'))->willReturn($httpRequest);
     $httpResponse->expects($this->once())->method('getStatusCode')->willReturn(404);
     $httpRequest->expects($this->once())->method('getQuery')->willReturn($httpQuery);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willThrowException(new ClientException('Not Found', $httpRequest, $httpResponse));
     $request->bucket = 'test_bucket';
     $request->type = 'default';
     $request->key = '1';
     $this->instance->send($request);
 }
 /**
  * Sets Guzzle expectations.
  *
  * @param string $action
  * @param mixed $contentId
  * @param int $contentTypeId
  * @param int $customerId
  * @param string $serverUri
  * @param string $licenseKey
  * @param string $apiEndpoint
  */
 protected function setGuzzleExpectations($action, $contentId, $contentTypeId, $customerId, $serverUri, $licenseKey, $apiEndpoint)
 {
     if (method_exists($this->guzzleClientMock, 'post')) {
         $this->guzzleClientMock->expects($this->once())->method('post')->with($this->equalTo($this->getExpectedEndpoint($apiEndpoint, $customerId)), $this->equalTo($this->getNotificationBody($action, $contentId, $contentTypeId, $serverUri, $customerId, $licenseKey)))->will($this->returnValue(new Response(202)));
     } else {
         $this->guzzleClientMock->expects($this->once())->method('requestAsync')->with('POST', $this->equalTo($this->getExpectedEndpoint($apiEndpoint, $customerId)), $this->equalTo($this->getNotificationBody($action, $contentId, $contentTypeId, $serverUri, $customerId, $licenseKey)))->will($this->returnValue(new Promise()));
     }
 }
Ejemplo n.º 8
0
 /**
  * @expectedException Riak\Client\Core\Transport\RiakTransportException
  * @expectedExceptionMessage Unexpected status code : "555"
  */
 public function testUnexpectedHttpStatusCode()
 {
     $request = new PutRequest();
     $httpQuery = $this->getMock('GuzzleHttp\\Query');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $this->client->expects($this->once())->method('createRequest')->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->willReturn($httpResponse);
     $httpRequest->expects($this->once())->method('getQuery')->willReturn($httpQuery);
     $httpQuery->expects($this->any())->method('add')->willReturn($httpRequest);
     $httpResponse->expects($this->any())->method('getStatusCode')->willReturn(555);
     $request->op = new CounterOp(10);
     $request->bucket = 'test_bucket';
     $request->type = 'default';
     $request->key = '1';
     $this->instance->send($request);
 }
Ejemplo n.º 9
0
 /**
  * @covers \Smartling\File\FileApi::import
  */
 public function testImport()
 {
     $locale = 'en-EN';
     $endpointUrl = vsprintf('%s/%s/locales/%s/file/import', [FileApi::ENDPOINT_URL, $this->projectId, $locale]);
     $this->client->expects(self::once())->method('request')->with('POST', $endpointUrl, ['headers' => ['Accept' => 'application/json', 'Authorization' => vsprintf(' %s %s', [$this->authProvider->getTokenType(), $this->authProvider->getAccessToken()])], 'http_errors' => false, 'multipart' => [['name' => 'file', 'contents' => $this->streamPlaceholder], ['name' => 'fileUri', 'contents' => 'test.xml'], ['name' => 'fileType', 'contents' => 'xml'], ['name' => 'translationState', 'contents' => 'PUBLISHED'], ['name' => 'overwrite', 'contents' => '0']]])->willReturn($this->responseMock);
     $this->invokeMethod($this->object, 'setAuth', [$this->authProvider]);
     $this->object->import($locale, 'test.xml', 'xml', 'tests/resources/test.xml', 'PUBLISHED', false);
 }
Ejemplo n.º 10
0
 public function testPutSchemaRequestContent()
 {
     $request = new PutSchemaRequest();
     $body = Stream::factory('schema-content');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $callback = function ($subject) {
         $this->assertInstanceOf('GuzzleHttp\\Stream\\Stream', $subject);
         $this->assertEquals('schema-content', (string) $subject);
         return true;
     };
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('PUT'), $this->equalTo('/search/schema/schema-name'))->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willReturn($httpResponse);
     $httpRequest->expects($this->once())->method('setBody')->with($this->callback($callback))->willReturn($body);
     $request->name = 'schema-name';
     $request->content = 'schema-content';
     $this->assertInstanceOf('Riak\\Client\\Core\\Message\\Search\\PutSchemaResponse', $this->instance->send($request));
 }
Ejemplo n.º 11
0
 /**
  * Setup pieces for testing exception passing
  *
  * @param string $message
  */
 protected function setUpExceptionTest($message)
 {
     $secondWarning = rand();
     $this->guzzle->expects($this->at(1))->method('request')->will($this->throwException(new Exception($secondWarning)));
     $logger = $this->getMock('\\Psr\\Log\\LoggerInterface');
     $logger->expects($this->at(0))->method('warning')->with($this->equalTo($message));
     $logger->expects($this->at(1))->method('warning')->with($this->equalTo('Message: `' . $secondWarning . '`'));
     $this->fixture->setLogger($logger);
 }
Ejemplo n.º 12
0
 /**
  * Make sure that an exception with a error response is wrapped properly.
  *
  * @return void
  */
 public function testSendConnectorException()
 {
     $this->response->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('application/json'));
     $data = ['error_code' => 'ERROR_CODE_1', 'error_messages' => ['Oh dear...', 'Oh no...'], 'correlation_id' => 'corr_id_1'];
     $this->response->expects($this->once())->method('json')->will($this->returnValue($data));
     $exception = new RequestException('Something went terribly wrong', $this->request, $this->response);
     $this->client->expects($this->once())->method('send')->with($this->request)->will($this->throwException($exception));
     $this->setExpectedException('Klarna\\Rest\\Transport\\Exception\\ConnectorException', 'ERROR_CODE_1: Oh dear..., Oh no... (#corr_id_1)');
     $this->object->send($this->request);
 }
Ejemplo n.º 13
0
 /**
  * Test the NowPlaying string to SimpleXML parsing
  *
  * @param string[]           $xmlList  Array of XML strings
  * @param SimpleXMLElement[] $expected Array of XML Elements
  *
  * @dataProvider guzzleReturnParsingProvider
  */
 public function testGuzzleReturnParsing($xmlList, $expected)
 {
     foreach ($xmlList as $index => $xmlString) {
         $response = $this->getMock('\\Psr\\Http\\Message\\ResponseInterface');
         $response->method('getBody')->willReturn($xmlString);
         $response->method('getStatusCode')->willReturn(200);
         $this->guzzle->expects($this->at($index))->method('send')->will($this->returnValue($response));
     }
     $actual = $this->fixture->download();
     $this->assertEquals($expected, $actual);
 }
Ejemplo n.º 14
0
 public function testPutIndexRequestContent()
 {
     $request = new PutIndexRequest();
     $body = Stream::factory('index-content');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $callback = function ($subject) {
         $json = json_decode($subject, true);
         $this->assertEquals('schema-content', $json['schema']);
         $this->assertEquals(3, $json['n_val']);
         return true;
     };
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('PUT'), $this->equalTo('/search/index/index-name'))->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willReturn($httpResponse);
     $httpRequest->expects($this->once())->method('setBody')->with($this->callback($callback))->willReturn($body);
     $request->nVal = 3;
     $request->name = 'index-name';
     $request->schema = 'schema-content';
     $this->assertInstanceOf('Riak\\Client\\Core\\Message\\Search\\PutIndexResponse', $this->instance->send($request));
 }
Ejemplo n.º 15
0
 public function testSearchRequestContent()
 {
     $request = new SearchRequest();
     $query = $this->getMock('GuzzleHttp\\Query');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('GET'), $this->equalTo('/search/query/index-name'))->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->with($this->equalTo($httpRequest))->willReturn($httpResponse);
     $httpRequest->expects($this->once())->method('getQuery')->willReturn($query);
     $httpResponse->expects($this->once())->method('json')->willReturn(['response' => ['numFound' => 2, 'maxScore' => 1, 'docs' => [['name' => 'Fabio B. Silva', 'username' => 'fabios'], ['name' => 'Fabio B. Silva', 'username' => 'FabioBatSilva']]]]);
     $request->index = 'index-name';
     $request->q = 'name:Fabio*';
     $result = $this->instance->send($request);
     $this->assertInstanceOf('Riak\\Client\\Core\\Message\\Search\\SearchResponse', $result);
     $this->assertEquals(2, $result->numFound);
     $this->assertEquals(1, $result->maxScore);
     $this->assertCount(2, $result->docs);
     $this->assertArrayHasKey('name', $result->docs[0]);
     $this->assertArrayHasKey('name', $result->docs[1]);
     $this->assertArrayHasKey('username', $result->docs[0]);
     $this->assertArrayHasKey('username', $result->docs[1]);
 }
Ejemplo n.º 16
0
 /**
  * Tests search() success.
  *
  * @covers ::login
  * @covers ::upload
  * @covers ::doRequest
  *
  * @test
  */
 public function searchCorrectlyPassesParametersToRequest()
 {
     $mock_login_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock();
     $mock_login_response->expects($this->once())->method('getStatusCode')->willReturn(200);
     $mock_login_response->expects($this->once())->method('getBody')->willReturn(file_get_contents('expected/login-expected-good-response.json', TRUE));
     // This sucks, returnValueMap wasn't working though.
     $this->client->expects($this->at(0))->method('request')->with('POST', self::EXAMPLE_LOGIN_URL, $this->defaultLoginOptions)->willReturn($mock_login_response);
     $mock_search_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock();
     $mock_search_response->expects($this->once())->method('getStatusCode')->willReturn(200);
     $search_response_body = file_get_contents('expected/search-expected-good-response.json', TRUE);
     $mock_search_response->expects($this->once())->method('getBody')->willReturn($search_response_body);
     $options = $this->defaultOptions;
     $body = ['page' => 2, 'hitsperpage' => 10, 'showfilters' => "true", 'query' => ['terms' => [['field' => 'name', 'operator' => 'matches', 'value' => 'test*']]]];
     $options['json'] = $body;
     $this->client->expects($this->at(1))->method('request')->with('POST', self::EXAMPLE_SEARCH_URL, $options)->willReturn($mock_search_response);
     $decoded_body = $this->serializer->decode($search_response_body);
     $filters = [['field' => 'name', 'operator' => 'matches', 'value' => 'test*']];
     $this->assertEquals($decoded_body, $this->emdbClient->search(2, 10, $filters));
 }
Ejemplo n.º 17
0
 /**
  * @expectedException Riak\Client\Core\Transport\RiakTransportException
  * @expectedExceptionMessage Unexpected status code : "555"
  */
 public function testUnexpectedHttpStatusCode()
 {
     $content = new Content();
     $request = new PutRequest();
     $httpQuery = $this->getMock('GuzzleHttp\\Query');
     $httpRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface');
     $httpResponse = $this->getMock('GuzzleHttp\\Message\\ResponseInterface');
     $request->bucket = 'test_bucket';
     $request->type = 'default';
     $request->key = '1';
     $request->returnBody = true;
     $request->content = $content;
     $request->vClock = 'vclock-hash';
     $content->contentType = 'application/json';
     $content->value = '[1,1,1]';
     $this->client->expects($this->once())->method('createRequest')->willReturn($httpRequest);
     $this->client->expects($this->once())->method('send')->willReturn($httpResponse);
     $httpRequest->expects($this->once())->method('getQuery')->willReturn($httpQuery);
     $httpQuery->expects($this->any())->method('add')->willReturn($httpRequest);
     $httpResponse->expects($this->any())->method('getStatusCode')->willReturn(555);
     $this->instance->send($request);
 }
Ejemplo n.º 18
0
 public function testCreateIndexRequest()
 {
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('GET'), $this->equalTo('/search/index/index-name'));
     $this->invokeMethod($this->instance, 'createIndexRequest', ['GET', 'index-name']);
 }
Ejemplo n.º 19
0
 public function testTwoFactorTokenPost()
 {
     $this->transport->expects($this->once())->method('send')->with($this->isRequestFor('POST', '/'), $this->isValidOptionsArray(['CB-2FA-TOKEN']));
     $this->client->post('/', ['foo' => 'bar', Param::TWO_FACTOR_TOKEN => 'TOKEN']);
 }
 /**
  * @param ClientInterface $clientMock
  * @param string $loginResponseHtml
  */
 private function performFullLoginProcess(ClientInterface $clientMock, $loginResponseHtml)
 {
     $homePageRequest = new Request('GET', 'https://www.fshare.vn/');
     $loginRequest = new Request('POST', 'https://www.fshare.vn/login');
     $loginOptions = ['form_params' => ['fs_csrf' => 'xxx', 'LoginForm[email]' => $this->email, 'LoginForm[password]' => $this->password, 'LoginForm[checkloginpopup]' => 0, 'LoginForm[rememberMe]' => 1, 'yt0' => 'Đăng nhập']];
     $clientMock->expects(static::exactly(2))->method('send')->withConsecutive([$homePageRequest, []], [$loginRequest, $loginOptions])->willReturnOnConsecutiveCalls(new Response(200, [], '<body><form id="_login-form"><input name="fs_csrf" value="xxx"></form></body>'), new Response(200, [], $loginResponseHtml));
     $clientMock->expects(static::once())->method('getConfig')->with('cookies')->willReturn(new CookieJar());
     $this->requestDecoratorMock->expects(static::exactly(2))->method('decorate')->willReturnArgument(0);
 }
 protected function setGuzzleExpectations($action, $contentId)
 {
     $this->guzzleClientMock->expects($this->once())->method('post')->with($this->equalTo($this->getExpectedEndpoint()), $this->equalTo($this->getNotificationBody($action, $contentId)))->will($this->returnValue(new Response(202)));
 }
Ejemplo n.º 22
0
 public function testCreateRequest()
 {
     $this->client->expects($this->once())->method('createRequest')->with($this->equalTo('GET'), $this->equalTo('/types/type/buckets/bucket/props'));
     $this->invokeMethod($this->instance, 'createRequest', ['GET', 'type', 'bucket', 'key']);
 }