本文整理匯總了Java中org.springframework.mock.web.MockFilterChain類的典型用法代碼示例。如果您正苦於以下問題:Java MockFilterChain類的具體用法?Java MockFilterChain怎麽用?Java MockFilterChain使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MockFilterChain類屬於org.springframework.mock.web包,在下文中一共展示了MockFilterChain類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testJWTFilter
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void testJWTFilter() throws Exception {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
"test-user",
"test-password",
Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.USER))
);
String jwt = tokenProvider.createToken(authentication, false);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
request.setRequestURI("/api/test");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
jwtFilter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("test-user");
assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials().toString()).isEqualTo(jwt);
}
示例2: onNonRepositoryRequestShouldPassTrough
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void onNonRepositoryRequestShouldPassTrough() throws Exception {
MockFilterChain filterChain = new MockFilterChain();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
request.setMethod("GET");
request.setContextPath("");
request.setServletPath(null);
request.setPathInfo(null);
request.setRequestURI("/api/somethingDifferent/");
request.addHeader("Accept", "*/*");
MockHttpServletResponse response = new MockHttpServletResponse();
filter.doFilter(request, response, filterChain);
// no content set yet
Assert.assertEquals(0, response.getContentLength());
}
示例3: test_filter_wrapper
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void test_filter_wrapper() throws IOException, ServletException {
HttpServlet servlet = new HttpServlet() {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
assertThat(req).isInstanceOf(SimpleSessionRequest.class);
super.service(req, res);
}
};
mockFilterChain = new MockFilterChain(servlet, filter);
filter.doFilter(mockRequest, mockResponse, mockFilterChain);
assertThat(mockRequest.getAttribute(SimpleSessionFilter.class.getName().concat(".VISITED")))
.isEqualTo(Boolean.TRUE);
}
示例4: call
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
public String[] call() throws Exception {
synchronized (concurrentThreads) {
concurrentThreads.increment();
//System.out.println("concurrentThreads = " + concurrentThreads);
}
try {
MockHttpServletRequest request = new MockHttpServletRequest();
String randomValue = UUID.randomUUID().toString();
String param_name = "param_name";
request.setParameter(param_name, randomValue);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
try {
filter.doFilter(request, response, filterChain);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
String filteredValue = filterChain.getRequest().getParameter(param_name);
//return the original parameter and filtered parameter
return new String[]{filteredValue, randomValue};
} finally {
synchronized (concurrentThreads) {
concurrentThreads.decrement();
}
}
}
示例5: testRedirectIssuedIfXForwardedProtoHeaderIsHttp
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void testRedirectIssuedIfXForwardedProtoHeaderIsHttp() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("any", "/path/morepath.extension?a=b&c=d");
request.setScheme("http");
request.setServerName("somehost.com");
request.setServerPort(81);
request.addHeader("X-Forwarded-Proto", "http");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
new HttpsOnlyFilter().doFilter(request, response, filterChain);
assertThat(response.getRedirectedUrl(), equalTo("https://somehost.com/path/morepath.extension?a=b&c=d"));
assertThat(filterChain.getRequest(), nullValue());
assertThat(filterChain.getResponse(), nullValue());
}
示例6: testUnacceptableRequestContentType
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void testUnacceptableRequestContentType() throws Exception {
MockFilterChain filterChain = new MockFilterChain();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
request.setMethod("GET");
request.setContextPath("");
request.setServletPath(null);
request.setPathInfo(null);
request.setRequestURI("/api/tasks/");
request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
request.addHeader("Accept", "application/xml");
MockHttpServletResponse response = new MockHttpServletResponse();
katharsisFilter.doFilter(request, response, filterChain);
assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
String responseContent = response.getContentAsString();
assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
示例7: testUnacceptableRequestContentType
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void testUnacceptableRequestContentType() throws Exception {
MockFilterChain filterChain = new MockFilterChain();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
request.setMethod("GET");
request.setContextPath("");
request.setServletPath(null);
request.setPathInfo(null);
request.setRequestURI("/api/tasks/");
request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
request.addHeader("Accept", "application/xml");
MockHttpServletResponse response = new MockHttpServletResponse();
katharsisFilter.doFilter(request, response, filterChain);
assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
String responseContent = response.getContentAsString();
assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
示例8: notAnErrorButNotOK
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void notAnErrorButNotOK() throws Exception {
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).setStatus(201);
super.doFilter(request, response);
response.flushBuffer();
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponse) this.chain.getResponse()).getStatus())
.isEqualTo(201);
assertThat(((HttpServletResponse) ((HttpServletResponseWrapper) this.chain
.getResponse()).getResponse()).getStatus()).isEqualTo(201);
assertThat(this.response.isCommitted()).isTrue();
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:19,代碼來源:ErrorPageFilterTests.java
示例9: unauthorizedWithErrorPath
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void unauthorizedWithErrorPath() throws Exception {
this.filter.addErrorPages(new ErrorPage("/error"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(401, "UNAUTHORIZED");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(this.chain.getRequest()).isEqualTo(this.request);
HttpServletResponseWrapper wrapper = (HttpServletResponseWrapper) this.chain
.getResponse();
assertThat(wrapper.getResponse()).isEqualTo(this.response);
assertThat(this.response.isCommitted()).isTrue();
assertThat(wrapper.getStatus()).isEqualTo(401);
// The real response has to be 401 as well...
assertThat(this.response.getStatus()).isEqualTo(401);
assertThat(this.response.getForwardedUrl()).isEqualTo("/error");
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:23,代碼來源:ErrorPageFilterTests.java
示例10: responseUncommittedWithoutErrorPage
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void responseUncommittedWithoutErrorPage() throws Exception {
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(this.chain.getRequest()).isEqualTo(this.request);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse())
.isEqualTo(this.response);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus())
.isEqualTo(400);
assertThat(this.response.getForwardedUrl()).isNull();
assertThat(this.response.isCommitted()).isTrue();
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:20,代碼來源:ErrorPageFilterTests.java
示例11: globalError
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void globalError() throws Exception {
this.filter.addErrorPages(new ErrorPage("/error"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus())
.isEqualTo(400);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE))
.isEqualTo(400);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE))
.isEqualTo("BAD");
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI))
.isEqualTo("/test/path");
assertThat(this.response.isCommitted()).isTrue();
assertThat(this.response.getForwardedUrl()).isEqualTo("/error");
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:24,代碼來源:ErrorPageFilterTests.java
示例12: statusError
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void statusError() throws Exception {
this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus())
.isEqualTo(400);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE))
.isEqualTo(400);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE))
.isEqualTo("BAD");
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI))
.isEqualTo("/test/path");
assertThat(this.response.isCommitted()).isTrue();
assertThat(this.response.getForwardedUrl()).isEqualTo("/400");
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:24,代碼來源:ErrorPageFilterTests.java
示例13: statusErrorWithCommittedResponse
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void statusErrorWithCommittedResponse() throws Exception {
this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
response.flushBuffer();
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus())
.isEqualTo(400);
assertThat(this.response.isCommitted()).isTrue();
assertThat(this.response.getForwardedUrl()).isNull();
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:19,代碼來源:ErrorPageFilterTests.java
示例14: exceptionError
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void exceptionError() throws Exception {
this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
super.doFilter(request, response);
throw new RuntimeException("BAD");
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus())
.isEqualTo(500);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE))
.isEqualTo(500);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE))
.isEqualTo("BAD");
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE))
.isEqualTo(RuntimeException.class.getName());
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI))
.isEqualTo("/test/path");
assertThat(this.response.isCommitted()).isTrue();
assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:26,代碼來源:ErrorPageFilterTests.java
示例15: subClassExceptionError
import org.springframework.mock.web.MockFilterChain; //導入依賴的package包/類
@Test
public void subClassExceptionError() throws Exception {
this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
super.doFilter(request, response);
throw new IllegalStateException("BAD");
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus())
.isEqualTo(500);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE))
.isEqualTo(500);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE))
.isEqualTo("BAD");
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE))
.isEqualTo(IllegalStateException.class.getName());
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI))
.isEqualTo("/test/path");
assertThat(this.response.isCommitted()).isTrue();
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:25,代碼來源:ErrorPageFilterTests.java