當前位置: 首頁>>代碼示例>>Java>>正文


Java MockFilterChain類代碼示例

本文整理匯總了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);
}
 
開發者ID:deepu105,項目名稱:spring-io,代碼行數:19,代碼來源:JWTFilterTest.java

示例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());
}
 
開發者ID:crnk-project,項目名稱:crnk-framework,代碼行數:20,代碼來源:CrnkFilterTest.java

示例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);
}
 
開發者ID:alexqdjay,項目名稱:simple-session,代碼行數:17,代碼來源:SimpleSessionFilterTest.java

示例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();
        }
    }
}
 
開發者ID:simbest,項目名稱:simbest-cores,代碼行數:27,代碼來源:AntiSamyConcurrentTest.java

示例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());
}
 
開發者ID:AusDTO,項目名稱:spring-security-stateless,代碼行數:17,代碼來源:HttpsOnlyFilterTest.java

示例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()));
}
 
開發者ID:katharsis-project,項目名稱:katharsis-framework,代碼行數:22,代碼來源:KatharsisFilterTest.java

示例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()));
}
 
開發者ID:katharsis-project,項目名稱:katharsis-framework,代碼行數:22,代碼來源:SampleKatharsisFilterTest.java

示例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


注:本文中的org.springframework.mock.web.MockFilterChain類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。