Permalink
Browse files

Brought percent-encoding of spaces into line with OAuth spec section 3.6

  • Loading branch information...
1 parent 871733d commit 541a29e5117db22276566d38409ca1c2c96d49c9 @eboto eboto committed Apr 23, 2010
@@ -27,17 +27,20 @@
public class URL {
private static final String UTF8 = "UTF-8";
+ private static final String URL_SPACE_ENCODING = "+";
+ private static final String OAUTH_SPEC_SPACE_ENCODING = "%20";
/**
- * URLencode a given string
+ * Encode a given string as specified in OAuth Spec section 3.6.
*
* @param string to encode
* @return encoded string
* @throws RuntimeException if the string cannot be encoded
*/
public static String percentEncode(String string){
try{
- return URLEncoder.encode(string,UTF8);
+ return URLEncoder.encode(string,UTF8)
+ .replace(URL_SPACE_ENCODING, OAUTH_SPEC_SPACE_ENCODING);
}catch(UnsupportedEncodingException uee){
throw new RuntimeException("Wrong encoding: " + UTF8);
}
@@ -42,7 +42,7 @@ public void shouldSetupDefaultParams(){
public void shouldReturnEncodedParams(){
params.put("q","foo");
params.put("other param", "bar");
- String encodedString = "oauth_consumer_key=any&oauth_nonce=827ccb0eea8a706c4c34a16891f84e7b&oauth_signature_method=HMAC-SHA1&oauth_timestamp=12345&oauth_version=1.0&other+param=bar&q=foo";
+ String encodedString = "oauth_consumer_key=any&oauth_nonce=827ccb0eea8a706c4c34a16891f84e7b&oauth_signature_method=HMAC-SHA1&oauth_timestamp=12345&oauth_version=1.0&other%20param=bar&q=foo";
assertEquals(encodedString, params.asSortedFormEncodedString());
}

0 comments on commit 541a29e

Please sign in to comment.