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

The following examples show how to use org.springframework.mock.web.test.MockFilterChain. 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: ForwardedHeaderFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private String sendRedirect(final String location) throws ServletException, IOException {
	Filter filter = new OncePerRequestFilter() {
		@Override
		protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
				FilterChain chain) throws IOException {

			res.sendRedirect(location);
		}
	};

	MockHttpServletResponse response = new MockHttpServletResponse();
	FilterChain filterChain = new MockFilterChain(mock(HttpServlet.class), this.filter, filter);
	filterChain.doFilter(request, response);

	return response.getRedirectedUrl();
}
 
Example #2
Source File: FormContentFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
	for (HttpMethod method : HttpMethod.values()) {
		MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
		request.setContent("foo=bar".getBytes("ISO-8859-1"));
		request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
		this.filterChain = new MockFilterChain();
		this.filter.doFilter(request, this.response, this.filterChain);
		if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
			assertNotSame(request, this.filterChain.getRequest());
		}
		else {
			assertSame(request, this.filterChain.getRequest());
		}
	}
}
 
Example #3
Source File: FormContentFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
	for (HttpMethod method : HttpMethod.values()) {
		MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
		request.setContent("foo=bar".getBytes("ISO-8859-1"));
		request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
		this.filterChain = new MockFilterChain();
		this.filter.doFilter(request, this.response, this.filterChain);
		if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
			assertNotSame(request, this.filterChain.getRequest());
		}
		else {
			assertSame(request, this.filterChain.getRequest());
		}
	}
}
 
Example #4
Source File: ResourceUrlProviderJavaConfigTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());

	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request = new MockHttpServletRequest("GET", "/");
	this.request.setContextPath("/myapp");
	this.response = new MockHttpServletResponse();

	Object urlProvider = context.getBean(ResourceUrlProvider.class);
	this.request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
}
 
Example #5
Source File: ForwardedHeaderFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private String sendRedirect(final String location) throws ServletException, IOException {
	Filter filter = new OncePerRequestFilter() {
		@Override
		protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
				FilterChain chain) throws IOException {

			res.sendRedirect(location);
		}
	};

	MockHttpServletResponse response = new MockHttpServletResponse();
	FilterChain filterChain = new MockFilterChain(mock(HttpServlet.class), this.filter, filter);
	filterChain.doFilter(request, response);

	return response.getRedirectedUrl();
}
 
Example #6
Source File: ResourceUrlProviderJavaConfigTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request = new MockHttpServletRequest("GET", "/");
	this.request.setContextPath("/myapp");
	this.response = new MockHttpServletResponse();

	this.filterChain = new MockFilterChain(this.servlet,
			new ResourceUrlEncodingFilter(),
			(request, response, chain) -> {
				Object urlProvider = context.getBean(ResourceUrlProvider.class);
				request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
				chain.doFilter(request, response);
			});
}
 
Example #7
Source File: ResourceUrlProviderJavaConfigTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(WebConfig.class);
	context.refresh();

	this.request = new MockHttpServletRequest("GET", "/");
	this.request.setContextPath("/myapp");
	this.response = new MockHttpServletResponse();

	this.filterChain = new MockFilterChain(this.servlet,
			new ResourceUrlEncodingFilter(),
			(request, response, chain) -> {
				Object urlProvider = context.getBean(ResourceUrlProvider.class);
				request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
				chain.doFilter(request, response);
			});
}
 
Example #8
Source File: RelativeRedirectFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void sendRedirect(String location) throws Exception {
	MockFilterChain chain = new MockFilterChain();
	this.filter.doFilterInternal(new MockHttpServletRequest(), this.response, chain);

	HttpServletResponse wrappedResponse = (HttpServletResponse) chain.getResponse();
	wrappedResponse.sendRedirect(location);
}
 
Example #9
Source File: ForwardedHeaderFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
@SuppressWarnings("serial")
public void setup() throws Exception {
	this.request = new MockHttpServletRequest();
	this.request.setScheme("http");
	this.request.setServerName("localhost");
	this.request.setServerPort(80);
	this.filterChain = new MockFilterChain(new HttpServlet() {});
}
 
Example #10
Source File: FormContentFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void invalidMediaType() throws Exception {
	this.request.setContent("".getBytes("ISO-8859-1"));
	this.request.setContentType("foo");
	this.filterChain = new MockFilterChain();
	this.filter.doFilter(this.request, this.response, this.filterChain);
	assertSame(this.request, this.filterChain.getRequest());
}
 
Example #11
Source File: FormContentFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void wrapFormEncodedOnly() throws Exception {
	String[] contentTypes = new String[] {"text/plain", "multipart/form-data"};
	for (String contentType : contentTypes) {
		MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/");
		request.setContent("".getBytes("ISO-8859-1"));
		request.setContentType(contentType);
		this.filterChain = new MockFilterChain();
		this.filter.doFilter(request, this.response, this.filterChain);
		assertSame(request, this.filterChain.getRequest());
	}
}
 
