Update RequestContextHolderTests

This commit refactors RequestContextHolderTests in preparation for the
work to be done in SPR-13211.

Issue: SPR-13211
This commit is contained in:
Sam Brannen
2015-07-07 21:37:46 +02:00
parent 50bed38a85
commit 5e24ee9b55

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,60 +16,89 @@
package org.springframework.test.web.servlet.samples.spr;
import javax.servlet.http.HttpServletRequest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.web.context.request.RequestAttributes.*;
/**
* Test for SPR-10025 (access to request attributes via RequestContextHolder).
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
@DirtiesContext
public class RequestContextHolderTests {
private static final String FOO = "foo";
private static final String BAR = "bar";
private static final String BAZ = "baz";
private static final String QUUX = "quux";
private static final String ENIGMA = "enigma";
private static final String PUZZLE = "puzzle";
@Autowired
private WebApplicationContext wac;
@Autowired
private MockHttpServletRequest servletRequest;
private MockHttpServletRequest mockRequest;
@Autowired
private MyScopedController myScopedController;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
this.mockRequest.setAttribute(FOO, BAR);
this.mockMvc = webAppContextSetup(this.wac)
.defaultRequest(get("/").requestAttr(ENIGMA, PUZZLE))
.alwaysExpect(status().isOk())
.build();
}
@Test
public void test() throws Exception {
this.servletRequest.setAttribute("foo1", "bar");
this.mockMvc.perform(get("/myUrl").requestAttr("foo2", "bar")).andExpect(status().isOk());
public void singletonController() throws Exception {
this.mockMvc.perform(get("/singleton").requestAttr(BAZ, QUUX));
}
@Test
public void requestScopedController() throws Exception {
assertTrue("request-scoped controller must be a CGLIB proxy", AopUtils.isCglibProxy(this.myScopedController));
this.mockMvc.perform(get("/requestScoped").requestAttr(BAZ, QUUX));
}
@@ -81,17 +110,47 @@ public class RequestContextHolderTests {
public MyController myController() {
return new MyController();
}
@Bean
@Scope(name = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyScopedController myScopedController() {
return new MyScopedController();
}
}
@Controller
private static void assertRequestAttributes() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
// TODO [SPR-13211] Assert that FOO is BAR, instead of NULL.
// assertThat(attributes.getAttribute(FOO, SCOPE_REQUEST), is(BAR));
assertThat(attributes.getAttribute(FOO, SCOPE_REQUEST), is(nullValue()));
assertThat(attributes.getAttribute(ENIGMA, SCOPE_REQUEST), is(PUZZLE));
assertThat(attributes.getAttribute(BAZ, SCOPE_REQUEST), is(QUUX));
}
@RestController
private static class MyController {
@RequestMapping("/myUrl")
@ResponseBody
@RequestMapping("/singleton")
public void handle() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
assertNull(attributes.getAttribute("foo1", RequestAttributes.SCOPE_REQUEST));
assertNotNull(attributes.getAttribute("foo2", RequestAttributes.SCOPE_REQUEST));
assertRequestAttributes();
}
}
@RestController
private static class MyScopedController {
@Autowired
private HttpServletRequest request;
@RequestMapping("/requestScoped")
public void handle() {
// TODO [SPR-13211] Assert that FOO is BAR, instead of NULL.
// assertThat(this.request.getAttribute(FOO), is(BAR));
assertThat(this.request.getAttribute(FOO), is(nullValue()));
assertRequestAttributes();
}
}