|
9 | 9 | import org.springframework.http.MediaType;
|
10 | 10 | import org.springframework.test.web.servlet.MockMvc;
|
11 | 11 | import org.springframework.test.web.servlet.MvcResult;
|
| 12 | +import org.springframework.util.StringUtils; |
12 | 13 |
|
13 | 14 | import java.nio.charset.StandardCharsets;
|
14 | 15 |
|
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
15 | 18 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
16 | 19 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
17 | 20 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
18 |
| -import static org.junit.jupiter.api.Assertions.*; |
19 | 21 |
|
20 | 22 | @AutoConfigureMockMvc
|
21 | 23 | @SpringBootTest
|
@@ -58,4 +60,43 @@ void haveToken() throws Exception {
|
58 | 60 | });
|
59 | 61 | }
|
60 | 62 |
|
| 63 | + |
| 64 | + @Test |
| 65 | + void resetToken() throws Exception { |
| 66 | + |
| 67 | + JSONObject json = new JSONObject(); |
| 68 | + json.put("username","admin"); |
| 69 | + json.put("password","123456"); |
| 70 | + MvcResult mvcResult = mockMvc.perform(post("/user/login").content(json.toJSONString().getBytes(StandardCharsets.UTF_8)).contentType(MediaType.APPLICATION_JSON)).andReturn(); |
| 71 | + JSONObject loginData = JSONObject.parseObject(mvcResult.getResponse().getContentAsString()); |
| 72 | + |
| 73 | + Thread.sleep(1000*5); |
| 74 | + |
| 75 | + mockMvc.perform(get("/api/hello").header("Authorization",loginData.getJSONObject("data").getString("token")).contentType(MediaType.APPLICATION_JSON)).andExpect(result -> { |
| 76 | + String body = result.getResponse().getContentAsString(); |
| 77 | + String newToken = result.getResponse().getHeader("Authorization"); |
| 78 | + |
| 79 | + assertTrue(StringUtils.hasText(newToken),"token reset error"); |
| 80 | + assertEquals(body,"hello","token authentication error"); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void expireToken() throws Exception { |
| 86 | + |
| 87 | + JSONObject json = new JSONObject(); |
| 88 | + json.put("username","admin"); |
| 89 | + json.put("password","123456"); |
| 90 | + MvcResult mvcResult = mockMvc.perform(post("/user/login").content(json.toJSONString().getBytes(StandardCharsets.UTF_8)).contentType(MediaType.APPLICATION_JSON)).andReturn(); |
| 91 | + JSONObject loginData = JSONObject.parseObject(mvcResult.getResponse().getContentAsString()); |
| 92 | + |
| 93 | + Thread.sleep(1000*10); |
| 94 | + |
| 95 | + mockMvc.perform(get("/api/hello").header("Authorization",loginData.getJSONObject("data").getString("token")).contentType(MediaType.APPLICATION_JSON)).andExpect(result -> { |
| 96 | + String body = result.getResponse().getContentAsString(); |
| 97 | + JSONObject data = JSONObject.parseObject(body); |
| 98 | + assertEquals(data.getString("errCode"),"token.expire","token authentication error"); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
61 | 102 | }
|
0 commit comments