org.springframework.mock.web.MockFilterChain Java Examples
The following examples show how to use
org.springframework.mock.web.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: ApmFilterTest.java From apm-agent-java with Apache License 2.0 | 8 votes |
@Test void testNoHeaderRecording() throws IOException, ServletException { when(coreConfiguration.isCaptureHeaders()).thenReturn(false); filterChain = new MockFilterChain(new TestServlet()); final MockHttpServletRequest get = new MockHttpServletRequest("GET", "/foo"); get.addHeader("Elastic-Apm-Traceparent", "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); get.setCookies(new Cookie("foo", "bar")); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); mockResponse.addHeader("foo", "bar"); mockResponse.addHeader("bar", "baz"); filterChain.doFilter(get, mockResponse); assertThat(reporter.getTransactions()).hasSize(1); assertThat(reporter.getFirstTransaction().getContext().getResponse().getHeaders().isEmpty()).isTrue(); assertThat(reporter.getFirstTransaction().getContext().getRequest().getHeaders().isEmpty()).isTrue(); assertThat(reporter.getFirstTransaction().getContext().getRequest().getCookies().isEmpty()).isTrue(); assertThat(reporter.getFirstTransaction().getTraceContext().getTraceId().toString()).isEqualTo("0af7651916cd43dd8448eb211c80319c"); assertThat(reporter.getFirstTransaction().getTraceContext().getParentId().toString()).isEqualTo("b9c7c989f97918e1"); }
Example #2
Source File: JWTFilterTest.java From jhipster-microservices-example with Apache License 2.0 | 7 votes |
@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); }
Example #3
Source File: Log4jMdcLoggingFilterTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testLoggingNoUser() throws Exception { invalidateApplicationUser(null); // Apply user logging filter. Log4jMdcLoggingFilter filterUnderTest = new Log4jMdcLoggingFilter(); filterUnderTest.init(new MockFilterConfig()); MockFilterChain mockChain = new MockFilterChain(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse rsp = new MockHttpServletResponse(); filterUnderTest.doFilter(req, rsp, mockChain); filterUnderTest.destroy(); }
Example #4
Source File: JWTFilterTest.java From 21-points with Apache License 2.0 | 6 votes |
@Test public void testJWTFilterWrongScheme() 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(JWTFilter.AUTHORIZATION_HEADER, "Basic " + 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()).isNull(); }
Example #5
Source File: ApmFilterTest.java From apm-agent-java with Apache License 2.0 | 6 votes |
@Test void captureException() { // we can't use mock(Servlet.class) here as the agent would instrument the created mock which confuses mockito final HttpServlet servlet = new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { throw new ServletException("Bazinga"); } }; filterChain = new MockFilterChain(servlet); assertThatThrownBy(() -> filterChain.doFilter( new MockHttpServletRequest("GET", "/test"), new MockHttpServletResponse())) .isInstanceOf(ServletException.class); assertThat(reporter.getTransactions()).hasSize(1); assertThat(reporter.getFirstTransaction().getContext().getRequest().getMethod()).isEqualTo("GET"); assertThat(reporter.getErrors()).hasSize(1); assertThat(reporter.getFirstError().getContext().getRequest().getMethod()).isEqualTo("GET"); assertThat(reporter.getFirstError().getException().getMessage()).isEqualTo("Bazinga"); }
Example #6
Source File: HttpHeaderAuthenticationFilterTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testHttpHeaderAuthenticationFilterNoSessionInitTime() throws Exception { modifyPropertySourceInEnvironment(getDefaultSecurityEnvironmentVariables()); try { MockHttpServletRequest request = getRequestWithHeaders(USER_ID, "testFirstName", "testLastName", "testEmail", null, null); // Invalidate user session if exists. invalidateApplicationUser(request); httpHeaderAuthenticationFilter.init(new MockFilterConfig()); httpHeaderAuthenticationFilter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); validateHttpHeaderApplicationUser(USER_ID, "testFirstName", "testLastName", "testEmail", (String) null, null, null, null); } finally { restorePropertySourceInEnvironment(); } }
Example #7
Source File: OncePerRequestFilterTests.java From spring-session with Apache License 2.0 | 6 votes |
@BeforeEach @SuppressWarnings("serial") void setup() { this.servlet = new HttpServlet() { }; this.request = new MockHttpServletRequest(); this.response = new MockHttpServletResponse(); this.chain = new MockFilterChain(); this.invocations = new ArrayList<>(); this.filter = new OncePerRequestFilter() { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { OncePerRequestFilterTests.this.invocations.add(this); filterChain.doFilter(request, response); } }; }
Example #8
Source File: JWTFilterTest.java From alchemy with Apache License 2.0 | 6 votes |
@Test public void testJWTFilterWrongScheme() 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(JWTFilter.AUTHORIZATION_HEADER, "Basic " + 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()).isNull(); }
Example #9
Source File: JWTFilterUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void testJWTFilterWrongScheme() 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(JWTFilter.AUTHORIZATION_HEADER, "Basic " + 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()).isNull(); }
Example #10
Source File: ApmFilterTest.java From apm-agent-java with Apache License 2.0 | 6 votes |
@Test void testAllHeaderRecording() throws IOException, ServletException { when(coreConfiguration.isCaptureHeaders()).thenReturn(true); filterChain = new MockFilterChain(new TestServlet()); final MockHttpServletRequest get = new MockHttpServletRequest("GET", "/foo"); get.addHeader("foo", "bar"); get.setCookies(new Cookie("foo", "bar")); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); mockResponse.addHeader("foo", "bar"); mockResponse.addHeader("bar", "baz"); filterChain.doFilter(get, mockResponse); assertThat(reporter.getTransactions()).hasSize(1); final Request request = reporter.getFirstTransaction().getContext().getRequest(); assertThat(request.getHeaders().isEmpty()).isFalse(); assertThat(request.getHeaders().get("foo")).isEqualTo("bar"); assertThat(request.getCookies().get("foo")).isEqualTo("bar"); final Response response = reporter.getFirstTransaction().getContext().getResponse(); assertThat(response.getHeaders().get("foo")).isEqualTo("bar"); assertThat(response.getHeaders().get("bar")).isEqualTo("baz"); }
Example #11
Source File: OptionsEndpointFilterTest.java From webauthn4j-spring-security with Apache License 2.0 | 6 votes |
@Test public void doFilter_test() throws IOException, ServletException { OptionsProvider optionsProvider = mock(OptionsProvider.class); AttestationOptions attestationOptions = new AttestationOptions(null, null, null, null, null, Collections.emptyList(), null); when(optionsProvider.getAttestationOptions(any(), any(), any())).thenReturn(attestationOptions); AssertionOptions assertionOptions = new AssertionOptions(null, null, null, null, null, null); when(optionsProvider.getAssertionOptions(any(), any(), any())).thenReturn(assertionOptions); OptionsEndpointFilter optionsEndpointFilter = new OptionsEndpointFilter(optionsProvider, objectConverter); AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); optionsEndpointFilter.setTrustResolver(trustResolver); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI(OptionsEndpointFilter.FILTER_URL); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain(); optionsEndpointFilter.doFilter(request, response, filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); }
Example #12
Source File: OptionsEndpointFilterTest.java From webauthn4j-spring-security with Apache License 2.0 | 6 votes |
@Test public void doFilter_with_error_test() throws IOException, ServletException { OptionsProvider optionsProvider = mock(OptionsProvider.class); doThrow(new RuntimeException()).when(optionsProvider).getAttestationOptions(any(), any(), any()); OptionsEndpointFilter optionsEndpointFilter = new OptionsEndpointFilter(optionsProvider, objectConverter); AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); optionsEndpointFilter.setTrustResolver(trustResolver); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI(OptionsEndpointFilter.FILTER_URL); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain(); optionsEndpointFilter.doFilter(request, response, filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.value()); }
Example #13
Source File: JWTFilterTest.java From e-commerce-microservice with Apache License 2.0 | 6 votes |
@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(JWTFilter.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); }
Example #14
Source File: HttpHeaderAuthenticationFilterTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testHttpHeaderAuthenticationFilterUserIdWithDomainName() throws Exception { modifyPropertySourceInEnvironment(getDefaultSecurityEnvironmentVariables()); try { MockHttpServletRequest request = getRequestWithHeaders("[email protected]", "testFirstName", "testLastName", "testEmail", "testRole", "Wed, 11 Mar 2015 10:24:09"); // Invalidate user session if exists. invalidateApplicationUser(request); httpHeaderAuthenticationFilter.init(new MockFilterConfig()); httpHeaderAuthenticationFilter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); validateHttpHeaderApplicationUser("[email protected]", "testFirstName", "testLastName", "testEmail", "testRole", "Wed, 11 Mar 2015 10:24:09", null, null); } finally { restorePropertySourceInEnvironment(); } }
Example #15
Source File: RequestCorrelationFilterTest.java From request-correlation-spring-cloud-starter with Apache License 2.0 | 6 votes |
@Test public void shouldUseExistingCorrelationId() throws IOException, ServletException { // given final String requestId = UUID.randomUUID().toString(); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); final MockFilterChain chain = new MockFilterChain(); request.addHeader(RequestCorrelationConsts.HEADER_NAME, requestId); // when instance.doFilter(request, response, chain); // then final Object requestCorrelation = request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME); assertNotNull(requestCorrelation); assertEquals(requestId, ((RequestCorrelation) requestCorrelation).getRequestId()); final String header = ((HttpServletRequest) chain.getRequest()).getHeader(RequestCorrelationConsts.HEADER_NAME); assertNotNull(header); assertEquals(requestId, header); }
Example #16
Source File: JWTFilterTest.java From Full-Stack-Development-with-JHipster with MIT License | 6 votes |
@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); }
Example #17
Source File: RequestCorrelationFilterTest.java From request-correlation-spring-cloud-starter with Apache License 2.0 | 6 votes |
@Test public void shouldUseCustomHeader() throws IOException, ServletException { // given final String headerName = "X-TraceId"; final String requestId = UUID.randomUUID().toString(); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); final MockFilterChain chain = new MockFilterChain(); request.addHeader(headerName, requestId); properties.setHeaderName(headerName); // when instance.doFilter(request, response, chain); // then final Object requestCorrelation = request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME); assertNotNull(requestCorrelation); assertEquals(requestId, ((RequestCorrelation) requestCorrelation).getRequestId()); final String header = ((HttpServletRequest) chain.getRequest()).getHeader(headerName); assertNotNull(header); assertEquals(requestId, header); }
Example #18
Source File: HttpHeaderAuthenticationFilterTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testHttpHeaderAuthenticationFilterMultipleRoles() throws Exception { modifyPropertySourceInEnvironment(getDefaultSecurityEnvironmentVariables()); try { MockHttpServletRequest request = getRequestWithHeaders(USER_ID, "testFirstName", "testLastName", "testEmail", "testRole1,testRole2", "Wed, 11 Mar 2015 10:24:09"); // Invalidate user session if exists. invalidateApplicationUser(request); httpHeaderAuthenticationFilter.init(new MockFilterConfig()); httpHeaderAuthenticationFilter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); Set<String> expectedRoles = new HashSet<>(); expectedRoles.add("testRole1"); expectedRoles.add("testRole2"); validateHttpHeaderApplicationUser(USER_ID, "testFirstName", "testLastName", "testEmail", expectedRoles, "Wed, 11 Mar 2015 10:24:09", null, null); } finally { restorePropertySourceInEnvironment(); } }
Example #19
Source File: JWTFilterTest.java From jhipster-online with Apache License 2.0 | 6 votes |
@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(JWTFilter.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); }
Example #20
Source File: JWTFilterTest.java From jhipster-online with Apache License 2.0 | 6 votes |
@Test public void testJWTFilterWrongScheme() 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(JWTFilter.AUTHORIZATION_HEADER, "Basic " + 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()).isNull(); }
Example #21
Source File: KatharsisFilterTest.java From katharsis-framework with Apache License 2.0 | 6 votes |
@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())); }
Example #22
Source File: HttpHeaderAuthenticationFilterTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testHttpHeaderAuthenticationFilterNoRoles() throws Exception { modifyPropertySourceInEnvironment(getDefaultSecurityEnvironmentVariables()); try { MockHttpServletRequest request = getRequestWithHeaders(USER_ID, "testFirstName", "testLastName", "testEmail", null, "Wed, 11 Mar 2015 10:24:09"); // Invalidate user session if exists. invalidateApplicationUser(request); httpHeaderAuthenticationFilter.init(new MockFilterConfig()); httpHeaderAuthenticationFilter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); validateHttpHeaderApplicationUser(USER_ID, "testFirstName", "testLastName", "testEmail", (String) null, "Wed, 11 Mar 2015 10:24:09", null, null); } finally { restorePropertySourceInEnvironment(); } }
Example #23
Source File: JWTFilterTest.java From ehcache3-samples with Apache License 2.0 | 6 votes |
@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(JWTFilter.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); }
Example #24
Source File: JWTFilterUnitTest.java From tutorials with MIT License | 6 votes |
@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(JWTFilter.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); }
Example #25
Source File: JWTFilterTest.java From jhipster-registry with Apache License 2.0 | 6 votes |
@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(JWTFilter.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); }
Example #26
Source File: FlashLoadingFilterTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldLoadExistingFlashFromSession() throws IOException, ServletException { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpSession session = new MockHttpSession(); FlashMessageService.Flash oldFlash = new FlashMessageService.Flash(); oldFlash.put("my_key", new FlashMessageModel("my other message", "warning")); session.putValue(FlashLoadingFilter.FLASH_SESSION_KEY, oldFlash); req.setSession(session); MockHttpServletResponse res = new MockHttpServletResponse(); FilterChain filterChain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) { flash = service.get("my_key"); } }; filter.doFilter(req, res, filterChain); assertThat(flash.toString(), is("my other message")); assertThat(flash.getFlashClass(), is("warning")); }
Example #27
Source File: FlashLoadingFilterTest.java From gocd with Apache License 2.0 | 6 votes |
@Test public void shouldClearThreadContext() throws IOException, ServletException { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); FilterChain filterChain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) { messageKey = service.add(new FlashMessageModel("my message", "error")); flash = service.get(messageKey); } }; filter.doFilter(req, res, filterChain); assertThat(flash.toString(), is("my message")); try { service.get(messageKey); fail("attempt to load flash message should fail, as no thread local is cleared out"); } catch (Exception e) { assertThat(e.getMessage(), is("No flash context found, this call should only be made within a request.")); } }
Example #28
Source File: HttpFilterBenchmarks.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Benchmark @Measurement(iterations = 5, time = 1) @Fork(3) public void filterWithoutSleuth(BenchmarkContext context) throws IOException, ServletException { MockHttpServletRequest request = builder().buildRequest(new MockServletContext()); MockHttpServletResponse response = new MockHttpServletResponse(); response.setContentType(MediaType.APPLICATION_JSON_VALUE); context.dummyFilter.doFilter(request, response, new MockFilterChain()); }
Example #29
Source File: CrnkFilterTest.java From crnk-framework with Apache License 2.0 | 5 votes |
@Test public void onSimpleResourceGetShouldReturnOneResource() throws Exception { MockFilterChain filterChain = new MockFilterChain(); MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setMethod("GET"); request.setContextPath(""); request.setServletPath("/api"); request.setPathInfo(null); request.setRequestURI("/api/tasks/1"); request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE); request.addHeader("Accept", "*/*"); MockHttpServletResponse response = new MockHttpServletResponse(); filter.doFilter(request, response, filterChain); String responseContent = response.getContentAsString(); log.debug("responseContent: {}", responseContent); assertNotNull(responseContent); assertJsonPartEquals("tasks", responseContent, "data.type"); assertJsonPartEquals("\"1\"", responseContent, "data.id"); assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data.attributes"); assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links"); assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links"); }
Example #30
Source File: HttpHeaderAuthenticationFilterTest.java From herd with Apache License 2.0 | 5 votes |
@Test public void testHttpHeaderAuthenticationFilterUserWithMultiRoleHeaderNameRegexMultiRoles() throws Exception { String testUserId = "testUser"; String userIdSuffix = "suffix"; String userIdWithSuffix = testUserId + "@" + userIdSuffix; modifyPropertySourceInEnvironment(getDefaultSecurityEnvironmentVariablesWithMultiHeaderRoles()); try { MockHttpServletRequest request = getRequestWithHeaders(testUserId, "testFirstName", "testLastName", "testEmail", "", "Wed, 11 Mar 2015 10:24:09"); request.addHeader("privtestrole1", "valid"); request.addHeader("privtestrole2", "valid"); request.addHeader("useridSuffix", userIdSuffix); // Invalidate user session if exists. invalidateApplicationUser(request); httpHeaderAuthenticationFilter.init(new MockFilterConfig()); httpHeaderAuthenticationFilter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); Set<String> expectedRoles = new HashSet<>(); expectedRoles.add("testrole1"); expectedRoles.add("testrole2"); validateHttpHeaderApplicationUser(userIdWithSuffix, "testFirstName", "testLastName", "testEmail", expectedRoles, "Wed, 11 Mar 2015 10:24:09", null, null); } finally { restorePropertySourceInEnvironment(); } }