Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return content-type from saved request #13440

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Clean up SavedRequestAwareWrapper and related test
  • Loading branch information
Jonas Bamberger committed Jun 30, 2023
commit 0ed0a6c18dc502bb9eeed2763b3f6c7847ac7905
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,14 +99,12 @@ public String getHeader(String name) {
}

@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));
}

Expand All @@ -126,8 +121,7 @@ public Locale getLocale() {
}

@Override
@SuppressWarnings("unchecked")
public Enumeration getLocales() {
public Enumeration<Locale> getLocales() {
List<Locale> locales = this.savedRequest.getLocales();
if (locales.isEmpty()) {
// Fall back to default locale
Expand Down Expand Up @@ -171,8 +165,7 @@ public String getParameter(String name) {
}

@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) {
Expand All @@ -181,7 +174,6 @@ public Map getParameterMap() {
return parameterMap;
}

@SuppressWarnings("unchecked")
private Set<String> getCombinedParameterNames() {
Set<String> names = new HashSet<>();
names.addAll(super.getParameterMap().keySet());
Expand All @@ -190,9 +182,8 @@ private Set<String> getCombinedParameterNames() {
}

@Override
@SuppressWarnings("unchecked")
public Enumeration getParameterNames() {
return new Enumerator(getCombinedParameterNames());
public Enumeration<String> getParameterNames() {
return new Enumerator<>(getCombinedParameterNames());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ private SavedRequestAwareWrapper createWrapper(MockHttpServletRequest requestToS
@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");
Expand Down Expand Up @@ -98,7 +97,7 @@ public void savedRequestDoesntCreateDuplicateParams() {
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
Expand Down Expand Up @@ -128,7 +127,7 @@ public void getParameterValuesReturnsCombinedSavedAndWrappedRequestValues() {
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");
}
Expand Down