Ejemplo n.º 1
0
 public function testBehaviorOwner()
 {
     $factory = new Factory('http://localhost/');
     $factory->setRequestClassNamespace('\\Sokil\\Rest\\Client\\RequestMock');
     $factory->attachBehavior('my', new \Sokil\Rest\Client\MyBehavior());
     // exec behavior
     $requert = $factory->createRequest('GetRequestMock');
     $this->assertEquals('http://localhost/some/resource', $requert->getRequestUrl());
 }
Ejemplo n.º 2
0
 public function testCreateSignedRequest()
 {
     // replace response
     $plugin = new \Guzzle\Plugin\Mock\MockPlugin();
     $plugin->addResponse(new \Guzzle\Http\Message\Response(200, array('Content-type' => 'application/json'), json_encode(array('error' => 0))));
     // configure subscriber
     $signKey = 'APP_KEY';
     $factory = new Factory('http://localhost/');
     $factory->setRequestClassNamespace('\\Sokil\\Rest\\Client\\RequestMock')->setSignKey($signKey)->addSignAdditionalParam('app_id', 'APP_ID');
     $request = $factory->createSignedRequest('GetRequestMock');
     $request->addSubscriber($plugin);
     $response = $request->setQueryParam('param', 'value')->send();
     // test signature
     $body = $request->getQueryParams();
     unset($body['sign']);
     ksort($body);
     // calculate and compare sign with passed
     $this->assertEquals(hash_hmac($factory->getSignAlgo(), http_build_query($body), $signKey), $request->getQueryParam('sign'));
 }
Ejemplo n.º 3
0
 /**
  * @expectedException \Guzzle\Http\Exception\BadResponseException
  */
 public function testOnError()
 {
     // replace response
     $plugin = new \Guzzle\Plugin\Mock\MockPlugin();
     $plugin->addResponse(new \Guzzle\Http\Message\Response(404, array('Content-type' => 'application/json'), json_encode(array('error' => 0))));
     $factory = new Factory('http://localhost/');
     $factory->setRequestClassNamespace('\\Sokil\\Rest\\Client\\RequestMock');
     $factory->addSubscriber($plugin);
     $status = new \stdclass();
     $status->status = 0;
     $factory->onError(function ($event) use($status) {
         $status->status = $event['response']->getStatusCode();
     });
     // send
     $request = $factory->createRequest('GetRequestMock');
     try {
         $response = $request->send();
     } catch (\Exception $e) {
         // check if event occured
         $this->assertEquals(404, $status->status);
         throw $e;
     }
 }
Ejemplo n.º 4
0
 public function testBuildUrlForRequest()
 {
     // configure subscriber
     $factory = new Factory('http://localhost/basepath');
     $factory->setRequestClassNamespace('\\Sokil\\Rest\\Client\\RequestMock');
     $request = $factory->createRequest('GetRequestMock');
     $this->assertEquals('http://localhost/basepath/some/resource', $request->getUrl());
 }