diff --git a/zuul-core/src/main/java/com/netflix/zuul/util/HTTPRequestUtils.java b/zuul-core/src/main/java/com/netflix/zuul/util/HTTPRequestUtils.java index 8fb2d567c1..6ec6b95924 100755 --- a/zuul-core/src/main/java/com/netflix/zuul/util/HTTPRequestUtils.java +++ b/zuul-core/src/main/java/com/netflix/zuul/util/HTTPRequestUtils.java @@ -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; @@ -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; @@ -163,7 +168,7 @@ public Map> 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); @@ -184,6 +189,24 @@ public Map> getQueryParams() { valueList.add(value); } + else if (i == -1) + { + String name=s; + String value=""; + try { + name = URLDecoder.decode(name, "UTF-8"); + } catch (Exception e) { + } + + List valueList = qp.get(name); + if (valueList == null) { + valueList = new LinkedList(); + qp.put(name, valueList); + } + + valueList.add(value); + + } } RequestContext.getCurrentContext().setRequestQueryParams(qp); @@ -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")); @@ -236,6 +269,36 @@ public void detectsNonGzip() { public void detectsGzipAmongOtherEncodings() { assertTrue(HTTPRequestUtils.getInstance().isGzipped("gzip, deflate")); } + + @Test + public void testGetQueryParams() { + Map> qp; + LinkedList blankValue = new LinkedList(); + 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")); + } } }