Search in sources :

Example 11 with RequestContext

use of org.springframework.web.servlet.support.RequestContext in project spring-framework by spring-projects.

the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.

private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    vr.setApplicationContext(wac);
    vr.setContentType("myContentType");
    vr.setRequestContextAttribute("rc");
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
    assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
    assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());
    HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);
    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertEquals("Correct view class", RedirectView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
    assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext());
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertEquals("Correct view class", InternalResourceView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) TestBean(org.springframework.tests.sample.beans.TestBean) RequestContext(org.springframework.web.servlet.support.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 12 with RequestContext

use of org.springframework.web.servlet.support.RequestContext in project spring-framework by spring-projects.

the class OptionsTagTests method exposeBindingResult.

@Override
protected void exposeBindingResult(Errors errors) {
    // wrap errors in a Model
    Map model = new HashMap();
    model.put(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, errors);
    // replace the request context with one containing the errors
    MockPageContext pageContext = getPageContext();
    RequestContext context = new RequestContext((HttpServletRequest) pageContext.getRequest(), model);
    pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, context);
}
Also used : HashMap(java.util.HashMap) MockPageContext(org.springframework.mock.web.test.MockPageContext) RequestContext(org.springframework.web.servlet.support.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with RequestContext

use of org.springframework.web.servlet.support.RequestContext in project gocd by gocd.

the class RequestContextTestCase method setUp.

@Before
public void setUp() {
    mockContext.checking(new ExpectationsForRequestContext());
    requestContext = new RequestContext(request);
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) Before(org.junit.Before)

Aggregations

RequestContext (org.springframework.web.servlet.support.RequestContext)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 MockPageContext (org.springframework.mock.web.test.MockPageContext)4 Test (org.junit.Test)3 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 PageContext (javax.servlet.jsp.PageContext)2 MessageSourceResolvable (org.springframework.context.MessageSourceResolvable)2 DefaultMessageSourceResolvable (org.springframework.context.support.DefaultMessageSourceResolvable)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 JspAwareRequestContext (org.springframework.web.servlet.support.JspAwareRequestContext)2 JsonView (com.thoughtworks.go.server.web.JsonView)1 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 ArrayList (java.util.ArrayList)1