QrCodeTest.java
4.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.xkl.internal.qrcode;
import com.utils.DESTools;
import com.xkl.*;
import com.xkl.partner.TokenTest;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* Created by zhaoyue on 2017/3/11.
*/
public class QrCodeTest {
private static final String URL_USER = Constants.URL_PREFIX + "/userInfo";
public static final String URL_GETQR_WITHACCPWD = Constants.URL_PREFIX + "/qrcode/getQrWithAccPwd";
public static final String URL_GETQR_WITHTOKEN = Constants.URL_PREFIX + "/qrcode/getQrWithToken";
public static final String URL_GETQR_WITHOPNEID = Constants.URL_PREFIX + "/qrcode/getQrWithOpenId";
public static final String URL_GETUSRINFO_WITHQR = Constants.URL_PREFIX + "/qrcode/getUsrInfoWithQr";
private static final String HTTP_PARA_QR_KEY = "tnvkqybFHp69pinDZJ7UWuX7";
/**
* 1 .使用用户名密码得到qrcode
*/
@Test
public void testGetQrWithAccPwd() {
Map<String, String> params = new HashMap<String, String>();
params.put("username", Constants.USR_ACC);
params.put("password", EncodeTools.encode("MD5", Constants.USR_PWD));
params.put("t", HttpTools.getNow());
String response = HttpTools.requestByMap(URL_GETQR_WITHACCPWD, "GET", params);
System.out.println(response);
Assert.assertTrue(hasQrCode(response));
}
/**
* 2 .使用openid得到qrcode
*/
@Test
public void testGetQrWithOpenid() {
Map<String, String> params = new HashMap<String, String>();
params.put("openId", Constants.USR_OPENID);
params.put("openIdType", Constants.USR_OPENID_TYPE);
params.put("t", HttpTools.getNow());
params.put("type", Constants.KEY_ID);
params.put("sign", HttpTools.getSign(params));
String response = HttpTools.requestByMap(URL_GETQR_WITHOPNEID, "GET", params);
System.out.println(response);
Assert.assertTrue(hasQrCode(response));
}
/**
* 3 .使用token得到qrcode
*/
@Test
public void testGetQrWithToken() {
String token = TokenTest.loginAndGetToken(Constants.USR_ACC, Constants.USR_PWD);
Map<String, String> params = new HashMap<String, String>();
params.put("t", HttpTools.getNow());
params.put("type", Constants.KEY_ID);
params.put("sign", HttpTools.getSign(params));
String response = HttpTools.requestByMapWithToken(URL_GETQR_WITHTOKEN, "GET", params, token);
System.out.println(response);
Assert.assertTrue(hasQrCode(response));
}
/**
* 4 .使用qrcode查询个人信息
*/
@Test
public void testGetUsrInfoWithQr() {
try {
String qrcode = getQrCode();
Map<String, String> params = new HashMap<String, String>();
String now = HttpTools.getNow();
String encryptQr = DESTools.encrypt(qrcode + now, HTTP_PARA_QR_KEY);
params.put("t", now);
params.put("encryptQr", encryptQr);
String response = HttpTools.requestByMap(URL_GETUSRINFO_WITHQR, "GET", params);
System.out.println(response);
Assert.assertTrue(RtnCodeTools.verifyCode(response, Constants.CODE_SUCC));
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean hasQrCode(String responseStr) {
try {
JSONObject res = new JSONObject(responseStr);
String qrcode = res.getJSONObject("content").getString("qrcode");
Assert.assertNotEquals("qrcode", "");
Assert.assertNotEquals("qrcode", null);
} catch (Exception e) {
return false;
}
return true;
}
private String getQrCode() {
Map<String, String> params = new HashMap<String, String>();
params.put("username", Constants.USR_ACC);
params.put("password", EncodeTools.encode("MD5", Constants.USR_PWD));
params.put("t", HttpTools.getNow());
String response = HttpTools.requestByMap(URL_GETQR_WITHACCPWD, "GET", params);
String qrcode = null;
try {
JSONObject res = new JSONObject(response);
qrcode = res.getJSONObject("content").getString("qrcode");
} catch (Exception e) {
return null;
}
return qrcode;
}
}