@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 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 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 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()); }