Search in sources :

Example 1 with JakartaServletWebApplication

use of org.thymeleaf.web.servlet.JakartaServletWebApplication in project spring-boot by spring-projects.

the class ThymeleafServletAutoConfigurationTests method useSecurityDialect.

@Test
void useSecurityDialect() {
    this.contextRunner.run((context) -> {
        TemplateEngine engine = context.getBean(TemplateEngine.class);
        MockServletContext servletContext = new MockServletContext();
        JakartaServletWebApplication webApplication = JakartaServletWebApplication.buildApplication(servletContext);
        WebContext attrs = new WebContext(webApplication.buildExchange(new MockHttpServletRequest(servletContext), new MockHttpServletResponse()));
        try {
            SecurityContextHolder.setContext(new SecurityContextImpl(new TestingAuthenticationToken("alice", "admin")));
            String result = engine.process("security-dialect", attrs);
            assertThat(result).isEqualTo("<html><body><div>alice</div></body></html>" + System.lineSeparator());
        } finally {
            SecurityContextHolder.clearContext();
        }
    });
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) JakartaServletWebApplication(org.thymeleaf.web.servlet.JakartaServletWebApplication) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) WebContext(org.thymeleaf.context.WebContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)1 TemplateEngine (org.thymeleaf.TemplateEngine)1 WebContext (org.thymeleaf.context.WebContext)1 SpringTemplateEngine (org.thymeleaf.spring6.SpringTemplateEngine)1 JakartaServletWebApplication (org.thymeleaf.web.servlet.JakartaServletWebApplication)1