Skip to content

Commit a3df197

Browse files
committed
Polish LDAP Test to Confirm Authenticated
This commit updates a test that confirms the users is successfully authenticated. It uses SecurityMockMvcResultMatchers to check the SecurityContextHolder. It further updates one of the assertions which was working only when the resulting JSON was malformed.
1 parent af2e8d8 commit a3df197

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

security/security-ldap/src/test/java/com/example/security/ldap/SecurityLdapApplicationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
import org.springframework.test.web.servlet.MockMvc;
2525

2626
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
27+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
2728
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2930
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3031

3132
@SpringBootTest
@@ -40,7 +41,8 @@ void rootWhenAuthenticatedThenSaysHelloUser() throws Exception {
4041
// @formatter:off
4142
this.mvc.perform(get("/")
4243
.with(httpBasic("user", "password")))
43-
.andExpect(content().string("Hello, user!"));
44+
.andExpect(authenticated().withUsername("user"))
45+
.andExpect(jsonPath("$").value("Hello, user!"));
4446
// @formatter:on
4547
}
4648

0 commit comments

Comments
 (0)