Rework the new testing support so that @SpringApplicationTest can be used for standard integration tests, web integration tests with a mock Servlet environment and web integration tests with an embedded servlet container. This means that it a replacement for 1.3's @IntegrationTest and @WebIntegrationTest and allows all SpringApplication testing to be configured using a common annotation. The old @IntegrationTest and @WebIntegrationTest along with their supporting classes have been reinstated to their previous form (while remaining deprecated). This should ensure that they continue to work in 1.4 exactly as they did in 1.3 giving users a smooth path to @SpringApplicationTest. See gh-5477
75 lines
2.4 KiB
Java
75 lines
2.4 KiB
Java
/*
|
|
* Copyright 2012-2016 the original author or authors.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package sample;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.context.SpringApplicationTest;
|
|
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
|
|
import org.springframework.mock.web.MockFilterChain;
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import org.springframework.security.crypto.codec.Base64;
|
|
import org.springframework.security.web.FilterChainProxy;
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
@RunWith(SpringRunner.class)
|
|
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
|
public class HelloWebSecurityApplicationTests {
|
|
|
|
@Autowired
|
|
private FilterChainProxy springSecurityFilterChain;
|
|
|
|
private MockHttpServletRequest request;
|
|
|
|
private MockHttpServletResponse response;
|
|
|
|
private MockFilterChain chain;
|
|
|
|
@Before
|
|
public void setup() {
|
|
this.request = new MockHttpServletRequest();
|
|
this.response = new MockHttpServletResponse();
|
|
this.chain = new MockFilterChain();
|
|
}
|
|
|
|
@Test
|
|
public void requiresAuthentication() throws Exception {
|
|
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
assertThat(this.response.getStatus())
|
|
.isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
}
|
|
|
|
@Test
|
|
public void userAuthenticates() throws Exception {
|
|
this.request.addHeader("Authorization",
|
|
"Basic " + new String(Base64.encode("user:password".getBytes("UTF-8"))));
|
|
|
|
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
|
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
|
}
|
|
|
|
}
|