QrCodeTest.java
1.98 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
package com.xkl;
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 {
public static final String URL_GETQR_WITHTOKEN = Constants.URL_PREFIX + "/qrcode/getQrWithToken";
public static final String URL_GETQR_WITHOPNEID = Constants.URL_PREFIX + "/qrcode/getQrWithOpenId";
/**
* 使用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));
}
/**
* 使用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));
}
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;
}
}