Skip to content

Commit 5187eb6

Browse files
committed
Merge pull request #268 from m0ppers/tokenerrors
Better error handling. There might be notices
2 parents 10d450e + 60dcf28 commit 5187eb6

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/OAuth/OAuth1/Service/Twitter.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ protected function parseAccessTokenResponse($responseBody)
100100
parse_str($responseBody, $data);
101101

102102
if (null === $data || !is_array($data)) {
103-
throw new TokenResponseException('Unable to parse response.');
103+
throw new TokenResponseException('Unable to parse response: ' . $responseBody);
104104
} elseif (isset($data['error'])) {
105105
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
106+
} elseif (!isset($data["oauth_token"]) || !isset($data["oauth_token_secret"])) {
107+
throw new TokenResponseException('Invalid response. OAuth Token data not set: ' . $responseBody);
106108
}
107109

108110
$token = new StdOAuth1Token();

tests/Unit/OAuth1/Service/TwitterTest.php

+76
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,80 @@ public function testParseAccessTokenResponseValid()
304304

305305
$this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
306306
}
307+
308+
/**
309+
* @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
310+
*/
311+
public function testParseAccessTokenErrorTotalBullshit()
312+
{
313+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
314+
$service = new Twitter(
315+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
316+
$client,
317+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
318+
$this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
319+
);
320+
321+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
322+
$method = new \ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
323+
$method->setAccessible(true);
324+
$method->invokeArgs($service, array("hoho"));
325+
}
326+
327+
/**
328+
* @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
329+
*/
330+
public function testParseAccessTokenErrorItsAnError()
331+
{
332+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
333+
$service = new Twitter(
334+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
335+
$client,
336+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
337+
$this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
338+
);
339+
340+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
341+
$method = new \ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
342+
$method->setAccessible(true);
343+
$method->invokeArgs($service, array("error=hihihaha"));
344+
}
345+
346+
/**
347+
* @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
348+
*/
349+
public function testParseAccessTokenErrorItsMissingOauthToken()
350+
{
351+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
352+
$service = new Twitter(
353+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
354+
$client,
355+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
356+
$this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
357+
);
358+
359+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
360+
$method = new \ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
361+
$method->setAccessible(true);
362+
$method->invokeArgs($service, array("oauth_token_secret=1"));
363+
}
364+
365+
/**
366+
* @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
367+
*/
368+
public function testParseAccessTokenErrorItsMissingOauthTokenSecret()
369+
{
370+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
371+
$service = new Twitter(
372+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
373+
$client,
374+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
375+
$this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
376+
);
377+
378+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
379+
$method = new \ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
380+
$method->setAccessible(true);
381+
$method->invokeArgs($service, array("oauth_token=1"));
382+
}
307383
}

0 commit comments

Comments
 (0)