OpenIdTest.java
2.68 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
package com.xkl;
import org.json.JSONObject;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* Created by win7 on 2017/1/2.
*/
public class OpenIdTest {
public static final String URL_OPEN_ID = Constants.URL_PREFIX + "/openId";
public static final String URL_LOGIN = Constants.URL_PREFIX + "/openId/login";
/**
* OpenId绑定接口
*/
@Test
public void testBind(){
System.out.println(bindWithOpenIdAndUser());
}
public static String bindWithOpenIdAndUser(){
Map<String,String> params = new HashMap<String, String>();
params.put("username","user1");
params.put("password",EncodeTools.encode("MD5","pass1"));
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_OPEN_ID,"POST",params);
return response;
}
/**
* OpenId解除绑定接口
*/
@Test
public void testUnBind(){
Map<String,String> params = new HashMap<String, String>();
params.put("username","user1");
params.put("password",EncodeTools.encode("MD5","pass1"));
params.put("openId","abcdefghijklmnopqrstuvwxyz123456");
params.put("openIdType","0");
params.put("t",HttpTools.getNow());
params.put("type",Constants.KEY_ID);
params.put("sign",HttpTools.getSign(params));
String response = HttpTools.requestByMap(URL_OPEN_ID,"DELETE",params);
System.out.println(response);
// 解除绑定后,又重新绑定好以便下次测试
testBind();
}
/**
* OpenId登陆接口
*/
@Test
public void testLogin(){
System.out.println(bindWithOpenIdAndLoginAndGetToken("abcdefghijklmnopqrstuvwxyz123456","0"));
}
public static String bindWithOpenIdAndLoginAndGetToken(String openId, String openIdType){
bindWithOpenIdAndUser();
Map<String,String> params = new HashMap<String, String>();
params.put("openId",openId);
params.put("openIdType",openIdType);
params.put("t",HttpTools.getNow());
params.put("type",Constants.KEY_ID);
params.put("sign",HttpTools.getSign(params));
String response = HttpTools.requestByMap(URL_LOGIN,"POST",params);
System.out.println("response:"+ response);
JSONObject jsonObject = new JSONObject(response);
JSONObject content = jsonObject.getJSONObject("content");
return content.getString("userId") + "_" + content.getString("token");
}
}