made token extraction regex easier and added a test case for multiple parameters

This commit is contained in:
Pablo Fernandez committed 2011-09-24 16:56:07 -03:00
1 parent 7e7d3ff7e0
commit 9f4569fbbc
2 files changed
+11 -2

No files matched your search

@@ -12,7 +12,7 @@ import org.scribe.utils.*;
*/
public class TokenExtractor20Impl implements AccessTokenExtractor
{
private static final String TOKEN_REGEX = "access_token=(\\S*?)(&(\\S*))?";
private static final String TOKEN_REGEX = "access_token=([^&]+)";
private static final String EMPTY_SECRET = "";
/**
@@ -23,7 +23,7 @@ public class TokenExtractor20Impl implements AccessTokenExtractor
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
if (matcher.matches())
if (matcher.find())
{
String token = URLUtils.formURLDecode(matcher.group(1));
return new Token(token, EMPTY_SECRET, response);
@@ -35,6 +35,15 @@ public class TokenExtractor20Test
assertEquals("", extracted.getSecret());
}
@Test
public void shouldExtractTokenFromResponseWithManyParameters()
{
String response = "access_token=foo1234&other_stuff=yeah_we_have_this_too&number=42";
Token extracted = extractor.extract(response);
assertEquals("foo1234", extracted.getToken());
assertEquals("", extracted.getSecret());
}
@Test(expected = OAuthException.class)
public void shouldThrowExceptionIfTokenIsAbsent()
{