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

Fix #12578 pretty print getParameterMap #12585

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jetty.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -329,7 +330,31 @@ public Iterator<Field> iterator()
*/
public Map<String, String[]> toStringArrayMap()
{
Map<String, String[]> result = new LinkedHashMap<>();
Map<String, String[]> result = new LinkedHashMap<>()
{
@Override
public String toString()
{
if (fields.isEmpty())
return "{}";

StringBuilder sb = new StringBuilder();
sb.append('{');
for (Iterator<Map.Entry<String, String[]>> i = entrySet().iterator(); i.hasNext();)
{
Map.Entry<String, String[]> e = i.next();
String key = e.getKey();
String[] value = e.getValue();
sb.append(key);
sb.append('=');
sb.append(Arrays.asList(value));
if (i.hasNext())
sb.append(',');
}
sb.append('}');
return sb.toString();
gregw marked this conversation as resolved.
Show resolved Hide resolved
}
};
fields.forEach((k, f) -> result.put(f.getName(), f.getValues().toArray(new String[0])));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
Expand Down Expand Up @@ -626,4 +627,49 @@ protected void service(HttpServletRequest request, HttpServletResponse resp) thr
assertThat(date, containsString(":"));
}
}

@Test
public void testParameters() throws Exception
{
final AtomicReference<String> parameterMap = new AtomicReference<>();

startServer(new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse resp) throws IOException
{
parameterMap.set(request.getParameterMap().toString());
PrintWriter out = resp.getWriter();
out.println(request.getParameter("a"));
out.println(request.getParameterValues("a")[1]);
out.println(request.getParameterValues("a")[2]);
out.println(Arrays.asList(request.getParameterValues("b")));
out.println(Arrays.asList(request.getParameterValues("c")));
out.println(Arrays.asList(request.getParameterValues("d")));

}
});

String rawResponse = _connector.getResponse(
"""
POST /test/parameters?a=1&a=2&b=one&c= HTTP/1.1\r
Host: localhost\r
Connection: close\r
Content-Type: application/x-www-form-urlencoded\r
Content-Length: 23\r
\r
a=3&b=two&b=three&d=xyz\r
""");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(parameterMap.get(), is("{a=[1, 2, 3],b=[one, two, three],c=[],d=[xyz]}"));
assertThat(response.getContent().replaceAll("\r\n", "\n"), is("""
1
2
3
[one, two, three]
[]
[xyz]
"""));
}
}