作为这个问题的后续,我想知道如何透明地将“授权”标头添加到 a MockHttpServletRequestBuilder
,只有当测试中存在给定的注释时。
样本:
@RunWith(SpringRunner.class)
@EnableSpringDataWebSupport
@WebMvcTest(MyController.class)
@Import({WebSecurityConfig.class})
public class MyControllerTest {
@Autowired
protected MockMvc mockMvc;
@Test
@WithJwt(principal = "admin", authorities = {"READ_USERS"})
public void readUsersAuthorityCanListUsers() throws Exception {
final List<User> users = Arrays.asList(admin, user);
when(userRepo.findAll(any(Pageable.class))).thenReturn(new PageImpl<>(users));
this.mockMvc
.perform(get("/")
.header("Authorization", "Bearer foo"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content", hasSize(users.size())));
}
}
.header("Authorization", "Bearer foo")
如果测试用 装饰,如何对请求生成器进行后处理以自动应用@WithJwt
?