forked from scribejava/scribejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthEncoderTest.java
More file actions
69 lines (59 loc) · 2.37 KB
/
Copy pathOAuthEncoderTest.java
File metadata and controls
69 lines (59 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.github.scribejava.core.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
public class OAuthEncoderTest {
@Test
public void shouldPercentEncodeString() {
final String plain = "this is a test &^";
final String encoded = "this%20is%20a%20test%20%26%5E";
assertEquals(encoded, OAuthEncoder.encode(plain));
}
@Test
public void shouldFormURLDecodeString() {
final String encoded = "this+is+a+test+%26%5E";
final String plain = "this is a test &^";
assertEquals(plain, OAuthEncoder.decode(encoded));
}
@Test
public void shouldPercentEncodeAllSpecialCharacters() {
final String plain = "!*'();:@&=+$,/?#[]";
final String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
assertEquals(encoded, OAuthEncoder.encode(plain));
assertEquals(plain, OAuthEncoder.decode(encoded));
}
@Test
public void shouldNotPercentEncodeReservedCharacters() {
final String plain = "abcde123456-._~";
final String encoded = plain;
assertEquals(encoded, OAuthEncoder.encode(plain));
}
public void shouldThrowExceptionIfStringToEncodeIsNull() {
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
OAuthEncoder.encode(null);
}
});
}
public void shouldThrowExceptionIfStringToDecodeIsNull() {
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
OAuthEncoder.decode(null);
}
});
}
@Test
public void shouldPercentEncodeCorrectlyTwitterCodingExamples() {
// These tests are part of the Twitter dev examples here
// -> https://dev.twitter.com/docs/auth/percent-encoding-parameters
final String[] sources = {"Ladies + Gentlemen", "An encoded string!", "Dogs, Cats & Mice"};
final String[] encoded = {"Ladies%20%2B%20Gentlemen", "An%20encoded%20string%21",
"Dogs%2C%20Cats%20%26%20Mice"};
for (int i = 0; i < sources.length; i++) {
assertEquals(encoded[i], OAuthEncoder.encode(sources[i]));
}
}
}