use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class ServerHttpSecurityTests method basicWithGlobalWebSessionServerSecurityContextRepository.
@Test
public void basicWithGlobalWebSessionServerSecurityContextRepository() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("rob", "rob", "ROLE_USER", "ROLE_ADMIN")));
this.http.securityContextRepository(new WebSessionServerSecurityContextRepository());
this.http.httpBasic();
this.http.authenticationManager(this.authenticationManager);
ServerHttpSecurity.AuthorizeExchangeSpec authorize = this.http.authorizeExchange();
authorize.anyExchange().authenticated();
WebTestClient client = buildClient();
// @formatter:off
EntityExchangeResult<String> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("rob", "rob")).exchange().expectStatus().isOk().expectHeader().valueMatches(HttpHeaders.CACHE_CONTROL, ".+").expectBody(String.class).consumeWith((b) -> assertThat(b.getResponseBody()).isEqualTo("ok")).returnResult();
// @formatter:on
assertThat(result.getResponseCookies().getFirst("SESSION")).isNotNull();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class RequestCacheTests method requestCacheNoOp.
@Test
public void requestCacheNoOp() {
// @formatter:off
SecurityWebFilterChain securityWebFilter = this.http.authorizeExchange().anyExchange().authenticated().and().formLogin().and().requestCache().requestCache(NoOpServerRequestCache.getInstance()).and().build();
WebTestClient webTestClient = WebTestClient.bindToController(new SecuredPageController(), new WebTestClientBuilder.Http200RestController()).webFilter(new WebFilterChainProxy(securityWebFilter)).build();
WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();
// @formatter:on
DefaultLoginPage loginPage = SecuredPage.to(driver, DefaultLoginPage.class).assertAt();
// @formatter:off
HomePage securedPage = loginPage.loginForm().username("user").password("password").submit(HomePage.class);
// @formatter:on
securedPage.assertAt();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class RequestCacheTests method requestWhenCustomRequestCacheInLambdaThenCustomCacheUsed.
@Test
public void requestWhenCustomRequestCacheInLambdaThenCustomCacheUsed() {
// @formatter:off
SecurityWebFilterChain securityWebFilter = this.http.authorizeExchange((exchange) -> exchange.anyExchange().authenticated()).formLogin(withDefaults()).requestCache((requestCache) -> requestCache.requestCache(NoOpServerRequestCache.getInstance())).build();
WebTestClient webTestClient = WebTestClient.bindToController(new SecuredPageController(), new WebTestClientBuilder.Http200RestController()).webFilter(new WebFilterChainProxy(securityWebFilter)).build();
WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();
// @formatter:on
DefaultLoginPage loginPage = SecuredPage.to(driver, DefaultLoginPage.class).assertAt();
// @formatter:off
HomePage securedPage = loginPage.loginForm().username("user").password("password").submit(HomePage.class);
// @formatter:on
securedPage.assertAt();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class WebTestClientHtmlUnitDriverBuilderTests method helloWorld.
@Test
public void helloWorld() {
WebTestClient webTestClient = WebTestClient.bindToController(new HelloWorldController()).build();
// @formatter:off
WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();
// @formatter:on
driver.get("http://localhost/");
assertThat(driver.getPageSource()).contains("Hello World");
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class FormLoginTests method defaultLoginPage.
@Test
public void defaultLoginPage() {
// @formatter:off
SecurityWebFilterChain securityWebFilter = this.http.authorizeExchange().anyExchange().authenticated().and().formLogin().and().build();
WebTestClient webTestClient = WebTestClientBuilder.bindToWebFilters(securityWebFilter).build();
WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();
// @formatter:on
DefaultLoginPage loginPage = HomePage.to(driver, DefaultLoginPage.class).assertAt();
// @formatter:off
loginPage = loginPage.loginForm().username("user").password("invalid").submit(DefaultLoginPage.class).assertError();
HomePage homePage = loginPage.loginForm().username("user").password("password").submit(HomePage.class);
// @formatter:on
homePage.assertAt();
loginPage = DefaultLogoutPage.to(driver).assertAt().logout();
loginPage.assertAt().assertLogout();
}
Aggregations