Skip to content

Commit

Permalink
Add headerBuilder.addAll(headers) method (#4121)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Jul 6, 2018
1 parent 21acfae commit c84c8fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions okhttp-tests/src/test/java/okhttp3/HeadersTest.java
Expand Up @@ -343,6 +343,20 @@ public final class HeadersTest {
assertEquals("A: a\nB: bb\n", headers.toString());
}

@Test public void headersAddAll() {
Headers sourceHeaders = new Headers.Builder()
.add("A", "aa")
.add("a", "aa")
.add("B", "bb")
.build();
Headers headers = new Headers.Builder()
.add("A", "a")
.addAll(sourceHeaders)
.add("C", "c")
.build();
assertEquals("A: a\nA: aa\na: aa\nB: bb\nC: c\n", headers.toString());
}

/** See https://github.com/square/okhttp/issues/2780. */
@Test public void testDigestChallenges() {
// Strict RFC 2617 header.
Expand Down
16 changes: 15 additions & 1 deletion okhttp/src/main/java/okhttp3/Headers.java
Expand Up @@ -283,12 +283,26 @@ public Builder add(String line) {
return add(line.substring(0, index).trim(), line.substring(index + 1));
}

/** Add a field with the specified value. */
/**
* Add a header with the specified name and value. Does validation of header names and values.
*/
public Builder add(String name, String value) {
checkNameAndValue(name, value);
return addLenient(name, value);
}

/**
* Adds all headers from an existing collection. Does validation of header names and values.
*/
public Builder addAll(Headers headers) {
int size = headers.size();
for (int i = 0; i < size; i++) {
addLenient(headers.name(i), headers.value(i));
}

return this;
}

/**
* Add a field with the specified value without any validation. Only appropriate for headers
* from the remote peer or cache.
Expand Down

0 comments on commit c84c8fa

Please sign in to comment.