Netty4ClientHttpRequest ignores query params
Before this commit, Netty4ClientHttpRequest ignored query parameters
(?foo=bar). This commit fixes that.
Issue: SPR-12779
(cherry picked from commit caee78a)
This commit is contained in:
committed by
Juergen Hoeller
parent
e2a55ced50
commit
e799554ab8
@@ -145,7 +145,7 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
|
||||
io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name());
|
||||
|
||||
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
|
||||
nettyMethod, this.uri.getRawPath(), this.body.buffer());
|
||||
nettyMethod, this.uri.toString(), this.body.buffer());
|
||||
|
||||
nettyRequest.headers().set(HttpHeaders.HOST, uri.getHost());
|
||||
nettyRequest.headers().set(HttpHeaders.CONNECTION, io.netty.handler.codec.http.HttpHeaders.Values.CLOSE);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,8 +23,6 @@ import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -36,12 +34,16 @@ import org.springframework.http.StreamingHttpOutputMessage;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public abstract class AbstractHttpRequestFactoryTestCase extends
|
||||
AbstractJettyServerTestCase {
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class AbstractHttpRequestFactoryTestCase extends AbstractJettyServerTestCase {
|
||||
|
||||
protected ClientHttpRequestFactory factory;
|
||||
|
||||
|
||||
@Before
|
||||
public final void createFactory() throws Exception {
|
||||
factory = createRequestFactory();
|
||||
@@ -60,6 +62,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends
|
||||
|
||||
protected abstract ClientHttpRequestFactory createRequestFactory();
|
||||
|
||||
|
||||
@Test
|
||||
public void status() throws Exception {
|
||||
URI uri = new URI(baseUrl + "/status/notfound");
|
||||
@@ -180,4 +183,18 @@ public abstract class AbstractHttpRequestFactoryTestCase extends
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParameters() throws Exception {
|
||||
URI uri = new URI(baseUrl + "/params?param1=value¶m2=value1¶m2=value2");
|
||||
ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET);
|
||||
|
||||
ClientHttpResponse response = request.execute();
|
||||
try {
|
||||
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
|
||||
}
|
||||
finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.client;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
import javax.servlet.GenericServlet;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
@@ -37,13 +38,16 @@ import org.junit.BeforeClass;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class AbstractJettyServerTestCase {
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class AbstractJettyServerTestCase {
|
||||
|
||||
protected static String baseUrl;
|
||||
|
||||
private static Server jettyServer;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void startJettyServer() throws Exception {
|
||||
int port = SocketUtils.findAvailableTcpPort();
|
||||
@@ -54,6 +58,7 @@ public class AbstractJettyServerTestCase {
|
||||
handler.setContextPath("/");
|
||||
|
||||
handler.addServlet(new ServletHolder(new EchoServlet()), "/echo");
|
||||
handler.addServlet(new ServletHolder(new ParameterServlet()), "/params");
|
||||
handler.addServlet(new ServletHolder(new StatusServlet(200)), "/status/ok");
|
||||
handler.addServlet(new ServletHolder(new StatusServlet(404)), "/status/notfound");
|
||||
handler.addServlet(new ServletHolder(new MethodServlet("DELETE")), "/methods/delete");
|
||||
@@ -75,6 +80,7 @@ public class AbstractJettyServerTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Servlet that sets a given status code.
|
||||
*/
|
||||
@@ -94,6 +100,7 @@ public class AbstractJettyServerTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class MethodServlet extends GenericServlet {
|
||||
|
||||
@@ -112,6 +119,7 @@ public class AbstractJettyServerTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class PostServlet extends MethodServlet {
|
||||
|
||||
@@ -136,6 +144,7 @@ public class AbstractJettyServerTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class EchoServlet extends HttpServlet {
|
||||
|
||||
@@ -158,4 +167,28 @@ public class AbstractJettyServerTestCase {
|
||||
StreamUtils.copy(request.getInputStream(), response.getOutputStream());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class ParameterServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
Map<String, String[]> parameters = req.getParameterMap();
|
||||
assertEquals(2, parameters.size());
|
||||
|
||||
String[] values = parameters.get("param1");
|
||||
assertEquals(1, values.length);
|
||||
assertEquals("value", values[0]);
|
||||
|
||||
values = parameters.get("param2");
|
||||
assertEquals(2, values.length);
|
||||
assertEquals("value1", values[0]);
|
||||
assertEquals("value2", values[1]);
|
||||
|
||||
resp.setStatus(200);
|
||||
resp.setContentLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user