[java-support] 10/15: Unit tests and bug fixes for remoted requests.
Scott Cantor
cantor.2 at osu.edu
Mon Apr 18 20:50:06 UTC 2022
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch dev/JSPT-111
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=df5278a99e0dc2f670ffdf9bfdc04a33aeb47ef1
commit df5278a99e0dc2f670ffdf9bfdc04a33aeb47ef1
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Sep 14 14:37:34 2021 -0400
Unit tests and bug fixes for remoted requests.
---
.../support/ddf/RemotedHttpServletRequest.java | 18 ++-
.../support/ddf/RemotedHttpServletRequestTest.java | 168 +++++++++++++++++++++
2 files changed, 181 insertions(+), 5 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequest.java b/src/main/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequest.java
index ee779e6..ac9fd9a 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequest.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequest.java
@@ -81,7 +81,7 @@ public class RemotedHttpServletRequest implements HttpServletRequest {
@Nonnull private final DDF obj;
/** Cookie array. */
- @Nullable @NonnullElements private ArrayList<Cookie> cookies;
+ @Nullable @NonnullElements private List<Cookie> cookies;
/** Parameter map. */
@Nullable private Map<String, String[]> parameters;
@@ -165,7 +165,7 @@ public class RemotedHttpServletRequest implements HttpServletRequest {
/** {@inheritDoc} */
public Map<String, String[]> getParameterMap() {
- if (parameters != null) {
+ if (parameters == null) {
parameters = new HashMap<>();
final Multimap<String,String> multimap = ArrayListMultimap.create();
final String qs = getQueryString();
@@ -285,8 +285,7 @@ public class RemotedHttpServletRequest implements HttpServletRequest {
/** {@inheritDoc} */
public int getLocalPort() {
- // TODO: If we need this, should be configurable via the c'tor.
- throw new UnsupportedOperationException();
+ return getServerPort();
}
/** {@inheritDoc} */
@@ -349,9 +348,17 @@ public class RemotedHttpServletRequest implements HttpServletRequest {
cookies.add(new Cookie(name, nvpair[1]));
}
}
+ } else {
+ cookies = Collections.emptyList();
}
+ } else {
+ cookies = Collections.emptyList();
}
}
+
+ if (cookies.isEmpty()) {
+ return null;
+ }
return cookies.toArray(new Cookie[cookies.size()]);
}
@@ -456,7 +463,8 @@ public class RemotedHttpServletRequest implements HttpServletRequest {
/** {@inheritDoc} */
public StringBuffer getRequestURL() {
- return new StringBuffer(obj.getmember("url").string());
+ final String url = obj.getmember("url").string();
+ return new StringBuffer(url != null ? url : "");
}
/** {@inheritDoc} */
diff --git a/src/test/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequestTest.java b/src/test/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequestTest.java
new file mode 100644
index 0000000..c0c03c1
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/ddf/RemotedHttpServletRequestTest.java
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.utilities.java.support.ddf;
+
+import static org.testng.Assert.*;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.servlet.http.Cookie;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import org.testng.reporters.Files;
+
+/**
+ * Unit test for {@link RemotedHttpServletRequest}.
+ */
+public class RemotedHttpServletRequestTest {
+
+ private DDF obj;
+ private RemotedHttpServletRequest req;
+
+ @BeforeMethod
+ public void setUp() {
+ obj = new DDF();
+ req = new RemotedHttpServletRequest(obj);
+ }
+
+ @Test
+ public void testEmpty() throws IOException {
+ assertEquals(req.getContentLength(), -1);
+ assertEquals(req.getContentType(), null);
+ assertEquals(req.getCookies(), null);
+ assertEquals(req.getHeader("foo"), null);
+ assertEquals(req.getInputStream().read(), -1);
+ assertEquals(req.getMethod(), null);
+ assertEquals(req.getParameter("foo"), null);
+ assertEquals(req.getQueryString(), null);
+ assertEquals(req.getRemoteAddr(), null);
+ assertEquals(req.getRemoteUser(), null);
+ assertEquals(req.getRequestURI(), null);
+ assertEquals(req.getRequestURL().toString(), "");
+ assertEquals(req.getScheme(), null);
+ assertFalse(req.isSecure());
+ assertEquals(req.getServerPort(), -1);
+ }
+
+ @Test
+ public void testBasics() throws IOException {
+ obj.structure();
+ obj.addmember("content_length").integer(100);
+ obj.addmember("content_type").string("text/xml");
+ obj.addmember("method").string("POST");
+ obj.addmember("body").string("<foo/>");
+ obj.addmember("port").integer(80);
+ obj.addmember("client_addr").string("127.0.0.1");
+ obj.addmember("remote_user").string("jdoe");
+ obj.addmember("uri").string("/endpoint");
+ obj.addmember("url").string("http://localhost/endpoint");
+ obj.addmember("scheme").string("http");
+
+ assertEquals(req.getContentLength(), 100);
+ assertEquals(req.getContentType(), "text/xml");
+ assertEquals(req.getCookies(), null);
+ assertEquals(req.getHeader("foo"), null);
+ assertEquals(Files.streamToString(req.getInputStream()), "<foo/>");
+ assertEquals(req.getMethod(), "POST");
+ assertEquals(req.getParameter("foo"), null);
+ assertEquals(req.getQueryString(), null);
+ assertEquals(req.getRemoteAddr(), "127.0.0.1");
+ assertEquals(req.getRemoteUser(), "jdoe");
+ assertEquals(req.getRequestURI(), "/endpoint");
+ assertEquals(req.getRequestURL().toString(), "http://localhost/endpoint");
+ assertEquals(req.getScheme(), "http");
+ assertFalse(req.isSecure());
+ assertEquals(req.getServerPort(), 80);
+ }
+
+ @Test
+ public void testOneQueryParameter() throws IOException {
+ obj.structure();
+ obj.addmember("content_type").string("text/xml");
+ obj.addmember("body").string("<foo/>");
+ obj.addmember("query").string("foo=bar+baz");
+
+ assertEquals(req.getParameterNames().nextElement(), "foo");
+ assertEquals(req.getParameter("foo"), "bar baz");
+ }
+
+ @Test
+ public void testMultiQueryParameters() throws IOException {
+ obj.structure();
+ obj.addmember("content_type").string("text/xml");
+ obj.addmember("body").string("<foo/>");
+ obj.addmember("query").string("foo=bar+baz&zork=grue&foo=baf");
+
+ assertEquals(req.getParameter("foo"), "bar baz");
+ assertEquals(req.getParameterValues("foo"), List.of("bar baz", "baf").toArray());
+ assertEquals(req.getParameter("zork"), "grue");
+ }
+
+ @Test
+ public void testFormParameters() throws IOException {
+ obj.structure();
+ obj.addmember("content_type").string("application/x-www-form-urlencoded");
+ obj.addmember("body").string("foo=baf");
+ obj.addmember("query").string("foo=bar+baz&zork=grue");
+
+ assertEquals(req.getParameter("foo"), "bar baz");
+ assertEquals(req.getParameterValues("foo"), List.of("bar baz", "baf").toArray());
+ assertEquals(req.getParameter("zork"), "grue");
+ }
+
+ @Test
+ public void testHeaders() throws IOException {
+ obj.structure();
+ obj.addmember("headers.foo").string("bar");
+ obj.addmember("headers.zork").string("grue");
+
+ assertEquals(req.getHeaders("foo").nextElement(), "bar");
+ assertEquals(req.getHeader("zork"), "grue");
+ assertNull(req.getHeader("baz"));
+ }
+
+ @Test
+ public void testCookie() throws IOException {
+ obj.structure();
+ obj.addmember("headers.Cookie").string("foo=bar;");
+
+ final Cookie[] cookies = req.getCookies();
+
+ assertEquals(cookies.length, 1);
+ assertEquals(cookies[0].getName(), "foo");
+ assertEquals(cookies[0].getValue(), "bar");
+ }
+
+ @Test
+ public void testCookies() throws IOException {
+ obj.structure();
+ obj.addmember("headers.Cookie").string("foo=bar; zork=grue");
+
+ final Cookie[] cookies = req.getCookies();
+
+ assertEquals(cookies.length, 2);
+ assertEquals(cookies[0].getName(), "foo");
+ assertEquals(cookies[0].getValue(), "bar");
+ assertEquals(cookies[1].getName(), "zork");
+ assertEquals(cookies[1].getValue(), "grue");
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list