forked from whgojp/JavaSecLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloResourceTest.java
More file actions
69 lines (59 loc) · 2.99 KB
/
HelloResourceTest.java
File metadata and controls
69 lines (59 loc) · 2.99 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
package top.whgojp;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.context.WebApplicationContext;
import top.whgojp.modules.system.entity.AuthenticationRequest;
import java.nio.charset.StandardCharsets;
// SpringBootTest 注解默认使用 webEnvironment = WebEnvironment.MOCK,它是不会对 Filter、Servlet进行初始化的。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// 这个注解可以用来自动注入 mockMvc,这里一定要使用这个注解来注入,不然默认的那种写法是没有添加 Filter的
@AutoConfigureMockMvc
class HelloResourceTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private MockMvc mockMvc;
private String jwt;
ObjectMapper objectMapper;
@BeforeEach
void setUp() throws Exception {
objectMapper = new ObjectMapper();
createAuthenticateToken(); // 生成 Token
}
@Test
void hello() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders.get("/hello")
.header("Authorization", "Bearer " + jwt) // 别忘了要加个空格
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("this is resource"))
.andDo(MockMvcResultHandlers.print());
}
void createAuthenticateToken() throws Exception {
AuthenticationRequest authenticationRequest = new AuthenticationRequest("123", "123");
String json = objectMapper.writeValueAsString(authenticationRequest);
mockMvc.perform(
MockMvcRequestBuilders.post("/authenticate")
.accept(MediaType.APPLICATION_JSON_VALUE)
.contentType("application/json;charset=UTF-8")
.content(json.getBytes(StandardCharsets.UTF_8))
)
.andExpect(MockMvcResultMatchers.status().isOk())
// .andDo(MockMvcResultHandlers.print())
.andDo(result -> {
String body = result.getResponse().getContentAsString();
// 注意:最后这里要用 asText 不要用 toString,否则结果是有 " " 引号的
jwt = objectMapper.readTree(body).get("jwt").asText();
});
}
}