TokenTest.java
1.5 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
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 TokenTest {
private static final String URL_LOGIN = Constants.URL_PREFIX + "/token";
public static String loginAndGetToken(String user,String pass){
Map<String,String> params = new HashMap<String, String>();
params.put("username",user);
params.put("password",EncodeTools.encode("MD5",pass));
params.put("t",HttpTools.getNow());
params.put("type","1");
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");
}
@Test
public void testLogin(){
System.out.println(loginAndGetToken("user1","pass1"));
}
@Test
public void logout(){
String token = loginAndGetToken("user1","pass1");
Map<String,String> params = new HashMap<String, String>();
params.put("t",HttpTools.getNow());
params.put("type","1");
params.put("sign",HttpTools.getSign(params));
String response = HttpTools.requestByMapWithToken(URL_LOGIN,"DELETE",params,token);
System.out.println(response);
}
}