示例#1
0
 /**
  * Test the translate API call with a text longer than the Google Translate API limit (5000 chars)
  *
  * @return void
  */
 public function testTranslateLongText()
 {
     $lipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ';
     $text = 'Bonjour ... ';
     $expectedTranslation = 'Hello ... ';
     while (strlen($text) <= 6000) {
         $text .= $lipsum;
         $expectedTranslation .= $lipsum;
     }
     $expectedTranslation = trim($expectedTranslation);
     if ($this->__mockSocket) {
         // Expects a first call with "Bonjour" and as many lipsums as needed to fill the 5000 chars
         $this->GoogleTranslate->useUserIp = false;
         $expectedText = 'Bonjour ... ';
         $response = 'Hello ... ';
         while (strlen($expectedText) + strlen($lipsum) < 5000) {
             $expectedText .= $lipsum;
             $response .= $lipsum;
         }
         $expected = array('langpair' => 'fr|en', 'format' => 'text', 'q' => $expectedText, 'v' => '1.0', 'key' => 'myApiKey');
         $this->Http->expectAt(0, 'post', array('http://ajax.googleapis.com/ajax/services/language/translate', $expected));
         $this->Http->setReturnValueAt(0, 'post', '{"responseData": {"translatedText":"' . trim($response) . '"}, "responseDetails": null, "responseStatus": 200}');
         // Expects a second call with the remaining lipsums filling the 6000 total chars
         $limit = 6000 - strlen($expectedText);
         $expectedText = '';
         while (strlen($expectedText) <= $limit) {
             $expectedText .= $lipsum;
         }
         $expected['q'] = $expectedText;
         $this->Http->expectAt(1, 'post', array('http://ajax.googleapis.com/ajax/services/language/translate', $expected));
         $this->Http->setReturnValueAt(1, 'post', '{"responseData": {"translatedText":"' . trim($expectedText) . '"}, "responseDetails": null, "responseStatus": 200}');
         $this->Http->response['status']['code'] = 200;
     }
     $result = $this->GoogleTranslate->translate($text, 'fr', 'en');
     $this->assertEqual($result, $expectedTranslation);
 }
示例#2
0
 /**
  * test that two consecutive get() calls reset the authentication credentials.
  *
  * @return void
  */
 public function testConsecutiveGetResetsAuthCredentials()
 {
     $socket = new MockHttpSocket();
     $socket->get('http://*****:*****@example.com/test');
     $this->assertEquals('mark', $socket->request['uri']['user']);
     $this->assertEquals('secret', $socket->request['uri']['pass']);
     $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
     $socket->get('/test2');
     $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
     $socket->get('/test3');
     $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
 }
示例#3
0
 /**
  * test that two consecutive get() calls reset the authentication credentials.
  *
  * @return void
  */
 function testConsecutiveGetResetsAuthCredentials()
 {
     $socket = new MockHttpSocket();
     $socket->config['request']['auth'] = array('method' => 'Basic', 'user' => 'mark', 'pass' => 'secret');
     $socket->get('http://*****:*****@example.com/test');
     $this->assertEqual($socket->request['uri']['user'], 'mark');
     $this->assertEqual($socket->request['uri']['pass'], 'secret');
     $socket->get('/test2');
     $this->assertEqual($socket->request['auth']['user'], 'mark');
     $this->assertEqual($socket->request['auth']['pass'], 'secret');
     $socket->get('/test3');
     $this->assertEqual($socket->request['auth']['user'], 'mark');
     $this->assertEqual($socket->request['auth']['pass'], 'secret');
 }