1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

Merge pull request #1330 from joschi/headers-map

Allow to create Headers from Map
This commit is contained in:
Jesse Wilson
2015-01-16 22:54:29 -05:00
2 changed files with 97 additions and 0 deletions

View File

@@ -22,7 +22,11 @@ import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.spdy.Header;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import static com.squareup.okhttp.TestUtil.headerEntries;
@@ -234,4 +238,68 @@ public final class HeadersTest {
} catch (IllegalArgumentException expected) {
}
}
@Test public void ofMapThrowsOnNull() {
try {
Headers.of(Collections.<String, String>singletonMap("User-Agent", null));
fail();
} catch (IllegalArgumentException expected) {
}
}
@Test public void ofMapThrowsOnEmptyName() {
try {
Headers.of(Collections.singletonMap("", "OkHttp"));
fail();
} catch (IllegalArgumentException expected) {
}
}
@Test public void ofMapThrowsOnBlankName() {
try {
Headers.of(Collections.singletonMap(" ", "OkHttp"));
fail();
} catch (IllegalArgumentException expected) {
}
}
@Test public void ofMapAcceptsEmptyValue() {
Headers headers = Headers.of(Collections.singletonMap("User-Agent", ""));
assertEquals("", headers.value(0));
}
@Test public void ofMapTrimsKey() {
Headers headers = Headers.of(Collections.singletonMap(" User-Agent ", "OkHttp"));
assertEquals("User-Agent", headers.name(0));
}
@Test public void ofMapTrimsValue() {
Headers headers = Headers.of(Collections.singletonMap("User-Agent", " OkHttp "));
assertEquals("OkHttp", headers.value(0));
}
@Test public void ofMapMakesDefensiveCopy() {
Map<String, String> namesAndValues = new HashMap<>();
namesAndValues.put("User-Agent", "OkHttp");
Headers headers = Headers.of(namesAndValues);
namesAndValues.put("User-Agent", "Chrome");
assertEquals("OkHttp", headers.value(0));
}
@Test public void ofMapRejectsNulCharInName() {
try {
Headers.of(Collections.singletonMap("User-Agent", "Square\u0000OkHttp"));
fail();
} catch (IllegalArgumentException expected) {
}
}
@Test public void ofMapRejectsNulCharInValue() {
try {
Headers.of(Collections.singletonMap("User-\u0000Agent", "OkHttp"));
fail();
} catch (IllegalArgumentException expected) {
}
}
}

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
@@ -167,6 +168,34 @@ public final class Headers {
return new Headers(namesAndValues);
}
/**
* Returns headers for the header names and values in the {@link Map}.
*/
public static Headers of(Map<String, String> headers) {
if (headers == null) {
throw new IllegalArgumentException("Expected map with header names and values");
}
// Make a defensive copy and clean it up.
String[] namesAndValues = new String[headers.size() * 2];
int i = 0;
for (Map.Entry<String, String> header : headers.entrySet()) {
if (header.getKey() == null || header.getValue() == null) {
throw new IllegalArgumentException("Headers cannot be null");
}
String name = header.getKey().trim();
String value = header.getValue().trim();
if (name.length() == 0 || name.indexOf('\0') != -1 || value.indexOf('\0') != -1) {
throw new IllegalArgumentException("Unexpected header: " + name + ": " + value);
}
namesAndValues[i] = name;
namesAndValues[i + 1] = value;
i += 2;
}
return new Headers(namesAndValues);
}
public static final class Builder {
private final List<String> namesAndValues = new ArrayList<>(20);