[OLINGO-328] improve multi valued headers of ODataRequest

This commit is contained in:
Stephan Klevenz 2014-06-26 08:53:18 +02:00
parent 97222fad31
commit af3f998b7f
2 changed files with 45 additions and 11 deletions

View File

@ -19,6 +19,7 @@
package org.apache.olingo.server.api; package org.apache.olingo.server.api;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -44,13 +45,25 @@ public class ODataRequest {
} }
/** /**
* Add header to request where name handled as case insensitive key. * Add header to request where name handled as case insensitive key. If a header already exists then the list of
* values will just be extended.
* @param name case insensitive header name * @param name case insensitive header name
* @param values * @param values
*/ */
public void addHeader(String name, List<String> values) { public void addHeader(String name, List<String> values) {
String key = name.toUpperCase();
if (headers.containsKey(key)) {
List<String> oldValues = headers.get(key);
List<String> newValues = new ArrayList<String>();
newValues.addAll(oldValues);
newValues.addAll(values);
headers.put(name.toUpperCase(), newValues);
} else {
headers.put(name.toUpperCase(), values); headers.put(name.toUpperCase(), values);
} }
}
/** /**
* Returns header value for name where name is a case insensitive key. * Returns header value for name where name is a case insensitive key.

View File

@ -36,10 +36,31 @@ public class ODataRequestTest {
assertEquals("cc", r.getHeader("aA").get(0)); assertEquals("cc", r.getHeader("aA").get(0));
assertEquals("cc", r.getHeader("AA").get(0)); assertEquals("cc", r.getHeader("AA").get(0));
}
@Test
public void testHeader2() {
ODataRequest r = new ODataRequest();
r.addHeader("AA", Arrays.asList("dd")); r.addHeader("AA", Arrays.asList("dd"));
assertEquals("dd", r.getHeader("aa").get(0)); assertEquals("dd", r.getHeader("aa").get(0));
assertEquals("dd", r.getHeader("aA").get(0)); assertEquals("dd", r.getHeader("aA").get(0));
assertEquals("dd", r.getHeader("AA").get(0)); assertEquals("dd", r.getHeader("AA").get(0));
} }
@Test
public void testMultiValueHeader() {
ODataRequest r = new ODataRequest();
r.addHeader("aa", Arrays.asList("a", "b"));
assertEquals("a", r.getHeader("aa").get(0));
assertEquals("b", r.getHeader("aA").get(1));
r.addHeader("Aa", Arrays.asList("c"));
assertEquals("a", r.getHeader("aa").get(0));
assertEquals("b", r.getHeader("aA").get(1));
assertEquals("c", r.getHeader("aA").get(2));
}
} }