示例#1
0
 /**
  * @covers OAuth2\Client\Response::content_type()
  * @covers OAuth2\Client\Response::parse()
  */
 public function testParseResponse()
 {
     // parses application/x-www-form-urlencoded body
     $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
     $body = 'foo=bar&answer=42';
     $this->response = new \OAuth2\Client\Response(new \GuzzleHttp\Message\Response(200, $headers, \GuzzleHttp\Stream\Stream::factory($body)));
     $parsedResponse = $this->response->parse();
     $this->assertEquals(2, count(array_keys($parsedResponse)));
     $this->assertEquals('bar', $parsedResponse['foo']);
     $this->assertEquals(42, $parsedResponse['answer']);
     // parses application/json body
     $headers = array('Content-Type' => 'application/json');
     $body = json_encode(array('foo' => 'bar', 'answer' => 42));
     $this->response = new \OAuth2\Client\Response(new \GuzzleHttp\Message\Response(200, $headers, \GuzzleHttp\Stream\Stream::factory($body)));
     $parsedResponse = $this->response->parse();
     $this->assertEquals(2, count(array_keys($parsedResponse)));
     $this->assertEquals('bar', $parsedResponse['foo']);
     $this->assertEquals(42, $parsedResponse['answer']);
     // doesn't try to parse other content-types
     $headers = array('Content-Type' => 'text/html');
     $body = '<!DOCTYPE html><html><head></head><body></body></html>';
     $this->response = new \OAuth2\Client\Response(new \GuzzleHttp\Message\Response(200, $headers, \GuzzleHttp\Stream\Stream::factory($body)));
     $this->assertNull($this->response->parse());
 }
示例#2
0
 /**
  * Intercept all OAuth2\Client\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\Client\Response object
     $response = new \OAuth2\Client\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\Client\Error($response);
                 if ($args[0]->getConfig()['exceptions'] || $this->client->options['request_opts']['exceptions']) {
                     throw $e;
                 }
                 $response->error = $e;
                 return $response;
             } else {
                 throw new \OAuth2\Client\Error($response);
             }
         }
     }
 }