org.springframework.mock.web.test.MockFilterConfig Java Examples

The following examples show how to use org.springframework.mock.web.test.MockFilterConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CharacterEncodingFilterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void doesNotIfEncodingIsNotEmptyAndNotForced() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	given(request.getCharacterEncoding()).willReturn(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX)).willReturn(null);

	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, Boolean.TRUE);
	verify(request).removeAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX);
	verify(filterChain).doFilter(request, response);
}
 
Example #2
Source File: CharacterEncodingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void setForceEncodingOnRequestOnly() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	request.setCharacterEncoding(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	HttpServletResponse response = mock(HttpServletResponse.class);
	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true, false);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(request, times(2)).setCharacterEncoding(ENCODING);
	verify(response, never()).setCharacterEncoding(ENCODING);
	verify(filterChain).doFilter(request, response);
}
 
Example #3
Source File: CharacterEncodingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void doesNotIfEncodingIsNotEmptyAndNotForced() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	given(request.getCharacterEncoding()).willReturn(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(filterChain).doFilter(request, response);
}
 
Example #4
Source File: CharacterEncodingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void encodingIfEmptyAndNotForced() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	given(request.getCharacterEncoding()).willReturn(null);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setCharacterEncoding(ENCODING);
	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(filterChain).doFilter(request, response);
}
 
Example #5
Source File: CharacterEncodingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void forceEncodingAlwaysSetsEncoding() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	request.setCharacterEncoding(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	HttpServletResponse response = mock(HttpServletResponse.class);
	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(response).setCharacterEncoding(ENCODING);
	verify(filterChain).doFilter(request, response);
}
 
Example #6
Source File: CompositeFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCompositeFilter() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	MockFilter targetFilter = new MockFilter();
	MockFilterConfig proxyConfig = new MockFilterConfig(sc);

	CompositeFilter filterProxy = new CompositeFilter();
	filterProxy.setFilters(Arrays.asList(targetFilter));
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNotNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #7
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #8
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterDelegateInstance() throws ServletException, IOException {
	MockFilter targetFilter = new MockFilter();

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(targetFilter);
	filterProxy.init(new MockFilterConfig(new MockServletContext()));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #9
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterName() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #10
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #11
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	// wac.refresh();
	// note that the context is not set as the ROOT attribute in the ServletContext!

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", wac);
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #12
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #13
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterDelegateInstance() throws ServletException, IOException {
	MockFilter targetFilter = new MockFilter();

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(targetFilter);
	filterProxy.init(new MockFilterConfig(new MockServletContext()));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #14
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterName() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #15
Source File: CharacterEncodingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setForceEncodingOnRequestOnly() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	request.setCharacterEncoding(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	HttpServletResponse response = mock(HttpServletResponse.class);
	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true, false);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(request, times(2)).setCharacterEncoding(ENCODING);
	verify(response, never()).setCharacterEncoding(ENCODING);
	verify(filterChain).doFilter(request, response);
}
 
Example #16
Source File: CharacterEncodingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void doesNotIfEncodingIsNotEmptyAndNotForced() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	given(request.getCharacterEncoding()).willReturn(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(filterChain).doFilter(request, response);
}
 
Example #17
Source File: CharacterEncodingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void encodingIfEmptyAndNotForced() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	given(request.getCharacterEncoding()).willReturn(null);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setCharacterEncoding(ENCODING);
	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(filterChain).doFilter(request, response);
}
 
Example #18
Source File: CharacterEncodingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void forceEncodingAlwaysSetsEncoding() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	request.setCharacterEncoding(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);

	HttpServletResponse response = mock(HttpServletResponse.class);
	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
	verify(request).removeAttribute(filteredName(FILTER_NAME));
	verify(response).setCharacterEncoding(ENCODING);
	verify(filterChain).doFilter(request, response);
}
 
Example #19
Source File: CompositeFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCompositeFilter() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	MockFilter targetFilter = new MockFilter();
	MockFilterConfig proxyConfig = new MockFilterConfig(sc);

	CompositeFilter filterProxy = new CompositeFilter();
	filterProxy.setFilters(Arrays.asList(targetFilter));
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNotNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #20
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #21
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #22
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterName() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #23
Source File: CharacterEncodingFilterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void encodingIfEmptyAndNotForced() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	given(request.getCharacterEncoding()).willReturn(null);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX)).willReturn(null);

	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setCharacterEncoding(ENCODING);
	verify(request).setAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, Boolean.TRUE);
	verify(request).removeAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX);
	verify(filterChain).doFilter(request, response);
}
 
Example #24
Source File: CompositeFilterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompositeFilter() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	MockFilter targetFilter = new MockFilter();
	MockFilterConfig proxyConfig = new MockFilterConfig(sc);

	CompositeFilter filterProxy = new CompositeFilter();
	filterProxy.setFilters(Arrays.asList(targetFilter));
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNotNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #25
Source File: CharacterEncodingFilterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void forceEncodingAlwaysSetsEncoding() throws Exception {
	HttpServletRequest request = mock(HttpServletRequest.class);
	request.setCharacterEncoding(ENCODING);
	given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
	given(request.getAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX)).willReturn(null);

	HttpServletResponse response = mock(HttpServletResponse.class);
	FilterChain filterChain = mock(FilterChain.class);

	CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true);
	filter.init(new MockFilterConfig(FILTER_NAME));
	filter.doFilter(request, response, filterChain);

	verify(request).setAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, Boolean.TRUE);
	verify(request).removeAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX);
	verify(response).setCharacterEncoding(ENCODING);
	verify(filterChain).doFilter(request, response);
}
 
Example #26
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #27
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithFilterDelegateInstance() throws ServletException, IOException {
	MockFilter targetFilter = new MockFilter();

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(targetFilter);
	filterProxy.init(new MockFilterConfig(new MockServletContext()));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #28
Source File: CommonsMultipartResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void withServletContextAndFilter() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
	wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
	wac.refresh();
	wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
	assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));

	MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
	filterConfig.addInitParameter("class", "notWritable");
	filterConfig.addInitParameter("unknownParam", "someValue");
	final MultipartFilter filter = new MultipartFilter();
	filter.init(filterConfig);

	final List<MultipartFile> files = new ArrayList<MultipartFile>();
	final FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
			MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
			files.addAll(request.getFileMap().values());
		}
	};

	FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);

	MockHttpServletRequest originalRequest = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	originalRequest.setMethod("POST");
	originalRequest.setContentType("multipart/form-data");
	originalRequest.addHeader("Content-type", "multipart/form-data");
	filter.doFilter(originalRequest, response, filterChain2);

	CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
	CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
	assertTrue(((MockFileItem) file1.getFileItem()).deleted);
	assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
 
Example #29
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelegatingFilterProxy() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example #30
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithLazyContextStartup() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}