public function testFetch500() { $this->setExpectedException('RuntimeException', 'Provider name responded with an HTTP 500: abc'); $response = $this->_setupResponse(); $mockBody = $this->mock('tubepress_api_streams_StreamInterface'); $response->shouldReceive('getStatusCode')->times(3)->andReturn(500); $response->shouldReceive('getBody')->once()->andReturn($mockBody); $mockBody->shouldReceive('toString')->once()->andReturn('abc'); $this->_sut->fetchWithCodeGrant($this->_mockProvider, 'the code'); $this->assertTrue(true); }
public function onHttpRequest(tubepress_api_event_EventInterface $event) { /** * @var tubepress_api_http_message_RequestInterface */ $request = $event->getSubject(); $providers = $this->getAllProviders(); $requestConfig = $request->getConfig(); if (!array_key_exists('tubepress-remote-api-call', $requestConfig)) { return; } if ($requestConfig['tubepress-remote-api-call'] !== true) { return; } if ($this->_shouldLog) { $this->_logDebug(sprintf('OAuth2 signing listener invoked for <code>%s</code> to <code>%s</code> with <code>%d</code> registered OAuth2 provider(s)', $request->getMethod(), $request->getUrl(), count($providers))); } foreach ($providers as $provider) { if ($this->_shouldLog) { $this->_logDebug(sprintf('Seeing if %s wants to authorize <code>%s</code> to <code>%s</code>', $provider->getDisplayName(), $request->getMethod(), $request->getUrl())); } if (!$provider->wantsToAuthorizeRequest($request)) { if ($this->_shouldLog) { $this->_logDebug(sprintf('%s declined to authorize <code>%s</code> to <code>%s</code>', $provider->getDisplayName(), $request->getMethod(), $request->getUrl())); } continue; } if ($this->_shouldLog) { $this->_logDebug(sprintf('%s wants to authorize <code>%s</code> to <code>%s</code>', $provider->getDisplayName(), $request->getMethod(), $request->getUrl())); } $token = $this->_persistenceHelper->getStoredToken($provider); if (!$token) { if ($this->_shouldLog) { $this->_logDebug(sprintf('No saved token for %s to use, or user requested no signing.', $provider->getDisplayName())); } break; } if ($token->isExpired()) { if ($this->_shouldLog) { $this->_logDebug('Existing token has expired.'); } if (!$token->getRefreshToken()) { if ($this->_shouldLog) { $this->_logDebug(sprintf('Token for %s has expired and no refresh token available.', $provider->getDisplayName())); } break; } if ($this->_shouldLog) { $this->_logDebug(sprintf('Token for %s has expired. We will try to refresh it.', $provider->getDisplayName())); } $oldToken = $token; $newToken = $this->_accessTokenFetcher->fetchWithRefreshToken($provider, $token); if (!$newToken) { if ($this->_shouldLog) { $this->_logDebug(sprintf('Unable to refresh token for %s. Boo.', $provider->getDisplayName())); } break; } if ($this->_shouldLog) { $this->_logDebug(sprintf('Successfully refreshed token for %s. Yay.', $provider->getDisplayName())); } $token = $newToken; $this->_persistenceHelper->updateToken($oldToken, $newToken); } $clientId = $this->_persistenceHelper->getClientId($provider); $clientSecret = $this->_persistenceHelper->getClientSecret($provider); if ($token && $clientId) { $provider->authorizeRequest($request, $token, $clientId, $clientSecret); break; } } }