Search in sources :

Example 1 with TestingServerWebExchange

use of org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange in project thymeleaf-tests by thymeleaf.

the class ReactiveTestUtils method buildReactiveContext.

public static ISpringWebFluxContext buildReactiveContext(final Map<String, Object> model, final RequestDataValueProcessor requestDataValueProcessor) {
    final ServerWebExchange exchange = new TestingServerWebExchange("reactive07", Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
    final TestingMessageSource testingMessageSource = new TestingMessageSource();
    final RequestContext requestContext = new RequestContext(exchange, model, testingMessageSource, requestDataValueProcessor);
    final SpringWebFluxThymeleafRequestContext thymeleafRequestContext = new SpringWebFluxThymeleafRequestContext(requestContext, exchange);
    model.put(SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext);
    model.put(SpringContextVariableNames.THYMELEAF_REQUEST_CONTEXT, thymeleafRequestContext);
    final TestingSpringWebFluxContext context = new TestingSpringWebFluxContext(exchange);
    context.setVariables(model);
    return context;
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) TestingSpringWebFluxContext(org.thymeleaf.spring5.reactive.context.TestingSpringWebFluxContext) TestingMessageSource(org.thymeleaf.spring5.reactive.messagesource.TestingMessageSource) SpringWebFluxThymeleafRequestContext(org.thymeleaf.spring5.context.webflux.SpringWebFluxThymeleafRequestContext) RequestContext(org.springframework.web.reactive.result.view.RequestContext) SpringWebFluxThymeleafRequestContext(org.thymeleaf.spring5.context.webflux.SpringWebFluxThymeleafRequestContext)

Example 2 with TestingServerWebExchange

use of org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange in project thymeleaf-tests by thymeleaf.

the class ThymeleafReactiveViewTest method testSeveralConversionServices.

@Test
public void testSeveralConversionServices() throws Exception {
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring5/view/applicationContextConversionService.xml");
    final SpringWebFluxTemplateEngine templateEngine = new SpringWebFluxTemplateEngine();
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("spring5/view/");
    templateResolver.setSuffix(".html");
    templateEngine.setTemplateResolver(templateResolver);
    final ThymeleafReactiveViewResolver resolver = new ThymeleafReactiveViewResolver();
    resolver.setApplicationContext(context);
    resolver.setTemplateEngine(templateEngine);
    resolver.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_HTML));
    // testview2 does not exist as a declared bean at the application context
    final Mono<View> view = resolver.resolveViewName("testSeveralConversionServices", Locale.US);
    final Map<String, Object> model = new ModelMap();
    model.put("one", "one");
    final TestingServerWebExchange exchange = new TestingServerWebExchange("/testing", new HashMap<>(), new HashMap<>(), new HashMap<>());
    view.flatMap(v -> v.render(model, MediaType.TEXT_HTML, exchange)).block();
}
Also used : ThymeleafReactiveViewResolver(org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) MediaType(org.springframework.http.MediaType) HashMap(java.util.HashMap) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ModelMap(org.springframework.ui.ModelMap) Locale(java.util.Locale) Map(java.util.Map) SpringWebFluxTemplateEngine(org.thymeleaf.spring5.SpringWebFluxTemplateEngine) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) View(org.springframework.web.reactive.result.view.View) Collections(java.util.Collections) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) SpringWebFluxTemplateEngine(org.thymeleaf.spring5.SpringWebFluxTemplateEngine) ModelMap(org.springframework.ui.ModelMap) View(org.springframework.web.reactive.result.view.View) ThymeleafReactiveViewResolver(org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 3 with TestingServerWebExchange

use of org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange in project thymeleaf-tests by thymeleaf.

the class AbstractSpring5ReactiveTest method testTemplateSpringView.

private static void testTemplateSpringView(final String template, final Set<String> markupSelectors, final IContext context, final String result, final boolean sse, final int responseMaxChunkSizeBytes) throws Exception {
    if (context instanceof ISpringWebFluxContext) {
        // we can influence when directly instantiating the ThymeleafReactiveView. So we'll just skip
        return;
    }
    final String dataDriverVariableName = detectDataDriver(context);
    final boolean isDataDriven = dataDriverVariableName != null;
    final ThymeleafReactiveView thymeleafReactiveView = createThymeleafReactiveView(templateEngine, template, markupSelectors, Locale.US, responseMaxChunkSizeBytes);
    final Map<String, Object> model = new HashMap<String, Object>();
    for (final String variableName : context.getVariableNames()) {
        model.put(variableName, context.getVariable(variableName));
    }
    final ServerWebExchange serverWebExchange = new TestingServerWebExchange("testing", Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
    List<DataBuffer> resultBuffers = null;
    try {
        final Mono<ServerHttpResponse> responseStream = thymeleafReactiveView.render(model, (sse ? sseMediaType : htmlMediaType), serverWebExchange).then(Mono.just(serverWebExchange.getResponse()));
        final ServerHttpResponse response = responseStream.block();
        resultBuffers = ((TestingServerHttpResponse) response).getWrittenOutput();
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error happened while executing reactive test for template " + template + " with markup " + "selectors " + markupSelectors + ", context with variables " + context.getVariableNames() + " and " + "response chunk size of " + responseMaxChunkSizeBytes + " bytes.", e);
    }
    if (responseMaxChunkSizeBytes != Integer.MAX_VALUE) {
        for (final DataBuffer resultBuffer : resultBuffers) {
            Assert.assertTrue("Buffer returned by stream is of size larger than " + responseMaxChunkSizeBytes, resultBuffer.readableByteCount() <= responseMaxChunkSizeBytes);
        }
    } else {
        if (!isDataDriven) {
            final int bufferCount = resultBuffers.size();
            Assert.assertTrue("No limit set on buffer size, and non-data-driven: there should only be one result buffer instead of " + bufferCount, bufferCount == 1);
        }
    }
    final String resultStr = resultBuffers.stream().map((buffer) -> ReactiveTestUtils.bufferAsString(buffer, charset)).map(// Note we NORMALIZE before joining it all
    ReactiveTestUtils::normalizeResult).collect(Collectors.joining());
    final String expected = ReactiveTestUtils.readExpectedNormalizedResults(result, charset);
    Assert.assertEquals(expected, resultStr);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) HashMap(java.util.HashMap) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) TestingServerHttpResponse(org.thymeleaf.spring5.reactive.exchange.TestingServerHttpResponse) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) ThymeleafReactiveView(org.thymeleaf.spring5.view.reactive.ThymeleafReactiveView) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) ISpringWebFluxContext(org.thymeleaf.spring5.context.webflux.ISpringWebFluxContext) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

TestingServerWebExchange (org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange)3 HashMap (java.util.HashMap)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 Collections (java.util.Collections)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Test (org.junit.Test)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 MediaType (org.springframework.http.MediaType)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ModelMap (org.springframework.ui.ModelMap)1 RequestContext (org.springframework.web.reactive.result.view.RequestContext)1 View (org.springframework.web.reactive.result.view.View)1 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 SpringWebFluxTemplateEngine (org.thymeleaf.spring5.SpringWebFluxTemplateEngine)1 ISpringWebFluxContext (org.thymeleaf.spring5.context.webflux.ISpringWebFluxContext)1 SpringWebFluxThymeleafRequestContext (org.thymeleaf.spring5.context.webflux.SpringWebFluxThymeleafRequestContext)1 TestingSpringWebFluxContext (org.thymeleaf.spring5.reactive.context.TestingSpringWebFluxContext)1 TestingServerHttpResponse (org.thymeleaf.spring5.reactive.exchange.TestingServerHttpResponse)1