Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0386df2606 | ||
|
|
ca0076c043 |
No files matched your search
@@ -11,6 +11,6 @@ public class FacebookApi extends DefaultApi20
|
||||
public String getAuthorizationUrl(OAuthConfig config)
|
||||
{
|
||||
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Facebook does not support OOB");
|
||||
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.percentEncode(config.getCallback()));
|
||||
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.urlEncodeWrapper(config.getCallback()));
|
||||
}
|
||||
}
|
||||
@@ -32,11 +32,31 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
|
||||
private String getSortedAndEncodedParams(OAuthRequest request)
|
||||
{
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.putAll(request.getQueryStringParams());
|
||||
params.putAll(request.getBodyParams());
|
||||
params.putAll(request.getOauthParameters());
|
||||
Map<String, String> tmp = null;
|
||||
//params.putAll(request.getQueryStringParams());
|
||||
tmp = request.getQueryStringParams();
|
||||
for (String key:tmp.keySet()){
|
||||
params.put(
|
||||
URLUtils.percentEncode(key),
|
||||
URLUtils.percentEncode(tmp.get(key))
|
||||
);
|
||||
}
|
||||
tmp = request.getBodyParams();
|
||||
for (String key:tmp.keySet()){
|
||||
params.put(
|
||||
URLUtils.percentEncode(key),
|
||||
URLUtils.percentEncode(tmp.get(key))
|
||||
);
|
||||
}
|
||||
tmp = request.getOauthParameters();
|
||||
for (String key:tmp.keySet()){
|
||||
params.put(
|
||||
URLUtils.percentEncode(key),
|
||||
URLUtils.percentEncode(tmp.get(key))
|
||||
);
|
||||
}
|
||||
params = MapUtils.sort(params);
|
||||
return URLUtils.percentEncode(URLUtils.formURLEncodeMap(params));
|
||||
return URLUtils.percentEncode(URLUtils.concatSortedPercentEncodedParams(params));
|
||||
}
|
||||
|
||||
private void checkPreconditions(OAuthRequest request)
|
||||
|
||||
@@ -27,8 +27,8 @@ public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExt
|
||||
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
|
||||
if (matcher.matches())
|
||||
{
|
||||
String token = URLUtils.percentDecode(matcher.group(1));
|
||||
String secret = URLUtils.percentDecode(matcher.group(2));
|
||||
String token = URLUtils.urlDecodeWrapper(matcher.group(1));
|
||||
String secret = URLUtils.urlDecodeWrapper(matcher.group(2));
|
||||
return new Token(token, secret);
|
||||
} else
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -19,8 +20,9 @@ public class OAuthRequest extends Request
|
||||
*
|
||||
* @param verb Http verb/method
|
||||
* @param url resource URL
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public OAuthRequest(Verb verb, String url)
|
||||
public OAuthRequest(Verb verb, String url) throws MalformedURLException
|
||||
{
|
||||
super(verb, url);
|
||||
this.oauthParameters = new HashMap<String, String>();
|
||||
|
||||
@@ -17,7 +17,7 @@ class Request
|
||||
{
|
||||
private static final String CONTENT_LENGTH = "Content-Length";
|
||||
|
||||
private String url;
|
||||
private URL url;
|
||||
private Verb verb;
|
||||
private Map<String, String> querystringParams;
|
||||
private Map<String, String> bodyParams;
|
||||
@@ -30,11 +30,12 @@ class Request
|
||||
*
|
||||
* @param verb Http Verb (GET, POST, etc)
|
||||
* @param url url with optional querystring parameters.
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public Request(Verb verb, String url)
|
||||
public Request(Verb verb, String url) throws MalformedURLException
|
||||
{
|
||||
this.verb = verb;
|
||||
this.url = url;
|
||||
this.url = new URL(url);
|
||||
this.querystringParams = new HashMap<String, String>();
|
||||
this.bodyParams = new HashMap<String, String>();
|
||||
this.headers = new HashMap<String, String>();
|
||||
@@ -61,7 +62,7 @@ class Request
|
||||
|
||||
private void createConnection() throws IOException
|
||||
{
|
||||
String effectiveUrl = URLUtils.appendParametersToQueryString(url, querystringParams);
|
||||
String effectiveUrl = URLUtils.appendParametersToQueryString(url.toString(), querystringParams);
|
||||
if (connection == null)
|
||||
{
|
||||
connection = (HttpURLConnection) new URL(effectiveUrl).openConnection();
|
||||
@@ -147,25 +148,22 @@ class Request
|
||||
*/
|
||||
public Map<String, String> getQueryStringParams()
|
||||
{
|
||||
try
|
||||
{
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
String query = new URL(url).getQuery();
|
||||
String query = url.getQuery();
|
||||
if (query != null)
|
||||
{
|
||||
for (String param : query.split("&"))
|
||||
{
|
||||
String pair[] = param.split("=");
|
||||
params.put(pair[0], pair[1]);
|
||||
String valuePart = "";
|
||||
if (pair.length > 1) {
|
||||
valuePart = URLUtils.urlDecodeWrapper(pair[1]);
|
||||
}
|
||||
params.put(URLUtils.urlDecodeWrapper(pair[0]),valuePart );
|
||||
}
|
||||
}
|
||||
params.putAll(querystringParams);
|
||||
return params;
|
||||
}
|
||||
catch (MalformedURLException mue)
|
||||
{
|
||||
throw new OAuthException("Malformed URL", mue);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +183,7 @@ class Request
|
||||
*/
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,7 +193,7 @@ class Request
|
||||
*/
|
||||
public String getSanitizedUrl()
|
||||
{
|
||||
return url.replaceAll("\\?.*", "").replace("\\:\\d{4}", "");
|
||||
return URLUtils.convertUrlToBaseStringURI(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import org.scribe.utils.*;
|
||||
|
||||
/**
|
||||
* Represents an OAuth verifier code.
|
||||
*
|
||||
@@ -19,7 +17,10 @@ public class Verifier
|
||||
*/
|
||||
public Verifier(String value)
|
||||
{
|
||||
this.value = URLUtils.percentDecode(value);
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue()
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.scribe.oauth;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.scribe.builder.api.*;
|
||||
import org.scribe.exceptions.OAuthException;
|
||||
import org.scribe.model.*;
|
||||
|
||||
/**
|
||||
@@ -35,7 +38,12 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
*/
|
||||
public Token getRequestToken()
|
||||
{
|
||||
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
|
||||
OAuthRequest request = null;;
|
||||
try {
|
||||
request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
|
||||
} catch (MalformedURLException e) {
|
||||
throw new OAuthException("Problems while creating connection", e);
|
||||
}
|
||||
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
|
||||
addOAuthHeader(request);
|
||||
Response response = request.send();
|
||||
@@ -59,7 +67,12 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
*/
|
||||
public Token getAccessToken(Token requestToken, Verifier verifier)
|
||||
{
|
||||
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
|
||||
OAuthRequest request = null;
|
||||
try {
|
||||
request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
|
||||
} catch (MalformedURLException e) {
|
||||
throw new OAuthException("Problems while creating connection", e);
|
||||
}
|
||||
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
|
||||
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
|
||||
addOAuthParams(request, requestToken);
|
||||
|
||||
@@ -16,6 +16,10 @@ public class URLUtils
|
||||
private static final char PAIR_SEPARATOR = '=';
|
||||
private static final char PARAM_SEPARATOR = '&';
|
||||
private static final char QUERY_STRING_SEPARATOR = '?';
|
||||
private static final String HTTP_PROTOCOL = "http";
|
||||
private static final int HTTP_DEFAULT_PORT = 80;
|
||||
private static final String HTTPS_PROTOCOL = "https";
|
||||
private static final int HTTPS_DEFAULT_PORT = 443;
|
||||
|
||||
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
|
||||
|
||||
@@ -31,7 +35,7 @@ public class URLUtils
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a map into a form-url-encoded string (key=value&key2=value2)
|
||||
* Turns a map into a form-url-encoded string (key=value&key2&key3=value)
|
||||
*
|
||||
* @param map any map
|
||||
* @return form-url-encoded string
|
||||
@@ -51,7 +55,10 @@ public class URLUtils
|
||||
{
|
||||
encodedString.append(PARAM_SEPARATOR);
|
||||
}
|
||||
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key)));
|
||||
encodedString.append(urlEncodeWrapper(key));
|
||||
if (map.get(key).length() > 0) {
|
||||
encodedString.append(PAIR_SEPARATOR).append(urlEncodeWrapper(map.get(key)));
|
||||
}
|
||||
}
|
||||
return encodedString.toString();
|
||||
}
|
||||
@@ -81,12 +88,31 @@ public class URLUtils
|
||||
}
|
||||
|
||||
/**
|
||||
* Percent decodes a string
|
||||
* URL encodes a string
|
||||
*
|
||||
* @param plain
|
||||
* @return percent encoded string
|
||||
*/
|
||||
public static String urlEncodeWrapper(String string)
|
||||
{
|
||||
Preconditions.checkNotNull(string, "Cannot encode null string");
|
||||
try
|
||||
{
|
||||
return URLEncoder.encode(string, UTF_8);
|
||||
}
|
||||
catch (UnsupportedEncodingException uee)
|
||||
{
|
||||
throw new IllegalStateException(ERROR_MSG, uee);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* URL decodes a string
|
||||
*
|
||||
* @param string percent encoded string
|
||||
* @return plain string
|
||||
*/
|
||||
public static String percentDecode(String string)
|
||||
public static String urlDecodeWrapper(String string)
|
||||
{
|
||||
Preconditions.checkNotNull(string, "Cannot decode null string");
|
||||
try
|
||||
@@ -133,4 +159,40 @@ public class URLUtils
|
||||
return string.replace(ch, toCh);
|
||||
}
|
||||
}
|
||||
|
||||
public static String concatSortedPercentEncodedParams(Map<String, String> params) {
|
||||
StringBuilder target = new StringBuilder();
|
||||
for (String key:params.keySet()){
|
||||
target.append(key);
|
||||
target.append(PAIR_SEPARATOR);
|
||||
target.append(params.get(key));
|
||||
target.append(PARAM_SEPARATOR);
|
||||
}
|
||||
return target.deleteCharAt(target.length() - 1).toString();
|
||||
}
|
||||
|
||||
public static String convertUrlToBaseStringURI(URL url){
|
||||
URI uri = null;
|
||||
try {
|
||||
uri = url.toURI();
|
||||
} catch (URISyntaxException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
String scheme = uri.getScheme().toLowerCase();
|
||||
String host = uri.getHost().toLowerCase();
|
||||
int port = uri.getPort();
|
||||
if (
|
||||
(scheme.equals(HTTP_PROTOCOL) && port == HTTP_DEFAULT_PORT)
|
||||
|| (scheme.equals(HTTPS_PROTOCOL) && port == HTTPS_DEFAULT_PORT)
|
||||
){
|
||||
port = -1;
|
||||
}
|
||||
URI baseUri = null;
|
||||
try {
|
||||
baseUri = new URI(scheme,null, host, port, uri.getPath(), null, null);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return baseUri.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.examples;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.*;
|
||||
|
||||
import org.scribe.builder.*;
|
||||
@@ -14,7 +15,7 @@ public class FacebookExample
|
||||
private static final String NO_SECRET_NEEDED = "";
|
||||
private static final Token EMPTY_TOKEN = null;
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws MalformedURLException
|
||||
{
|
||||
// Replace these with your own api key and secret
|
||||
String apiKey = "your_api_key";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.examples;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.scribe.builder.*;
|
||||
@@ -11,7 +12,7 @@ public class FoursquareExample
|
||||
{
|
||||
private static final String PROTECTED_RESOURCE_URL = "http://api.foursquare.com/v1/user";
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws MalformedURLException
|
||||
{
|
||||
OAuthService service = new ServiceBuilder()
|
||||
.provider(FoursquareApi.class)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.examples;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.*;
|
||||
|
||||
import org.scribe.builder.*;
|
||||
@@ -14,7 +15,7 @@ public class GoogleExample
|
||||
private static final String PROTECTED_RESOURCE_URL = "https://docs.google.com/feeds/default/private/full/";
|
||||
private static final String SCOPE = "https://docs.google.com/feeds/";
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws MalformedURLException
|
||||
{
|
||||
OAuthService service = new ServiceBuilder()
|
||||
.provider(GoogleApi.class)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.examples;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.scribe.builder.*;
|
||||
@@ -11,7 +12,7 @@ public class LinkedInExample
|
||||
{
|
||||
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws MalformedURLException
|
||||
{
|
||||
OAuthService service = new ServiceBuilder()
|
||||
.provider(LinkedInApi.class)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.examples;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.scribe.builder.*;
|
||||
@@ -11,7 +12,7 @@ public class TwitterExample
|
||||
{
|
||||
private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/account/verify_credentials.xml";
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws MalformedURLException
|
||||
{
|
||||
OAuthService service = new ServiceBuilder()
|
||||
.provider(TwitterApi.class)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.scribe.examples;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.scribe.builder.*;
|
||||
@@ -11,7 +12,7 @@ public class YahooExample
|
||||
{
|
||||
private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/A6ROU63MXWDCW3Y5MGCYWVHDJI/profile/status?format=json";
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws MalformedURLException
|
||||
{
|
||||
OAuthService service = new ServiceBuilder()
|
||||
.provider(YahooApi.class)
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.scribe.extractors;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.junit.*;
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.model.*;
|
||||
@@ -12,12 +14,16 @@ public class BaseStringExtractorTest
|
||||
|
||||
private BaseStringExtractorImpl extractor;
|
||||
private OAuthRequest request;
|
||||
private OAuthRequest request2;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
public void setup() throws MalformedURLException
|
||||
{
|
||||
request = ObjectMother.createSampleOAuthRequest();
|
||||
request2 = ObjectMother.createSampleOAuthRequest2();
|
||||
extractor = new BaseStringExtractorImpl();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -27,6 +33,14 @@ public class BaseStringExtractorTest
|
||||
String baseString = extractor.extract(request);
|
||||
assertEquals(expected, baseString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExtractBaseStringFromOAuthRequest2()
|
||||
{
|
||||
String expected = "GET&http%3A%2F%2Fexample.com&oauth_callback%3Dhttp%253A%252F%252Fexample%252Fcallback%26oauth_consumer_key%3DAS%2523%2524%255E%252A%2540%2526%26oauth_signature%3DOAuth-Signature%26oauth_timestamp%3D123456";
|
||||
String baseString = extractor.extract(request2);
|
||||
assertEquals(expected, baseString);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowExceptionIfRquestIsNull()
|
||||
@@ -36,7 +50,7 @@ public class BaseStringExtractorTest
|
||||
}
|
||||
|
||||
@Test(expected = OAuthParametersMissingException.class)
|
||||
public void shouldThrowExceptionIfRquestHasNoOAuthParameters()
|
||||
public void shouldThrowExceptionIfRquestHasNoOAuthParameters() throws MalformedURLException
|
||||
{
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||
extractor.extract(request);
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.scribe.extractors;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.junit.*;
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.model.*;
|
||||
@@ -14,7 +16,7 @@ public class HeaderExtractorTest
|
||||
private OAuthRequest request;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
public void setup() throws MalformedURLException
|
||||
{
|
||||
request = ObjectMother.createSampleOAuthRequest();
|
||||
extractor = new HeaderExtractorImpl();
|
||||
@@ -37,7 +39,7 @@ public class HeaderExtractorTest
|
||||
}
|
||||
|
||||
@Test(expected = OAuthParametersMissingException.class)
|
||||
public void shouldExceptionIfRequestHasNoOAuthParams()
|
||||
public void shouldExceptionIfRequestHasNoOAuthParams() throws MalformedURLException
|
||||
{
|
||||
OAuthRequest emptyRequest = new OAuthRequest(Verb.GET, "http://example.com");
|
||||
extractor.extract(emptyRequest);
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
public class OAuthRequestTest
|
||||
@@ -10,7 +12,7 @@ public class OAuthRequestTest
|
||||
private OAuthRequest request;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
public void setup() throws MalformedURLException
|
||||
{
|
||||
request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
public class RequestTest
|
||||
@@ -17,7 +19,7 @@ public class RequestTest
|
||||
connection = new ConnectionStub();
|
||||
postRequest = new Request(Verb.POST, "http://example.com");
|
||||
postRequest.setConnection(connection);
|
||||
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other=value+with+spaces");
|
||||
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
|
||||
getRequest.setConnection(connection);
|
||||
}
|
||||
|
||||
@@ -34,6 +36,8 @@ public class RequestTest
|
||||
assertEquals(2, getRequest.getQueryStringParams().size());
|
||||
assertEquals(0, postRequest.getQueryStringParams().size());
|
||||
assertTrue(getRequest.getQueryStringParams().containsKey("qsparam"));
|
||||
assertTrue(getRequest.getQueryStringParams().get("qsparam").equals("value"));
|
||||
assertTrue(getRequest.getQueryStringParams().get("other param").equals("value with spaces"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,7 +56,7 @@ public class RequestTest
|
||||
postRequest.addBodyParameter("param", "value");
|
||||
postRequest.addBodyParameter("param two", "value with spaces");
|
||||
postRequest.send();
|
||||
assertEquals("param%20two=value%20with%20spaces¶m=value", postRequest.getBodyContents());
|
||||
assertEquals("param+two=value+with+spaces¶m=value", postRequest.getBodyContents());
|
||||
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||
}
|
||||
|
||||
@@ -66,7 +70,7 @@ public class RequestTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAllowAddingQuerystringParametersAfterCreation()
|
||||
public void shouldAllowAddingQuerystringParametersAfterCreation() throws MalformedURLException
|
||||
{
|
||||
Request request = new Request(Verb.GET, "http://example.com?one=val");
|
||||
request.addQuerystringParameter("two", "other val");
|
||||
@@ -74,4 +78,37 @@ public class RequestTest
|
||||
assertEquals(3, request.getQueryStringParams().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sanatizeUrlTest() throws MalformedURLException
|
||||
{
|
||||
Request request = new Request(Verb.GET, "http://example.com?one=val");
|
||||
assertEquals("http://example.com", request.getSanitizedUrl());
|
||||
|
||||
request = new Request(Verb.GET, "http://EXAMPLE.com:80/?one=val");
|
||||
assertEquals("http://example.com/", request.getSanitizedUrl());
|
||||
|
||||
request = new Request(Verb.GET, "http://EXAMPLE.com:8080?one=val");
|
||||
assertEquals("http://example.com:8080", request.getSanitizedUrl());
|
||||
|
||||
request = new Request(Verb.GET, "http://EXAMPLE.com:8080?one=val");
|
||||
assertEquals("http://example.com:8080", request.getSanitizedUrl());
|
||||
|
||||
request = new Request(Verb.GET, "http://EXAMPLE.com:80/test/?one=val");
|
||||
assertEquals("http://example.com/test/", request.getSanitizedUrl());
|
||||
|
||||
request = new Request(Verb.GET, "http://EXAMPLE.com:8080/test/?one=val#Hello");
|
||||
assertEquals("http://example.com:8080/test/", request.getSanitizedUrl());
|
||||
|
||||
request = new Request(Verb.GET, "HTTP://EXAMPLE.com:8080/test/?one=val#Hello");
|
||||
assertEquals("http://example.com:8080/test/", request.getSanitizedUrl());
|
||||
|
||||
//From RFC5849 example p.20
|
||||
request = new Request(Verb.GET, "http://example.com:80/r%20v/X?id=123");
|
||||
assertEquals("http://example.com/r%20v/X", request.getSanitizedUrl());
|
||||
|
||||
//From RFC5849 example p.20
|
||||
request = new Request(Verb.GET, "https://www.example.com:8080/?q=1");
|
||||
assertEquals("https://www.example.com:8080/", request.getSanitizedUrl());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,10 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
public class VerifierTest
|
||||
{
|
||||
@Test
|
||||
public void shouldParse()
|
||||
{
|
||||
Verifier verifier = new Verifier("p8k%2BGIjIL9PblXq%2BpH6LmT9l");
|
||||
assertEquals(verifier.getValue(), "p8k+GIjIL9PblXq+pH6LmT9l");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowErrorForNullString()
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package org.scribe.test.helpers;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.scribe.model.*;
|
||||
|
||||
public class ObjectMother
|
||||
{
|
||||
|
||||
public static OAuthRequest createSampleOAuthRequest()
|
||||
public static OAuthRequest createSampleOAuthRequest() throws MalformedURLException
|
||||
{
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||
request.addOAuthParameter(OAuthConstants.TIMESTAMP, "123456");
|
||||
@@ -14,4 +16,14 @@ public class ObjectMother
|
||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, "OAuth-Signature");
|
||||
return request;
|
||||
}
|
||||
|
||||
public static OAuthRequest createSampleOAuthRequest2() throws MalformedURLException
|
||||
{
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "http://EXAMPLE.com");
|
||||
request.addOAuthParameter(OAuthConstants.TIMESTAMP, "123456");
|
||||
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, "AS#$^*@&");
|
||||
request.addOAuthParameter(OAuthConstants.CALLBACK, "http://example/callback");
|
||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, "OAuth-Signature");
|
||||
return request;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class URLUtilsTest
|
||||
params.put("key with spaces", "value with spaces");
|
||||
params.put("&symbols!", "#!");
|
||||
|
||||
String expected = "key=value&key%20with%20spaces=value%20with%20spaces&%26symbols%21=%23%21";
|
||||
String expected = "key=value&key+with+spaces=value+with+spaces&%26symbols%21=%23%21";
|
||||
assertEquals(expected, URLUtils.formURLEncodeMap(params));
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class URLUtilsTest
|
||||
{
|
||||
String toDecode = "this+is+a+test+%26%5E";
|
||||
String expected = "this is a test &^";
|
||||
assertEquals(expected, URLUtils.percentDecode(toDecode));
|
||||
assertEquals(expected, URLUtils.urlDecodeWrapper(toDecode));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -50,7 +50,7 @@ public class URLUtilsTest
|
||||
String plain = "!*'();:@&=+$,/?#[]";
|
||||
String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
|
||||
assertEquals(encoded, URLUtils.percentEncode(plain));
|
||||
assertEquals(plain, URLUtils.percentDecode(encoded));
|
||||
assertEquals(plain, URLUtils.urlDecodeWrapper(encoded));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,7 +79,7 @@ public class URLUtilsTest
|
||||
public void shouldThrowExceptionIfStringToDecodeIsNull()
|
||||
{
|
||||
String toDecode = null;
|
||||
URLUtils.percentDecode(toDecode);
|
||||
URLUtils.urlDecodeWrapper(toDecode);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -103,7 +103,7 @@ public class URLUtilsTest
|
||||
public void shouldAppendParametersToSimpleUrl()
|
||||
{
|
||||
String url = "http://www.example.com";
|
||||
String expectedUrl = "http://www.example.com?param1=value1¶m2=value%20with%20spaces";
|
||||
String expectedUrl = "http://www.example.com?param1=value1¶m2=value+with+spaces";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("param1", "value1");
|
||||
@@ -117,7 +117,7 @@ public class URLUtilsTest
|
||||
public void shouldAppendParametersToUrlWithQuerystring()
|
||||
{
|
||||
String url = "http://www.example.com?already=present";
|
||||
String expectedUrl = "http://www.example.com?already=present¶m1=value1¶m2=value%20with%20spaces";
|
||||
String expectedUrl = "http://www.example.com?already=present¶m1=value1¶m2=value+with+spaces";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("param1", "value1");
|
||||
@@ -142,6 +142,6 @@ public class URLUtilsTest
|
||||
String encoded = "oauth_verifier=7aEP%2BjNAwvjc0mjhqg0nuXPf";
|
||||
String expected = "oauth_verifier=7aEP+jNAwvjc0mjhqg0nuXPf";
|
||||
|
||||
Assert.assertEquals(expected, URLUtils.percentDecode(encoded));
|
||||
Assert.assertEquals(expected, URLUtils.urlDecodeWrapper(encoded));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user