@Override public WebTestClient build() { ClientHttpConnector connectorToUse = this.connector; if (connectorToUse == null) { Assert.state(this.httpHandlerBuilder != null, "No WebHttpHandlerBuilder available"); connectorToUse = new HttpHandlerConnector(this.httpHandlerBuilder.build()); } return new DefaultWebTestClient(this.webClientBuilder, connectorToUse, this.responseTimeout, new DefaultWebTestClientBuilder(this)); }
/** * Convert the given {@linkplain RouterFunction router function} into a {@link HttpHandler}, * using the given strategies. * <p>The returned {@code HttpHandler} can be adapted to run in * <ul> * <li>Servlet 3.1+ using the * {@link org.springframework.http.server.reactive.ServletHttpHandlerAdapter},</li> * <li>Reactor using the * {@link org.springframework.http.server.reactive.ReactorHttpHandlerAdapter},</li> * <li>Undertow using the * {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li> * </ul> * @param routerFunction the router function to convert * @param strategies the strategies to use * @return an http handler that handles HTTP request using the given router function */ public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) { WebHandler webHandler = toWebHandler(routerFunction, strategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(strategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(strategies.exceptionHandlers())) .localeContextResolver(strategies.localeContextResolver()) .build(); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { String servletName = getServletName(); Assert.hasLength(servletName, "getServletName() must not return null or empty"); ApplicationContext applicationContext = createApplicationContext(); Assert.notNull(applicationContext, "createApplicationContext() must not return null"); refreshApplicationContext(applicationContext); registerCloseListener(servletContext, applicationContext); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build(); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet); if (registration == null) { throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " + "Check if there is another servlet registered under the same name."); } registration.setLoadOnStartup(1); registration.addMapping(getServletMapping()); registration.setAsyncSupported(true); }
@Override protected HttpHandler createHttpHandler() { this.wac = new AnnotationConfigApplicationContext(); this.wac.register(TestConfiguration.class); this.wac.refresh(); return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build(); }
@Override protected HttpHandler createHttpHandler() { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(TestConfiguration.class); wac.refresh(); return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build(); }
@Override protected HttpHandler createHttpHandler() { this.restTemplate = initRestTemplate(); this.applicationContext = initApplicationContext(); return WebHttpHandlerBuilder.applicationContext(this.applicationContext).build(); }
@Override protected HttpHandler createHttpHandler() { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(TestConfiguration.class); wac.refresh(); return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build(); }
@Override protected HttpHandler createHttpHandler() { this.wac = new AnnotationConfigApplicationContext(); this.wac.register(TestConfiguration.class); this.wac.refresh(); return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build(); }
@Test // SPR-15074 public void orderedWebExceptionHandlerBeans() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(OrderedExceptionHandlerBeanConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); assertEquals("ExceptionHandlerB", response.getBodyAsString().block(ofMillis(5000))); }
@Test public void configWithoutFilters() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(NoFilterConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); assertEquals("handled", response.getBodyAsString().block(ofMillis(5000))); }
@Override protected HttpHandler createHttpHandler() { this.wac = new AnnotationConfigApplicationContext(); this.wac.register(TestConfiguration.class); this.wac.refresh(); DispatcherHandler webHandler = new DispatcherHandler(); webHandler.setApplicationContext(this.wac); return WebHttpHandlerBuilder.webHandler(webHandler).build(); }
private HttpHandler createHttpHandler() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DispatcherConfig.class, this.serverConfigClass); context.register(getWebConfigClass()); context.refresh(); return WebHttpHandlerBuilder.applicationContext(context).build(); }
@Test // SPR-16972 public void cloneWithApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(NoFilterConfig.class); context.refresh(); WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.applicationContext(context); assertSame(context, ((HttpWebHandlerAdapter) builder.build()).getApplicationContext()); assertSame(context, ((HttpWebHandlerAdapter) builder.clone().build()).getApplicationContext()); }
@Override protected HttpHandler createHttpHandler() { this.sessionManager = new DefaultWebSessionManager(); this.handler = new TestWebHandler(); return WebHttpHandlerBuilder.webHandler(this.handler).sessionManager(this.sessionManager).build(); }
@Override protected HttpHandler createHttpHandler() { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(WebConfig.class); wac.refresh(); return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)) .exceptionHandler(new ResponseStatusExceptionHandler()) .build(); }
private HttpHandler createHttpHandler() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebSocketTestConfig.class); context.refresh(); return WebHttpHandlerBuilder.applicationContext(context).build(); }
@Test // SPR-15074 public void orderedWebFilterBeans() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(OrderedWebFilterBeanConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); assertTrue(httpHandler instanceof HttpWebHandlerAdapter); assertSame(context, ((HttpWebHandlerAdapter) httpHandler).getApplicationContext()); MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); assertEquals("FilterB::FilterA", response.getBodyAsString().block(ofMillis(5000))); }
@Test public void servletPathMapping() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebAppConfig.class); context.refresh(); File base = new File(System.getProperty("java.io.tmpdir")); TomcatHttpServer server = new TomcatHttpServer(base.getAbsolutePath()); server.setContextPath("/app"); server.setServletMapping("/api/*"); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); server.setHandler(httpHandler); server.afterPropertiesSet(); server.start(); try { RestTemplate restTemplate = new RestTemplate(); String actual; String url = "http://localhost:" + server.getPort() + "/app/api/test"; actual = restTemplate.getForObject(url, String.class); assertEquals("Tested in /app/api", actual); } finally { server.stop(); } }
@Test public void multipleWebFluxApps() throws Exception { AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(); context1.register(WebAppConfig.class); context1.refresh(); AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(); context2.register(WebAppConfig.class); context2.refresh(); HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build(); HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build(); ReactorHttpServer server = new ReactorHttpServer(); server.registerHttpHandler("/webApp1", webApp1Handler); server.registerHttpHandler("/webApp2", webApp2Handler); server.afterPropertiesSet(); server.start(); try { RestTemplate restTemplate = new RestTemplate(); String actual; String url = "http://localhost:" + server.getPort() + "/webApp1/test"; actual = restTemplate.getForObject(url, String.class); assertEquals("Tested in /webApp1", actual); url = "http://localhost:" + server.getPort() + "/webApp2/test"; actual = restTemplate.getForObject(url, String.class); assertEquals("Tested in /webApp2", actual); } finally { server.stop(); } }
@Test public void handleErrorFromFilter() throws Exception { MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); TestExceptionHandler exceptionHandler = new TestExceptionHandler(); WebHttpHandlerBuilder.webHandler(new StubWebHandler()) .filter(new ExceptionFilter()) .exceptionHandler(exceptionHandler).build() .handle(request, response) .block(); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); assertNotNull(exceptionHandler.ex); assertEquals("boo", exceptionHandler.ex.getMessage()); }