BAEL-1311: Spring Security 5 - Security for Reactive Applications (#3056)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
f02ffe9ae6
commit
8dcc6cb106
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.security;
|
||||
|
||||
import com.baeldung.SpringSecurity5Application;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = SpringSecurity5Application.class)
|
||||
public class SecurityTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
private WebTestClient rest;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.rest = WebTestClient
|
||||
.bindToApplicationContext(this.context)
|
||||
.configureClient()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoCredentials_thenRedirectToLogin() {
|
||||
this.rest.get()
|
||||
.uri("/")
|
||||
.exchange()
|
||||
.expectStatus().is3xxRedirection();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void whenHasCredentials_thenSeesGreeting() {
|
||||
this.rest.get()
|
||||
.uri("/")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).isEqualTo("Hello, user");
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public class WebTestClientTest {
|
||||
.uri("/resource")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.is4xxClientError()
|
||||
.is3xxRedirection()
|
||||
.expectBody();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user