Skip to content

Commit ad7ceed

Browse files
committed
add junit test
1 parent 05ab11c commit ad7ceed

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

springboot-starter-security-jwt/src/test/java/com/codingapi/springboot/security/SecurityJwtApplicationTest.java

+42-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import org.springframework.http.MediaType;
1010
import org.springframework.test.web.servlet.MockMvc;
1111
import org.springframework.test.web.servlet.MvcResult;
12+
import org.springframework.util.StringUtils;
1213

1314
import java.nio.charset.StandardCharsets;
1415

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
1518
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1619
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
1720
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18-
import static org.junit.jupiter.api.Assertions.*;
1921

2022
@AutoConfigureMockMvc
2123
@SpringBootTest
@@ -58,4 +60,43 @@ void haveToken() throws Exception {
5860
});
5961
}
6062

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+
61102
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
server.port=8088
2-
spring.datasource.driver-class-name=org.h2.Driver
3-
spring.datasource.url=jdbc:h2:file:./test.db
4-
spring.jpa.hibernate.ddl-auto=create-drop
5-
spring.jpa.show-sql=true
2+
3+
codingapi.security.jwt-time=10000
4+
codingapi.security.jwt-rest-time=5000
5+

0 commit comments

Comments
 (0)