提交 0f421f9f 编写于 作者: S Sam Brannen

Support default character encoding for response in MockMvc

Commit e4b9b1fa introduced support for setting the default character
encoding in MockHttpServletResponse.

This commit introduces support for configuring the default character
encoding in the underlying MockHttpServletResponse used in MockMvc.

Closes gh-27230
上级 41fa1991
......@@ -16,6 +16,7 @@
package org.springframework.test.web.servlet;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
......@@ -78,6 +79,9 @@ public final class MockMvc {
@Nullable
private RequestBuilder defaultRequestBuilder;
@Nullable
private Charset defaultResponseCharacterEncoding;
private List<ResultMatcher> defaultResultMatchers = new ArrayList<>();
private List<ResultHandler> defaultResultHandlers = new ArrayList<>();
......@@ -106,6 +110,14 @@ public final class MockMvc {
this.defaultRequestBuilder = requestBuilder;
}
/**
* The default character encoding to be applied to every response.
* @see org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder#defaultResponseCharacterEncoding(Charset)
*/
void setDefaultResponseCharacterEncoding(@Nullable Charset defaultResponseCharacterEncoding) {
this.defaultResponseCharacterEncoding = defaultResponseCharacterEncoding;
}
/**
* Expectations to assert after every performed request.
* @see org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder#alwaysExpect(ResultMatcher)
......@@ -169,6 +181,10 @@ public final class MockMvc {
servletResponse = mockResponse;
}
if (this.defaultResponseCharacterEncoding != null) {
mockResponse.setDefaultCharacterEncoding(this.defaultResponseCharacterEncoding.name());
}
if (requestBuilder instanceof SmartRequestBuilder) {
request = ((SmartRequestBuilder) requestBuilder).postProcessRequest(request);
}
......
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2021 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.
......@@ -16,6 +16,7 @@
package org.springframework.test.web.servlet;
import java.nio.charset.Charset;
import java.util.List;
import javax.servlet.Filter;
......@@ -37,10 +38,28 @@ import org.springframework.web.context.WebApplicationContext;
* @author Rossen Stoyanchev
* @author Rob Winch
* @author Stephane Nicoll
* @author Sam Brannen
* @since 3.2
*/
public abstract class MockMvcBuilderSupport {
/**
* Delegates to {@link #createMockMvc(Filter[], MockServletConfig, WebApplicationContext, RequestBuilder, List, List, List)}
* for creation of the {@link MockMvc} instance and configures that instance
* with the supplied {@code defaultResponseCharacterEncoding}.
* @since 5.3.10
*/
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig,
WebApplicationContext webAppContext, @Nullable RequestBuilder defaultRequestBuilder,
@Nullable Charset defaultResponseCharacterEncoding,
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers,
@Nullable List<DispatcherServletCustomizer> dispatcherServletCustomizers) {
MockMvc mockMvc = createMockMvc(filters, servletConfig, webAppContext, defaultRequestBuilder, globalResultMatchers, globalResultHandlers, dispatcherServletCustomizers);
mockMvc.setDefaultResponseCharacterEncoding(defaultResponseCharacterEncoding);
return mockMvc;
}
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig,
WebApplicationContext webAppContext, @Nullable RequestBuilder defaultRequestBuilder,
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers,
......
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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.
......@@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.setup;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
......@@ -48,6 +49,7 @@ import org.springframework.web.context.WebApplicationContext;
*
* @author Rossen Stoyanchev
* @author Stephane Nicoll
* @author Sam Brannen
* @since 4.0
* @param <B> a self reference to the builder type
*/
......@@ -59,6 +61,9 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
@Nullable
private RequestBuilder defaultRequestBuilder;
@Nullable
private Charset defaultResponseCharacterEncoding;
private final List<ResultMatcher> globalResultMatchers = new ArrayList<>();
private final List<ResultHandler> globalResultHandlers = new ArrayList<>();
......@@ -95,6 +100,17 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
return self();
}
/**
* Define the default character encoding to be applied to every response.
* @param defaultResponseCharacterEncoding the default response character encoding
* @since 5.3.10
*/
@Override
public final <T extends B> T defaultResponseCharacterEncoding(Charset defaultResponseCharacterEncoding) {
this.defaultResponseCharacterEncoding = defaultResponseCharacterEncoding;
return self();
}
@Override
public final <T extends B> T alwaysExpect(ResultMatcher resultMatcher) {
this.globalResultMatchers.add(resultMatcher);
......@@ -157,7 +173,8 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
Filter[] filterArray = this.filters.toArray(new Filter[0]);
return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder,
this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers);
this.defaultResponseCharacterEncoding, this.globalResultMatchers, this.globalResultHandlers,
this.dispatcherServletCustomizers);
}
/**
......
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
......@@ -16,6 +16,8 @@
package org.springframework.test.web.servlet.setup;
import java.nio.charset.Charset;
import javax.servlet.Filter;
import org.springframework.test.web.servlet.DispatcherServletCustomizer;
......@@ -28,6 +30,7 @@ import org.springframework.test.web.servlet.ResultMatcher;
* Defines common methods for building a {@code MockMvc}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.1
* @param <B> a self reference to the builder type
*/
......@@ -81,6 +84,18 @@ public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder
*/
<T extends B> T defaultRequest(RequestBuilder requestBuilder);
/**
* Define the default character encoding to be applied to every response.
* <p>The default implementation of this method ignores the supplied value.
* Concrete implementations are therefore encouraged to override this method.
* @param defaultResponseCharacterEncoding the default response character encoding
* @since 5.3.10
*/
@SuppressWarnings("unchecked")
default <T extends B> T defaultResponseCharacterEncoding(Charset defaultResponseCharacterEncoding) {
return (T) this;
}
/**
* Define a global expectation that should <em>always</em> be applied to
* every response. For example, status code 200 (OK), content type
......@@ -110,6 +125,7 @@ public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder
* A more advanced variant of {@link #dispatchOptions(boolean)} that allows
* customizing any {@link org.springframework.web.servlet.DispatcherServlet}
* property.
* @since 5.3
*/
<T extends B> T addDispatcherServletCustomizer(DispatcherServletCustomizer customizer);
......
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
......@@ -16,8 +16,6 @@
package org.springframework.test.web.servlet.samples.standalone;
import javax.validation.constraints.NotNull;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
......@@ -25,6 +23,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
......@@ -42,11 +42,14 @@ class ResponseBodyTests {
@Test
void json() throws Exception {
standaloneSetup(new PersonController()).build()
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
standaloneSetup(new PersonController()).defaultResponseCharacterEncoding(UTF_8).build()
// We use a name containing an umlaut to test UTF-8 encoding for the request and the response.
.perform(get("/person/Jürgen").characterEncoding(UTF_8.name()).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.name").value("Lee"))
.andExpect(content().encoding(UTF_8.name()))
.andExpect(content().string(containsString("Jürgen")))
.andExpect(jsonPath("$.name").value("Jürgen"))
.andExpect(jsonPath("$.age").value(42))
.andExpect(jsonPath("$.age").value(42.0f))
.andExpect(jsonPath("$.age").value(equalTo(42)))
......@@ -70,7 +73,6 @@ class ResponseBodyTests {
@SuppressWarnings("unused")
private static class Person {
@NotNull
private final String name;
private int age;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册