<commit_before>/*
 * Copyright 2012-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.web.servlet.error;

import java.time.Clock;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.handler.DispatcherServletWebRequest;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link ErrorMvcAutoConfiguration}.
 *
 * @author <NAME>
 * @author <NAME>
 */
@ExtendWith(OutputCaptureExtension.class)
class ErrorMvcAutoConfigurationTests {

	private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner().withConfiguration(
			AutoConfigurations.of(DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class));

	@Test
	void renderContainsViewWithExceptionDetails() {
		this.contextRunner.run((context) -> {
			View errorView = context.getBean("error", View.class);
			ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
			DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
					false);
			errorView.render(errorAttributes.getErrorAttributes(webRequest, withAllOptions()), webRequest.getRequest(),
					webRequest.getResponse());
			assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
			String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
			assertThat(responseString).contains(
					"<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>")
					.contains("<div>Exception message</div>")
					.contains("<div style='white-space:pre-wrap;'>java.lang.IllegalStateException");
		});
	}

	@Test
	void renderCanUseJavaTimeTypeAsTimestamp() throws Exception { // gh-23256
		this.contextRunner.run((context) -> {
			View errorView = context.getBean("error", View.class);
			ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
			DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
					false);
			Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, true);
			attributes.put("timestamp", Clock.systemUTC().instant());
			errorView.render(attributes, webRequest.getRequest(), webRequest.getResponse());
			assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
			String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
			assertThat(responseString).contains("This application has no explicit mapping for /error");
		});
	}

	@Test
	void renderWhenAlreadyCommittedLogsMessage(CapturedOutput output) {
		this.contextRunner.run((context) -> {
			View errorView = context.getBean("error", View.class);<commit_msg>Fix deprecation warning<commit_after>/*
 * Copyright 2012-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.web.servlet.error;

import java.time.Clock;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.handler.DispatcherServletWebRequest;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link ErrorMvcAutoConfiguration}.
 *
 * @author <NAME>
 * @author <NAME>
 */
@ExtendWith(OutputCaptureExtension.class)
class ErrorMvcAutoConfigurationTests {

	private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner().withConfiguration(
			AutoConfigurations.of(DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class));

	@Test
	void renderContainsViewWithExceptionDetails() {
		this.contextRunner.run((context) -> {
			View errorView = context.getBean("error", View.class);
			ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
			DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
					false);
			errorView.render(errorAttributes.getErrorAttributes(webRequest, withAllOptions()), webRequest.getRequest(),
					webRequest.getResponse());
			assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
			String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
			assertThat(responseString).contains(
					"<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>")
					.contains("<div>Exception message</div>")
					.contains("<div style='white-space:pre-wrap;'>java.lang.IllegalStateException");
		});
	}

	@Test
	void renderCanUseJavaTimeTypeAsTimestamp() throws Exception { // gh-23256
		this.contextRunner.run((context) -> {
			View errorView = context.getBean("error", View.class);
			ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
			DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
					false);
			Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, withAllOptions());
			attributes.put("timestamp", Clock.systemUTC().instant());
			errorView.render(attributes, webRequest.getRequest(), webRequest.getResponse());
			assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
			String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
			assertThat(responseString).contains("This application has no explicit mapping for /error");
		});
	}

	@Test
	void renderWhenAlreadyCommittedLogsMessage(CapturedOutput output) {
		this.contextRunner.run((context) -> {
			View errorView = context.getBean("error", View.class);
{'fraction_non_alphanumeric': 0.10756972111553785, 'fraction_numerical': 0.00445277712678697, 'mean_word_length': 6.118432026688907, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1925699', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2191, 'n_words': 487}