Skip to content

Commit

Permalink
Fixed Issue 141 - Request Parameter with empty value being stripped
Browse files Browse the repository at this point in the history
Fixed an issue that caused request parameters with no value (e.g. ?wsdl
or ?wsdl=) to not be included in the result of getQueryParams()
  • Loading branch information
kcbaltz committed Aug 5, 2015
1 parent 85ae0cc commit d2e4bc8
Showing 1 changed file with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package com.netflix.zuul.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import java.net.URLDecoder;
import java.util.ArrayList;
Expand All @@ -30,7 +32,10 @@

import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.netflix.zuul.context.RequestContext;

Expand Down Expand Up @@ -163,7 +168,7 @@ public Map<String, List<String>> getQueryParams() {
while (st.hasMoreTokens()) {
String s = st.nextToken();
i = s.indexOf("=");
if (i > 0 && s.length() > i + 1) {
if (i > 0 && s.length() >= i + 1) {
String name = s.substring(0, i);
String value = s.substring(i + 1);

Expand All @@ -184,6 +189,24 @@ public Map<String, List<String>> getQueryParams() {

valueList.add(value);
}
else if (i == -1)
{
String name=s;
String value="";
try {
name = URLDecoder.decode(name, "UTF-8");
} catch (Exception e) {
}

List<String> valueList = qp.get(name);
if (valueList == null) {
valueList = new LinkedList<String>();
qp.put(name, valueList);
}

valueList.add(value);

}
}

RequestContext.getCurrentContext().setRequestQueryParams(qp);
Expand Down Expand Up @@ -222,6 +245,16 @@ public boolean isGzipped(String contentEncoding) {

public static class UnitTest {

@Mock
private RequestContext mockContext;
@Mock
private HttpServletRequest request;

@Before
public void before() {
MockitoAnnotations.initMocks(this);
}

@Test
public void detectsGzip() {
assertTrue(HTTPRequestUtils.getInstance().isGzipped("gzip"));
Expand All @@ -236,6 +269,36 @@ public void detectsNonGzip() {
public void detectsGzipAmongOtherEncodings() {
assertTrue(HTTPRequestUtils.getInstance().isGzipped("gzip, deflate"));
}

@Test
public void testGetQueryParams() {
Map<String, List<String>> qp;
LinkedList<String> blankValue = new LinkedList<String>();
blankValue.add("");

RequestContext.testSetCurrentContext(mockContext);
when(mockContext.getRequestQueryParams()).thenReturn(null);
when(mockContext.getRequest()).thenReturn(request);
when(request.getQueryString()).thenReturn("wsdl");

qp = HTTPRequestUtils.getInstance().getQueryParams();
assertEquals(blankValue, qp.get("wsdl"));

when(request.getQueryString()).thenReturn("wsdl=");

qp = HTTPRequestUtils.getInstance().getQueryParams();
assertEquals(blankValue, qp.get("wsdl"));

when(request.getQueryString()).thenReturn("a=123&b=234&b=345&c&d=");

qp = HTTPRequestUtils.getInstance().getQueryParams();
assertEquals("123", qp.get("a").get(0));
// Not sure that order is supposed to be guaranteed here
assertEquals("234", qp.get("b").get(0));
assertEquals("345", qp.get("b").get(1));
assertEquals(blankValue, qp.get("c"));
assertEquals(blankValue, qp.get("d"));
}
}

}

0 comments on commit d2e4bc8

Please sign in to comment.