Beispiel #1
0
 /**
  * @covers OAuth2\Response::__construct()
  * @covers OAuth2\Response::headers()
  * @covers OAuth2\Response::status()
  * @covers OAuth2\Response::body()
  */
 public function testConstructorBuildsResponse()
 {
     $status = 200;
     $headers = array('foo' => array('bar'));
     $body = 'foo';
     // returns the status, headers and body
     $this->response = new \OAuth2\Response(new \GuzzleHttp\Message\Response($status, $headers, \GuzzleHttp\Stream\Stream::factory($body)));
     $responseHeaders = $this->response->headers();
     $this->assertCount(1, $responseHeaders);
     $this->assertArrayHasKey('foo', $responseHeaders);
     $this->assertEquals($headers['foo'], $responseHeaders['foo']);
     $this->assertEquals($status, $this->response->status());
     $this->assertEquals($body, $this->response->body());
 }
Beispiel #2
0
 /**
  * Intercept all OAuth2\Client::getResponse() calls and mock their responses
  */
 public function mockGetResponse()
 {
     // retrieve arguments
     $args = func_get_args();
     // map routes
     $map = array();
     $map['GET']['/success'] = array('status' => 200, 'headers' => array('Content-Type' => 'text/awesome'), 'body' => 'yay');
     $map['GET']['/reflect'] = array('status' => 200, 'headers' => array(), 'body' => $args[0]->getBody());
     $map['POST']['/reflect'] = array('status' => 200, 'headers' => array(), 'body' => $args[0]->getBody());
     $map['GET']['/unauthorized'] = array('status' => 401, 'headers' => array('Content-Type' => 'application/json'), 'body' => json_encode(array('error' => $this->errorValue, 'error_description' => $this->errorDescriptionValue)));
     $map['GET']['/conflict'] = array('status' => 409, 'headers' => array('Content-Type' => 'text/plain'), 'body' => 'not authorized');
     $map['GET']['/redirect'] = array('status' => 302, 'headers' => array('Content-Type' => 'text/plain', 'location' => '/success'), 'body' => '');
     $map['POST']['/redirect'] = array('status' => 303, 'headers' => array('Content-Type' => 'text/plain', 'location' => '/reflect'), 'body' => '');
     $map['GET']['/error'] = array('status' => 500, 'headers' => array(), 'body' => '');
     $map['GET']['/empty_get'] = array('status' => 200, 'headers' => array(), 'body' => '');
     // match response
     $response = $map[$args[0]->getMethod()][$args[0]->getPath()];
     // wrap response in an OAuth2\Response object
     $response = new \OAuth2\Response(new \GuzzleHttp\Message\Response($response['status'], $response['headers'], \GuzzleHttp\Stream\Stream::factory($response['body'])), $args[1]);
     // handle response
     if (in_array($response->status(), range(200, 299))) {
         return $response;
     } else {
         if (in_array($response->status(), range(300, 399))) {
             // Increment redirect count
             $this->client->options['redirect_count'] = isset($this->client->options['redirect_count']) ? $this->client->options['redirect_count'] : 0;
             $this->client->options['redirect_count'] += 1;
             if ($this->client->options['redirect_count'] > $args[0]->getConfig()['redirect']['max']) {
                 return $response;
             }
             // Retrieve data
             $method = $response->status() === 303 ? 'GET' : $args[0]->getMethod();
             $headers = $response->headers();
             $location = $headers['location'];
             // Redirect request
             $request = $this->client->createRequest($method, $location[0], ['body' => $response->body()]);
             return $this->client->getResponse($request);
         } else {
             if (in_array($response->status(), range(400, 599))) {
                 $e = new \OAuth2\Error($response);
                 if ($args[0]->getConfig()['exceptions'] || $this->client->options['request_opts']['exceptions']) {
                     throw $e;
                 }
                 $response->error = $e;
                 return $response;
             } else {
                 throw new \OAuth2\Error($response);
             }
         }
     }
 }