Clean up SavedRequestAwareWrapper and related test

This commit is contained in:
Jonas Bamberger 2023-06-30 11:49:32 +02:00 committed by Josh Cummings
parent 07f737b989
commit 0d4e3f939a
2 changed files with 12 additions and 22 deletions

View File

@ -58,10 +58,7 @@ class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
protected static final TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT");
/** The default Locale if none are specified. */
protected static Locale defaultLocale = Locale.getDefault();
protected SavedRequest savedRequest = null;
protected SavedRequest savedRequest;
/**
* The set of SimpleDateFormat formats to use in getDateHeader(). Notice that because
@ -102,14 +99,12 @@ class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
}
@Override
@SuppressWarnings("unchecked")
public Enumeration getHeaderNames() {
public Enumeration<String> getHeaderNames() {
return new Enumerator<>(this.savedRequest.getHeaderNames());
}
@Override
@SuppressWarnings("unchecked")
public Enumeration getHeaders(String name) {
public Enumeration<String> getHeaders(String name) {
return new Enumerator<>(this.savedRequest.getHeaderValues(name));
}
@ -126,8 +121,7 @@ class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
}
@Override
@SuppressWarnings("unchecked")
public Enumeration getLocales() {
public Enumeration<Locale> getLocales() {
List<Locale> locales = this.savedRequest.getLocales();
if (locales.isEmpty()) {
// Fall back to default locale
@ -171,8 +165,7 @@ class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
}
@Override
@SuppressWarnings("unchecked")
public Map getParameterMap() {
public Map<String, String[]> getParameterMap() {
Set<String> names = getCombinedParameterNames();
Map<String, String[]> parameterMap = new HashMap<>(names.size());
for (String name : names) {
@ -181,7 +174,6 @@ class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
return parameterMap;
}
@SuppressWarnings("unchecked")
private Set<String> getCombinedParameterNames() {
Set<String> names = new HashSet<>();
names.addAll(super.getParameterMap().keySet());
@ -190,9 +182,8 @@ class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
}
@Override
@SuppressWarnings("unchecked")
public Enumeration getParameterNames() {
return new Enumerator(getCombinedParameterNames());
public Enumeration<String> getParameterNames() {
return new Enumerator<>(getCombinedParameterNames());
}
@Override

View File

@ -43,22 +43,21 @@ public class SavedRequestAwareWrapperTests {
@Test
public void savedRequestCookiesAreIgnored() {
MockHttpServletRequest newRequest = new MockHttpServletRequest();
newRequest.setCookies(new Cookie[] { new Cookie("cookie", "fromnew") });
newRequest.setCookies(new Cookie("cookie", "fromnew"));
MockHttpServletRequest savedRequest = new MockHttpServletRequest();
savedRequest.setCookies(new Cookie[] { new Cookie("cookie", "fromsaved") });
savedRequest.setCookies(new Cookie("cookie", "fromsaved"));
SavedRequestAwareWrapper wrapper = createWrapper(savedRequest, newRequest);
assertThat(wrapper.getCookies()).hasSize(1);
assertThat(wrapper.getCookies()[0].getValue()).isEqualTo("fromnew");
}
@Test
@SuppressWarnings("unchecked")
public void savedRequesthHeaderIsReturnedIfSavedRequestIsSet() {
MockHttpServletRequest savedRequest = new MockHttpServletRequest();
savedRequest.addHeader("header", "savedheader");
SavedRequestAwareWrapper wrapper = createWrapper(savedRequest, new MockHttpServletRequest());
assertThat(wrapper.getHeader("nonexistent")).isNull();
Enumeration headers = wrapper.getHeaders("nonexistent");
Enumeration<String> headers = wrapper.getHeaders("nonexistent");
assertThat(headers.hasMoreElements()).isFalse();
assertThat(wrapper.getHeader("Header")).isEqualTo("savedheader");
headers = wrapper.getHeaders("heaDer");
@ -98,7 +97,7 @@ public class SavedRequestAwareWrapperTests {
SavedRequestAwareWrapper wrapper = createWrapper(savedRequest, wrappedRequest);
assertThat(wrapper.getParameterValues("action")).hasSize(1);
assertThat(wrapper.getParameterMap()).hasSize(1);
assertThat(((String[]) wrapper.getParameterMap().get("action"))).hasSize(1);
assertThat(wrapper.getParameterMap().get("action")).hasSize(1);
}
@Test
@ -128,7 +127,7 @@ public class SavedRequestAwareWrapperTests {
wrappedRequest.setParameter("action", "bar");
assertThat(wrapper.getParameterValues("action")).isEqualTo(new Object[] { "bar", "foo" });
// Check map is consistent
String[] valuesFromMap = (String[]) wrapper.getParameterMap().get("action");
String[] valuesFromMap = wrapper.getParameterMap().get("action");
assertThat(valuesFromMap).hasSize(2);
assertThat(valuesFromMap[0]).isEqualTo("bar");
}