use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class HeaderSpecTests method assertHeaders.
private void assertHeaders() {
WebTestClient client = buildClient();
FluxExchangeResult<String> response = client.get().uri("https://example.com/").exchange().returnResult(String.class);
Map<String, List<String>> responseHeaders = response.getResponseHeaders();
if (!this.expectedHeaders.isEmpty()) {
assertThat(responseHeaders).describedAs(response.toString()).containsAllEntriesOf(this.expectedHeaders);
}
if (!this.headerNamesNotPresent.isEmpty()) {
assertThat(responseHeaders.keySet()).doesNotContainAnyElementsOf(this.headerNamesNotPresent);
}
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AnonymousAuthenticationWebFilterTests method anonymousAuthenticationFilterWorking.
@Test
public void anonymousAuthenticationFilterWorking() {
WebTestClient client = WebTestClientBuilder.bindToControllerAndWebFilters(HttpMeController.class, new AnonymousAuthenticationWebFilter(UUID.randomUUID().toString())).build();
client.get().uri("/me").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("anonymousUser");
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationFailThenUnauthorized.
@Test
public void filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationFailThenUnauthorized() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.error(new BadCredentialsException("failed")));
given(this.authenticationManagerResolver.resolve(any())).willReturn(Mono.just(this.authenticationManager));
this.filter = new AuthenticationWebFilter(this.authenticationManagerResolver);
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
EntityExchangeResult<Void> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("test", "this")).exchange().expectStatus().isUnauthorized().expectHeader().valueMatches("WWW-Authenticate", "Basic realm=\"Realm\"").expectBody().isEmpty();
assertThat(result.getResponseCookies()).isEmpty();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenDefaultsAndAuthenticationFailThenUnauthorized.
@Test
public void filterWhenDefaultsAndAuthenticationFailThenUnauthorized() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.error(new BadCredentialsException("failed")));
this.filter = new AuthenticationWebFilter(this.authenticationManager);
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
EntityExchangeResult<Void> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("test", "this")).exchange().expectStatus().isUnauthorized().expectHeader().valueMatches("WWW-Authenticate", "Basic realm=\"Realm\"").expectBody().isEmpty();
assertThat(result.getResponseCookies()).isEmpty();
}
use of org.springframework.test.web.reactive.server.WebTestClient in project spring-security by spring-projects.
the class AuthenticationWebFilterTests method filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationSuccessThenContinues.
@Test
public void filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationSuccessThenContinues() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("test", "this", "ROLE")));
given(this.authenticationManagerResolver.resolve(any())).willReturn(Mono.just(this.authenticationManager));
this.filter = new AuthenticationWebFilter(this.authenticationManagerResolver);
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.filter).build();
EntityExchangeResult<String> result = client.get().uri("/").headers((headers) -> headers.setBasicAuth("test", "this")).exchange().expectStatus().isOk().expectBody(String.class).consumeWith((b) -> assertThat(b.getResponseBody()).isEqualTo("ok")).returnResult();
assertThat(result.getResponseCookies()).isEmpty();
}
Aggregations