public RedirectViewControllerRegistration(String urlPath, String redirectUrl) { Assert.notNull(urlPath, "'urlPath' is required."); Assert.notNull(redirectUrl, "'redirectUrl' is required."); this.urlPath = urlPath; this.redirectView = new RedirectView(redirectUrl); this.redirectView.setContextRelative(true); this.controller.setView(this.redirectView); }
@Test(expected = IllegalArgumentException.class) public void noUrlSet() throws Exception { RedirectView rv = new RedirectView(); rv.afterPropertiesSet(); }
@Test public void uriTemplateWithObjectConversion() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("foo", new Long(611)); RedirectView redirectView = new RedirectView("/foo/{foo}"); redirectView.renderMergedOutputModel(model, this.request, this.response); assertEquals("/foo/611", this.response.getRedirectedUrl()); }
@Test public void uriTemplateAndArrayQueryParam() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("foo", "bar"); model.put("fooArr", new String[] { "baz", "bazz" }); RedirectView redirectView = new RedirectView("/foo/{foo}"); redirectView.renderMergedOutputModel(model, this.request, this.response); assertEquals("/foo/bar?fooArr=baz&fooArr=bazz", this.response.getRedirectedUrl()); }
@Test public void uriTemplateEncode() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("foo", "bar/bar baz"); String baseUrl = "http://url.somewhere.com"; RedirectView redirectView = new RedirectView(baseUrl + "/context path/{foo}"); redirectView.renderMergedOutputModel(model, this.request, this.response); assertEquals(baseUrl + "/context path/bar%2Fbar%20baz", this.response.getRedirectedUrl()); }
@Test public void uriTemplate() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("foo", "bar"); String baseUrl = "http://url.somewhere.com"; RedirectView redirectView = new RedirectView(baseUrl + "/{foo}"); redirectView.renderMergedOutputModel(model, this.request, this.response); assertEquals(baseUrl + "/bar", this.response.getRedirectedUrl()); }
@Test public void handleViewInstance() throws Exception { ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); assertEquals(RedirectView.class, mavContainer.getView().getClass()); assertEquals("attrValue", mavContainer.getModel().get("attrName")); }
@Test public void emptyRedirectString() throws Exception { Map<String, Object> model = new HashMap<>(); RedirectView redirectView = new RedirectView(""); redirectView.renderMergedOutputModel(model, this.request, this.response); assertEquals("", this.response.getRedirectedUrl()); }
@Test public void handleRedirectAttributesWithViewReference() throws Exception { RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap(); mavContainer.setRedirectModel(redirectAttributes); ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); assertEquals(RedirectView.class, mavContainer.getView().getClass()); assertEquals("attrValue", mavContainer.getModel().get("attrName")); assertSame("RedirectAttributes should be used if controller redirects", redirectAttributes, mavContainer.getModel()); }
@Test public void redirectView() throws Exception { RedirectView view = new RedirectView("/foo"); this.controller.setView(view); ModelAndView modelAndView = this.controller.handleRequest(this.request, this.response); assertSame(view, modelAndView.getView()); }
@Test public void http11() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setHttp10Compatible(false); rv.render(new HashMap<>(), request, response); assertEquals(303, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@Test public void explicitStatusCodeHttp10() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY); rv.render(new HashMap<>(), request, response); assertEquals(301, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@Test public void attributeStatusCodeHttp10() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED); rv.render(new HashMap<>(), request, response); assertEquals(201, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@Test public void dontApplyUriVariables() throws Exception { String url = "/test#{'one','abc'}"; RedirectView redirectView = new RedirectView(url, true); redirectView.setExpandUriTemplateVariables(false); redirectView.renderMergedOutputModel(new ModelMap(), this.request, this.response); assertEquals(url, this.response.getRedirectedUrl()); }
@Test public void explicitStatusCodeHttp11() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setHttp10Compatible(false); rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY); rv.render(new HashMap<>(), request, response); assertEquals(301, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@Test public void attributeStatusCodeHttp11() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setHttp10Compatible(false); request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED); rv.render(new HashMap<>(), request, response); assertEquals(201, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@Test public void propagateQueryParams() throws Exception { RedirectView rv = new RedirectView(); rv.setPropagateQueryParams(true); rv.setUrl("http://url.somewhere.com?foo=bar#bazz"); request.setQueryString("a=b&c=d"); rv.render(new HashMap<>(), request, response); assertEquals(302, response.getStatus()); assertEquals("http://url.somewhere.com?foo=bar&a=b&c=d#bazz", response.getHeader("Location")); }
@Test // SPR-14045 public void handleRedirectWithIgnoreDefaultModel() throws Exception { mavContainer.setIgnoreDefaultModelOnRedirect(true); RedirectView redirectView = new RedirectView(); ModelAndView mav = new ModelAndView(redirectView, "name", "value"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); ModelMap model = mavContainer.getModel(); assertSame(redirectView, mavContainer.getView()); assertEquals(1, model.size()); assertEquals("value", model.get("name")); }
@Test public void returnViewRedirect() throws Exception { RedirectView redirectView = new RedirectView("testView"); ModelMap redirectModel = new RedirectAttributesModelMap(); this.mavContainer.setRedirectModel(redirectModel); MethodParameter param = createReturnValueParam("view"); this.handler.handleReturnValue(redirectView, param, this.mavContainer, this.webRequest); assertSame(redirectView, this.mavContainer.getView()); assertSame("Should have switched to the RedirectModel", redirectModel, this.mavContainer.getModel()); }
@Test // SPR-13693 public void remoteHost() throws Exception { RedirectView rv = new RedirectView(); assertFalse(rv.isRemoteHost("http://url.somewhere.com")); assertFalse(rv.isRemoteHost("/path")); assertFalse(rv.isRemoteHost("http://url.somewhereelse.com")); rv.setHosts(new String[] {"url.somewhere.com"}); assertFalse(rv.isRemoteHost("http://url.somewhere.com")); assertFalse(rv.isRemoteHost("/path")); assertTrue(rv.isRemoteHost("http://url.somewhereelse.com")); }