Skip to content

Commit

Permalink
Fix #12578 pretty print getParameterMap
Browse files Browse the repository at this point in the history
Fix #12578 pretty print getParameterMap
Added test for parameters
  • Loading branch information
gregw committed Nov 26, 2024
1 parent 4d832d0 commit 6f91098
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
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();
}
};
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]
"""));
}
}

0 comments on commit 6f91098

Please sign in to comment.