DefaultHttpHeadersBuilder extends AbstractHttpHeadersBuilder<DefaultHttpHeadersBuilder> implements HttpHeadersBuilder { @Override public HttpHeaders build() { final HttpHeadersBase delegate = delegate(); if (delegate != null) { if (delegate.isEmpty()) { return delegate.isEndOfStream() ? DefaultHttpHeaders.EMPTY_EOS : DefaultHttpHeaders.EMPTY; } else { return new DefaultHttpHeaders(promoteDelegate()); } } final HttpHeadersBase parent = parent(); if (parent != null) { if (parent instanceof HttpHeaders) { return (HttpHeaders) parent; } return updateParent(new DefaultHttpHeaders(parent)); } return DefaultHttpHeaders.EMPTY; } DefaultHttpHeadersBuilder();   DefaultHttpHeadersBuilder(HttpHeadersBase parent); @Override HttpHeaders build();  }
@Test void add() { final HttpHeaders headers = HttpHeaders.builder() .add("a", "b") .add("c", ImmutableList.of("d", "e")) .add("f", "g", "h") .add(ImmutableMap.of("i", "j").entrySet()) .build(); assertThat(headers).containsExactly( Maps.immutableEntry(HttpHeaderNames.of("a"), "b"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d"), Maps.immutableEntry(HttpHeaderNames.of("c"), "e"), Maps.immutableEntry(HttpHeaderNames.of("f"), "g"), Maps.immutableEntry(HttpHeaderNames.of("f"), "h"), Maps.immutableEntry(HttpHeaderNames.of("i"), "j")); }
@Test void set() { final HttpHeaders headers = HttpHeaders.builder() .add("a", "b") .add("c", ImmutableList.of("d", "e")) .add("f", "g", "h") .add(ImmutableMap.of("i", "j").entrySet()) .set("a", "B") .set("c", ImmutableList.of("D", "E")) .set("f", "G", "H") .set(ImmutableMap.of("i", "J").entrySet()) .build(); assertThat(headers).containsExactly( Maps.immutableEntry(HttpHeaderNames.of("a"), "B"), Maps.immutableEntry(HttpHeaderNames.of("c"), "D"), Maps.immutableEntry(HttpHeaderNames.of("c"), "E"), Maps.immutableEntry(HttpHeaderNames.of("f"), "G"), Maps.immutableEntry(HttpHeaderNames.of("f"), "H"), Maps.immutableEntry(HttpHeaderNames.of("i"), "J")); }
@Test void mutation() { final HttpHeaders headers = HttpHeaders.of("a", "b"); final HttpHeaders headers2 = headers.toBuilder().set("a", "c").build(); assertThat(headers).isNotSameAs(headers2); assertThat(headers).isNotEqualTo(headers2); assertThat(headers).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "b")); assertThat(headers2).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "c")); }
@Test void mutationEndOfStreamOnly() { final HttpHeaders headers = HttpHeaders.of("a", "b"); final HttpHeaders headers2 = headers.toBuilder().endOfStream(true).build(); assertThat(headers.isEndOfStream()).isFalse(); assertThat(headers2.isEndOfStream()).isTrue(); assertThat(headers).isNotSameAs(headers2); assertThat(headers).isNotEqualTo(headers2); assertThat(headers).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "b")); assertThat(headers2).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "b")); }
@Test void mutationAfterBuild() { final HttpHeaders headers = HttpHeaders.of("a", "b"); final DefaultHttpHeadersBuilder builder = (DefaultHttpHeadersBuilder) headers.toBuilder(); assertThat(builder.parent()).isSameAs(headers); assertThat(builder.delegate()).isNull(); builder.add("c", "d"); assertThat(builder.parent()).isSameAs(headers); assertThat(builder.delegate()).isNotNull().isNotSameAs(builder.parent()); HttpHeadersBase oldDelegate = builder.delegate(); final HttpHeaders headers2 = builder.build(); assertThat(headers2).isNotSameAs(headers); assertThat(((HttpHeadersBase) headers2).entries).isNotSameAs(((HttpHeadersBase) headers).entries); assertThat(builder.parent()).isSameAs(oldDelegate); assertThat(builder.delegate()).isNull(); builder.add("e", "f"); assertThat(builder.parent()).isSameAs(oldDelegate); assertThat(builder.delegate()).isNotNull().isNotSameAs(builder.parent()); oldDelegate = builder.delegate(); final HttpHeaders headers3 = builder.build(); assertThat(headers3).isNotSameAs(headers); assertThat(headers3).isNotSameAs(headers2); assertThat(((HttpHeadersBase) headers3).entries).isNotSameAs(((HttpHeadersBase) headers).entries); assertThat(((HttpHeadersBase) headers3).entries).isNotSameAs(((HttpHeadersBase) headers2).entries); assertThat(builder.parent()).isSameAs(oldDelegate); assertThat(builder.delegate()).isNull(); builder.clear(); assertThat(headers).isNotSameAs(headers3); assertThat(headers).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "b")); assertThat(headers2).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "b"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d")); assertThat(headers3).containsExactly(Maps.immutableEntry(HttpHeaderNames.of("a"), "b"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d"), Maps.immutableEntry(HttpHeaderNames.of("e"), "f")); }
@Test void noMutationNoCopy() { final HttpHeaders headers = HttpHeaders.of("a", "b"); assertThat(headers.toBuilder().build()).isSameAs(headers); final DefaultHttpHeadersBuilder builder = (DefaultHttpHeadersBuilder) headers.toBuilder(); builder.getAndRemove("c"); builder.getAndRemove("c", "d"); builder.getIntAndRemove("c"); builder.getIntAndRemove("c", 0); builder.getLongAndRemove("c"); builder.getLongAndRemove("c", 0); builder.getFloatAndRemove("c"); builder.getFloatAndRemove("c", 0); builder.getDoubleAndRemove("c"); builder.getDoubleAndRemove("c", 0); builder.getAllAndRemove("c"); builder.getTimeMillisAndRemove("c"); builder.getTimeMillisAndRemove("c", 0); builder.remove("c"); builder.removeAndThen("c"); assertThat(builder.build()).isSameAs(headers); assertThat(builder.delegate()).isNull(); }
@Test void empty() { final HttpHeaders headers = HttpHeaders.of("a", "b"); assertThat(headers.toBuilder() .clear() .build()).isSameAs(DefaultHttpHeaders.EMPTY); assertThat(headers.toBuilder() .endOfStream(true) .clear() .build()).isSameAs(DefaultHttpHeaders.EMPTY_EOS); }
@Test void buildTwice() { final HttpHeadersBuilder builder = HttpHeaders.builder().add("foo", "bar"); assertThat(builder.build()).isEqualTo(HttpHeaders.of("foo", "bar")); assertThat(builder.build()).isEqualTo(HttpHeaders.of("foo", "bar")); }