| 1 |
package oauth.signpost; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
import static org.junit.Assert.assertTrue; |
| 5 |
|
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.Iterator; |
| 8 |
import java.util.LinkedHashMap; |
| 9 |
import java.util.Map; |
| 10 |
|
| 11 |
import oauth.signpost.http.HttpParameters; |
| 12 |
|
| 13 |
import org.junit.Test; |
| 14 |
|
| 15 |
public class OAuthTest { |
| 16 |
|
| 17 |
private String rfc3986ReservedCharacters = ":/?#[]@!$&'()*+,;="; |
| 18 |
|
| 19 |
private String rfc3986UnreservedCharacters = "-._~"; // alpha-numeric chars ignored |
| 20 |
|
| 21 |
private String reservedCharactersEncoded = "%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D"; |
| 22 |
|
| 23 |
/** |
| 24 |
* OAuth percent encoding demands characters from the URI reserved set to be |
| 25 |
* percent encoded, and characters from the unreserved to NOT be percent |
| 26 |
* encoded. All characters must be UTF-8 encoded first. |
| 27 |
*/ |
| 28 |
@Test |
| 29 |
public void shouldHonorOAuthPercentEncodingRules() { |
| 30 |
|
| 31 |
// ALWAYS percent encode all characters from the reserved set as per RFC3986 |
| 32 |
assertEquals(reservedCharactersEncoded, |
| 33 |
OAuth.percentEncode(rfc3986ReservedCharacters)); |
| 34 |
// NEVER percent encode any characters from the unreserved set as per RFC3986 |
| 35 |
assertEquals(rfc3986UnreservedCharacters, |
| 36 |
OAuth.percentEncode(rfc3986UnreservedCharacters)); |
| 37 |
// percent encode spaces, do not use + |
| 38 |
assertEquals("%20", OAuth.percentEncode(" ")); |
| 39 |
// percent encode % |
| 40 |
assertEquals("%25", OAuth.percentEncode("%")); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void shouldCorrectlyPercentDecodeReservedCharacters() { |
| 45 |
assertEquals(rfc3986ReservedCharacters, |
| 46 |
OAuth.percentDecode(reservedCharactersEncoded)); |
| 47 |
assertEquals("%", OAuth.percentDecode("%25")); |
| 48 |
assertEquals(" ", OAuth.percentDecode("%20")); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void shouldCorrectlyFormEncodeParameters() throws Exception { |
| 53 |
HashMap<String, String> params = new HashMap<String, String>(); |
| 54 |
params.put("one", rfc3986ReservedCharacters); |
| 55 |
params.put(rfc3986ReservedCharacters, rfc3986UnreservedCharacters); |
| 56 |
|
| 57 |
// both keys and values must be percent encoded |
| 58 |
assertEquals("one=" + reservedCharactersEncoded + "&" + reservedCharactersEncoded + "=" |
| 59 |
+ rfc3986UnreservedCharacters, OAuth.formEncode(params.entrySet())); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void shouldCorrectlyFormDecodeParameters() { |
| 64 |
HttpParameters params = OAuth.decodeForm("one=" + reservedCharactersEncoded |
| 65 |
+ "&" + "one=another&" |
| 66 |
+ reservedCharactersEncoded + "=" + rfc3986UnreservedCharacters); |
| 67 |
|
| 68 |
assertTrue(params.size() == 3); |
| 69 |
|
| 70 |
Iterator<String> iter1 = params.get("one").iterator(); |
| 71 |
assertEquals(rfc3986ReservedCharacters, iter1.next()); |
| 72 |
assertEquals("another", iter1.next()); |
| 73 |
|
| 74 |
Iterator<String> iter2 = params.get(rfc3986ReservedCharacters).iterator(); |
| 75 |
assertEquals(rfc3986UnreservedCharacters, iter2.next()); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void shouldCorrectlyAppendQueryParameters() { |
| 80 |
String url1 = "http://www.example.com"; |
| 81 |
assertEquals("http://www.example.com?a=1&b=2", |
| 82 |
OAuth.addQueryParameters(url1, "a", "1", "b", "2")); |
| 83 |
|
| 84 |
String url2 = "http://www.example.com?x=1"; |
| 85 |
assertEquals("http://www.example.com?x=1&a=1&b=2", |
| 86 |
OAuth.addQueryParameters(url2, "a", "1", "b", "2")); |
| 87 |
|
| 88 |
Map<String, String> params = new LinkedHashMap<String, String>(); |
| 89 |
params.put("a", "1"); |
| 90 |
params.put("b", "2"); |
| 91 |
assertEquals("http://www.example.com?x=1&a=1&b=2", OAuth.addQueryParameters(url2, params)); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void shouldCorrectlyParseOAuthAuthorizationHeader() { |
| 97 |
String header = "OAuth realm=\"http://xyz.com\", oauth_callback=\"oob\""; |
| 98 |
HttpParameters params = OAuth.oauthHeaderToParamsMap(header); |
| 99 |
assertEquals("http://xyz.com", params.getFirst("realm")); |
| 100 |
assertEquals("oob", params.getFirst("oauth_callback")); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void shouldCorrectlyPrepareOAuthHeader() { |
| 105 |
assertEquals("OAuth realm=\"http://x.com\"", OAuth.prepareOAuthHeader("realm", |
| 106 |
"http://x.com")); |
| 107 |
assertEquals("OAuth realm=\"http://x.com\", oauth_token=\"x%25y\"", OAuth |
| 108 |
.prepareOAuthHeader("realm", "http://x.com", "oauth_token", "x%y")); |
| 109 |
} |
| 110 |
} |