/** * Create a new builder for a redirect through a {@link RedirectView}. * @param url the redirect URL * @return the builder */ static RedirectBuilder redirectTo(String url) { return new DefaultRenderingBuilder(new RedirectView(url)); }
@Test(expected = IllegalArgumentException.class) public void noUrlSet() throws Exception { RedirectView rv = new RedirectView(null); rv.afterPropertiesSet(); }
@Test public void expandUriTemplateVariablesFromExchangeAttribute() { String url = "http://url.somewhere.com?foo={foo}"; Map<String, String> attributes = Collections.singletonMap("foo", "bar"); this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, attributes); RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange).block(); assertEquals(URI.create("http://url.somewhere.com?foo=bar"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void contextRelative() { String url = "/test.html"; RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(URI.create("/context/test.html"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void expandUriTemplateVariablesFromModel() { String url = "http://url.somewhere.com?foo={foo}"; Map<String, String> model = Collections.singletonMap("foo", "bar"); RedirectView view = new RedirectView(url); view.render(model, MediaType.TEXT_HTML, this.exchange).block(); assertEquals(URI.create("http://url.somewhere.com?foo=bar"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void contextRelativeQueryParam() { String url = "/test.html?id=1"; RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(URI.create("/context/test.html?id=1"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void customStatusCode() { String url = "http://url.somewhere.com"; RedirectView view = new RedirectView(url, HttpStatus.FOUND); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(HttpStatus.FOUND, this.exchange.getResponse().getStatusCode()); assertEquals(URI.create(url), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void defaultStatusCode() { String url = "http://url.somewhere.com"; RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(HttpStatus.SEE_OTHER, this.exchange.getResponse().getStatusCode()); assertEquals(URI.create(url), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void propagateQueryParams() throws Exception { RedirectView view = new RedirectView("http://url.somewhere.com?foo=bar#bazz"); view.setPropagateQuery(true); this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://url.somewhere.com?a=b&c=d")); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(HttpStatus.SEE_OTHER, this.exchange.getResponse().getStatusCode()); assertEquals(URI.create("http://url.somewhere.com?foo=bar&a=b&c=d#bazz"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void customizedRedirectView() throws Exception { this.resolver.setRedirectViewProvider(url -> new RedirectView(url, HttpStatus.FOUND)); Mono<View> mono = this.resolver.resolveViewName("redirect:foo", Locale.US); StepVerifier.create(mono) .consumeNextWith(view -> { assertEquals(RedirectView.class, view.getClass()); RedirectView redirectView = (RedirectView) view; assertEquals("foo", redirectView.getUrl()); assertEquals(HttpStatus.FOUND, redirectView.getStatusCode()); }) .expectComplete() .verify(Duration.ZERO); }
@Test public void remoteHost() { RedirectView view = new RedirectView(""); assertFalse(view.isRemoteHost("http://url.somewhere.com")); assertFalse(view.isRemoteHost("/path")); assertFalse(view.isRemoteHost("http://url.somewhereelse.com")); view.setHosts("url.somewhere.com"); assertFalse(view.isRemoteHost("http://url.somewhere.com")); assertFalse(view.isRemoteHost("/path")); assertTrue(view.isRemoteHost("http://url.somewhereelse.com")); }