-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathDataUtilsTest.java
More file actions
60 lines (50 loc) · 2.3 KB
/
Copy pathDataUtilsTest.java
File metadata and controls
60 lines (50 loc) · 2.3 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
package me.chanjar.weixin.common.util;
import org.testng.annotations.*;
import static org.testng.Assert.*;
/**
* <pre>
* Created by BinaryWang on 2018/5/8.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class DataUtilsTest {
@Test
public void testHandleDataWithSecret() {
String data = "js_code=001tZveq0SMoiq1AEXeq0ECJeq0tZveZ&secret=5681022fa1643845392367ea88888888&grant_type=authorization_code&appid=wxe156d4848d999999";
final String s = DataUtils.handleDataWithSecret(data);
assertTrue(s.contains("&secret=******&"));
}
@Test
public void testHandleDataWithSecretAtEnd() {
// Secret is the last parameter in the query string, so there is no trailing &
String data = "appid=wx123&secret=abc123";
final String s = DataUtils.handleDataWithSecret(data);
assertFalse(s.contains("abc123"), "Secret at the end of the string should be masked");
assertTrue(s.contains("secret=******"), "Secret should be replaced with asterisks");
}
@Test
public void testHandleDataWithSecretAsFirstParam() {
// Secret is the first parameter, so there is no leading &
String data = "secret=abc123&appid=wx123";
final String s = DataUtils.handleDataWithSecret(data);
assertFalse(s.contains("abc123"), "Secret as the first parameter should be masked");
assertTrue(s.contains("secret=******"), "Secret should be replaced with asterisks");
}
@Test
public void testHandleDataWithSecretEncodedValue() {
// The secret value contains URL-encoded and non-word characters; the whole value must be masked
String data = "appid=wx123&secret=abc%2Fdef-.+ghi&grant_type=client_credential";
final String s = DataUtils.handleDataWithSecret(data);
assertFalse(s.contains("def"), "The full secret value must be masked, including after non-word characters");
assertFalse(s.contains("%2F"), "Encoded characters in the secret must be masked too");
assertTrue(s.contains("&secret=******&"), "Secret should be replaced with asterisks");
}
@Test
public void testHandleDataWithOpenidInJson() {
String data = "{\"code\":\"phone-code\",\"openid\":\"user-openid\"}";
String result = DataUtils.handleDataWithSecret(data);
assertFalse(result.contains("user-openid"));
assertTrue(result.contains("\"openid\":\"******\""));
}
}