【发布时间】:2016-03-03 23:53:50
【问题描述】:
我正在尝试对我的 Spring-boot 应用程序进行集成测试。我有一个受保护的端点(/uaa/change_password,Spring MVC),它在未通过身份验证时重定向到 /uaa/login。如何拦截我发送的请求,我只为 /uaa/login 页面返回 200 OK。所以 assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); 的测试失败了,因为状态码是 200。(另一个测试也失败了)。任何帮助表示赞赏。
代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApiAuthServerApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class ApiAuthServerApplicationTests {
@Value("${local.server.port}")
private int port;
private RestTemplate template = new TestRestTemplate();
@Test
public void changePasswordPageProtected() {
ResponseEntity<String> response = template.getForEntity("http://localhost:"
+ port + "/uaa/change_password", String.class);
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
String auth = response.getHeaders().getFirst("WWW-Authenticate");
assertTrue("Wrong header: " + auth, auth.startsWith("Bearer realm"));
}
}
我的配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers().antMatchers(
"/login",
"/oauth/authorize",
"/oauth/confirm_access",
"/reset_password",
"/forgot_password",
"/change_password")
.and()
.authorizeRequests()
.antMatchers("/reset_password", "/forgot_password").permitAll()
.anyRequest().authenticated();
}
【问题讨论】:
标签: spring spring-mvc spring-boot spring-test