Skip to content

Commit

Permalink
Add option to disable header name canonicalization. This fixes google#90
Browse files Browse the repository at this point in the history
.

PiperOrigin-RevId: 408399619
Change-Id: I0adca149c11fd396fc69266f9d7cbf6bcf6e334b
  • Loading branch information
Tsunami Team authored and copybara-github committed Nov 8, 2021
1 parent 8ed1756 commit f630cc0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public Builder addHeader(String name, String value) {
return this;
}

public Builder addHeader(String name, String value, boolean canonicalize) {
checkNotNull(name, "Name cannot be null.");
checkNotNull(value, "Value cannot be null.");
if (canonicalize) {
return addHeader(name, value);
} else {
rawHeadersBuilder().put(name, value);
return this;
}
}

public abstract HttpHeaders build();

private static boolean isLegalHeaderName(String str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ public void builderAddHeader_withKnownHeader_canonicalizesHeaderName() {
ImmutableListMultimap.of(com.google.common.net.HttpHeaders.ACCEPT, "test_value"));
}

@Test
public void builderAddHeader_whenEnableCanonicalization_canonicalizesHeaderName() {
HttpHeaders httpHeaders =
HttpHeaders.builder()
.addHeader("TEST_Header", "test_value", true)
.build();
assertThat(httpHeaders.rawHeaders())
.containsExactlyEntriesIn(
ImmutableListMultimap.of("test_header", "test_value"));
}

@Test
public void builderAddHeader_whenDisableCanonicalization_addsHeaderNameAsIs() {
HttpHeaders httpHeaders =
HttpHeaders.builder()
.addHeader("TEST_Header", "test_value", false)
.build();
assertThat(httpHeaders.rawHeaders())
.containsExactlyEntriesIn(
ImmutableListMultimap.of("TEST_Header", "test_value"));
}

@Test
public void builderAddHeader_withNullName_throwsNullPointerException() {
assertThrows(
Expand Down

0 comments on commit f630cc0

Please sign in to comment.