Java Code Examples for org.springframework.mock.web.MockFilterChain#doFilter()

The following examples show how to use org.springframework.mock.web.MockFilterChain#doFilter() . 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 vote down vote up
@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: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Test
void testExceptionCapturingShouldContainUserInformationRecordedOnTheTransaction() throws IOException, ServletException {
    filterChain = new MockFilterChain(new HttpServlet() {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
            tracer.currentTransaction().setUser("id", "email", "username");
            tracer.getActive().captureException(new RuntimeException("Test exception capturing"));
        }
    });

    filterChain.doFilter(new MockHttpServletRequest("GET", "/foo"), new MockHttpServletResponse());
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getErrors()).hasSize(1);
    assertThat(reporter.getFirstError().getContext().getUser().getId()).isEqualTo("id");
    assertThat(reporter.getFirstError().getContext().getUser().getEmail()).isEqualTo("email");
    assertThat(reporter.getFirstError().getContext().getUser().getUsername()).isEqualTo("username");
}
 
Example 3
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testIgnoreUrlEndWith() throws IOException, ServletException {
    filterChain = new MockFilterChain(new HttpServlet() {
    });
    when(webConfiguration.getIgnoreUrls()).thenReturn(Collections.singletonList(WildcardMatcher.valueOf("*.js")));
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServletPath("/resources");
    request.setPathInfo("test.js");
    filterChain.doFilter(request, new MockHttpServletResponse());
    verify(webConfiguration, times(1)).getIgnoreUrls();
    assertThat(reporter.getTransactions()).hasSize(0);
}
 
Example 5
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testDoNotOverrideUsername() throws IOException, ServletException {
    filterChain = new MockFilterChain(new HttpServlet() {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            tracer.currentTransaction().setUser("id", "email", "username");
        }
    });

    filterChain.doFilter(new MockHttpServletRequest("GET", "/foo"), new MockHttpServletResponse());
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getFirstTransaction().getContext().getUser().getId()).isEqualTo("id");
    assertThat(reporter.getFirstTransaction().getContext().getUser().getEmail()).isEqualTo("email");
    assertThat(reporter.getFirstTransaction().getContext().getUser().getUsername()).isEqualTo("username");
}
 
Example 6
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testExceptionCapturingShouldContainContextInformation() throws IOException, ServletException {
    filterChain = new MockFilterChain(new HttpServlet() {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
            tracer.getActive().captureException(new RuntimeException("Test exception capturing"));
        }
    });

    filterChain.doFilter(new MockHttpServletRequest("GET", "/foo"), new MockHttpServletResponse());
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getErrors()).hasSize(1);
    assertThat(reporter.getFirstError().getContext().getRequest().getUrl().getPathname()).isEqualTo("/foo");
    assertThat(reporter.getFirstError().getTraceContext().isChildOf(reporter.getFirstTransaction().getTraceContext())).isTrue();
}
 
Example 7
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void exceptionCapturingShouldNotContainUserInformationRecordedOnTheTransactionAfterTheErrorWasCaptured() throws IOException, ServletException {
    filterChain = new MockFilterChain(new HttpServlet() {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
            tracer.getActive().captureException(new RuntimeException("Test exception capturing"));
            tracer.currentTransaction().setUser("id", "email", "username");
        }
    });

    filterChain.doFilter(new MockHttpServletRequest("GET", "/foo"), new MockHttpServletResponse());
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getErrors()).hasSize(1);
    assertThat(reporter.getFirstError().getContext().getUser().hasContent()).isFalse();
}
 
Example 8
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void captureTransactionNameBasedOnServlet() throws IOException, ServletException {
    filterChain = new MockFilterChain(new TestServlet());
    filterChain.doFilter(new MockHttpServletRequest("GET", "/foo"), new MockHttpServletResponse());
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getFirstTransaction().getNameAsString()).isEqualTo("ApmFilterTest$TestServlet#doGet");
}
 
Example 9
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void captureTransactionNameManuallySetInFilter() throws IOException, ServletException {
    filterChain = new MockFilterChain(new TestServlet(), new TransactionNamingFilter("CustomName"));
    filterChain.doFilter(new MockHttpServletRequest("GET", "/foo"), new MockHttpServletResponse());
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getFirstTransaction().getNameAsString()).isEqualTo("CustomName");
}
 
Example 10
Source File: TestRequestBodyCapturing.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private void executeRequest(MockFilterChain filterChain, byte[] bytes, String contentType) throws IOException, ServletException {
    try {
        filterChain.doFilter(createMockRequest(bytes, contentType), new MockHttpServletResponse());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: MockMvc.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Perform a request and return a type that allows chaining further
 * actions, such as asserting expectations, on the result.
 *
 * @param requestBuilder used to prepare the request to execute;
 * see static factory methods in
 * {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
 *
 * @return an instance of {@link ResultActions}; never {@code null}
 *
 * @see org.springframework.test.web.servlet.request.MockMvcRequestBuilders
 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
 */
public ResultActions perform(RequestBuilder requestBuilder) throws Exception {

	if (this.defaultRequestBuilder != null) {
		if (requestBuilder instanceof Mergeable) {
			requestBuilder = (RequestBuilder) ((Mergeable) requestBuilder).merge(this.defaultRequestBuilder);
		}
	}

	MockHttpServletRequest request = requestBuilder.buildRequest(this.servletContext);
	MockHttpServletResponse response = new MockHttpServletResponse();

	if (requestBuilder instanceof SmartRequestBuilder) {
		request = ((SmartRequestBuilder) requestBuilder).postProcessRequest(request);
	}

	final MvcResult mvcResult = new DefaultMvcResult(request, response);
	request.setAttribute(MVC_RESULT_ATTRIBUTE, mvcResult);

	RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));

	MockFilterChain filterChain = new MockFilterChain(this.servlet, this.filters);
	filterChain.doFilter(request, response);

	if (DispatcherType.ASYNC.equals(request.getDispatcherType()) &&
			request.getAsyncContext() != null & !request.isAsyncStarted()) {

		request.getAsyncContext().complete();
	}

	applyDefaultResultActions(mvcResult);

	RequestContextHolder.setRequestAttributes(previousAttributes);

	return new ResultActions() {

		@Override
		public ResultActions andExpect(ResultMatcher matcher) throws Exception {
			matcher.match(mvcResult);
			return this;
		}

		@Override
		public ResultActions andDo(ResultHandler handler) throws Exception {
			handler.handle(mvcResult);
			return this;
		}

		@Override
		public MvcResult andReturn() {
			return mvcResult;
		}
	};
}