405044 - Query parameters lost for non GET or POST.

This commit is contained in:
Simone Bordet 2013-04-05 23:20:04 +02:00
parent 01c2322615
commit 120b8c9839
2 changed files with 42 additions and 18 deletions

View File

@ -191,25 +191,17 @@ public class HttpConnection extends AbstractConnection implements Connection
params.append("&"); params.append("&");
} }
// Behave as a GET, adding the params to the path, if it's a POST with some content // POST with no content, send parameters as body
if (method == HttpMethod.POST && request.getContent() != null) if (method == HttpMethod.POST && request.getContent() == null)
method = HttpMethod.GET;
switch (method)
{ {
case GET: request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString());
{ request.content(new StringContentProvider(params.toString()));
path += "?"; }
path += params.toString(); else
request.path(path); {
break; path += "?";
} path += params.toString();
case POST: request.path(path);
{
request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString());
request.content(new StringContentProvider(params.toString()));
break;
}
} }
} }

View File

@ -254,6 +254,38 @@ public class HttpClientTest extends AbstractHttpClientServerTest
Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8")); Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8"));
} }
@Test
public void test_PUT_WithParameters() throws Exception
{
final String paramName = "a";
final String paramValue = "\u20AC";
start(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
String value = request.getParameter(paramName);
if (paramValue.equals(value))
{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getOutputStream().print(value);
}
}
});
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort() + "/path?" + paramName + "=" + paramValue);
ContentResponse response = client.newRequest(uri)
.method(HttpMethod.PUT)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8"));
}
@Test @Test
public void test_POST_WithParameters_WithContent() throws Exception public void test_POST_WithParameters_WithContent() throws Exception
{ {