Example #12
Source File: FormContentFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.request = new MockHttpServletRequest("PUT", "/");
	this.request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
	this.response = new MockHttpServletResponse();
	this.filterChain = new MockFilterChain();
}
 
Example #13
Source File: HttpPutFormContentFilterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	filter = new HttpPutFormContentFilter();
	request = new MockHttpServletRequest("PUT", "/");
	request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
	response = new MockHttpServletResponse();
	filterChain = new MockFilterChain();
}
 
Example #14
Source File: RelativeRedirectFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void sendRedirect(String location) throws Exception {
	MockFilterChain chain = new MockFilterChain();
	this.filter.doFilterInternal(new MockHttpServletRequest(), this.response, chain);

	HttpServletResponse wrappedResponse = (HttpServletResponse) chain.getResponse();
	wrappedResponse.sendRedirect(location);
}
 
Example #15
Source File: ForwardedHeaderFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
@SuppressWarnings("serial")
public void setup() throws Exception {
	this.request = new MockHttpServletRequest();
	this.request.setScheme("http");
	this.request.setServerName("localhost");
	this.request.setServerPort(80);
	this.filterChain = new MockFilterChain(new HttpServlet() {});
}
 
Example #16
Source File: FormContentFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void invalidMediaType() throws Exception {
	this.request.setContent("".getBytes("ISO-8859-1"));
	this.request.setContentType("foo");
	this.filterChain = new MockFilterChain();
	this.filter.doFilter(this.request, this.response, this.filterChain);
	assertSame(this.request, this.filterChain.getRequest());
}
 
Example #17
Source File: FormContentFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void wrapFormEncodedOnly() throws Exception {
	String[] contentTypes = new String[] {"text/plain", "multipart/form-data"};
	for (String contentType : contentTypes) {
		MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/");
		request.setContent("".getBytes("ISO-8859-1"));
		request.setContentType(contentType);
		this.filterChain = new MockFilterChain();
		this.filter.doFilter(request, this.response, this.filterChain);
		assertSame(request, this.filterChain.getRequest());
	}
}
 
Example #18
Source File: FormContentFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.request = new MockHttpServletRequest("PUT", "/");
	this.request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
	this.response = new MockHttpServletResponse();
	this.filterChain = new MockFilterChain();
}
 
Example #19
Source File: HttpPutFormContentFilterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void wrapPutAndPatchOnly() throws Exception {
	request.setContent("".getBytes("ISO-8859-1"));
	for (HttpMethod method : HttpMethod.values()) {
		request.setMethod(method.name());
		filterChain = new MockFilterChain();
		filter.doFilter(request, response, filterChain);
		if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)) {
			assertNotSame("Should wrap HTTP method " + method, request, filterChain.getRequest());
		}
		else {
			assertSame("Should not wrap for HTTP method " + method, request, filterChain.getRequest());
		}
	}
}
 
Example #20
Source File: HttpPutFormContentFilterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void wrapFormEncodedOnly() throws Exception {
	request.setContent("".getBytes("ISO-8859-1"));
	String[] contentTypes = new String[] {"text/plain", "multipart/form-data"};
	for (String contentType : contentTypes) {
		request.setContentType(contentType);
		filterChain = new MockFilterChain();
		filter.doFilter(request, response, filterChain);
		assertSame("Should not wrap for content type " + contentType, request, filterChain.getRequest());
	}
}
 
Example #21
Source File: HttpPutFormContentFilterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invalidMediaType() throws Exception {
	request.setContent("".getBytes("ISO-8859-1"));
	request.setContentType("foo");
	filterChain = new MockFilterChain();
	filter.doFilter(request, response, filterChain);
	assertSame(request, filterChain.getRequest());
}
 
Example #22
Source File: ServletUriComponentsBuilderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
	MockFilterChain chain = new MockFilterChain();
	new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
	return (HttpServletRequest) chain.getRequest();
}
 
Example #23
Source File: WebUtilsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
	MockFilterChain chain = new MockFilterChain();
	new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
	return (HttpServletRequest) chain.getRequest();
}
 
Example #24
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void adaptRequestFromForwardedHeaders() throws Exception {
	MockFilterChain chain = new MockFilterChain();
	new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain);
	HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest));
}
 
Example #25
Source File: WebUtilsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
	MockFilterChain chain = new MockFilterChain();
	new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
	return (HttpServletRequest) chain.getRequest();
}
 
Example #26
Source File: ServletUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
	MockFilterChain chain = new MockFilterChain();
	new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
	return (HttpServletRequest) chain.getRequest();
}
 
Example #27
Source File: MvcUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void adaptRequestFromForwardedHeaders() throws Exception {
	MockFilterChain chain = new MockFilterChain();
	new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain);
	HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest));
}