DefaultRequestHeadersBuilder extends AbstractHttpHeadersBuilder<RequestHeadersBuilder> implements RequestHeadersBuilder { @Override public RequestHeaders build() { final HttpHeadersBase delegate = delegate(); if (delegate != null) { checkState(delegate.contains(HttpHeaderNames.METHOD), ":method header does not exist."); checkState(delegate.contains(HttpHeaderNames.PATH), ":path header does not exist."); return new DefaultRequestHeaders(promoteDelegate()); } final HttpHeadersBase parent = parent(); if (parent != null) { if (parent instanceof RequestHeaders) { return (RequestHeaders) parent; } else { return updateParent(new DefaultRequestHeaders(parent)); } } throw new IllegalStateException("must set ':method' and ':path' headers"); } DefaultRequestHeadersBuilder();   DefaultRequestHeadersBuilder(DefaultRequestHeaders headers); @Override RequestHeaders build(); @Override URI uri(); @Override HttpMethod method(); @Override RequestHeadersBuilder method(HttpMethod method); @Override String path(); @Override RequestHeadersBuilder path(String path); @Nullable @Override String scheme(); @Override RequestHeadersBuilder scheme(String scheme); @Nullable @Override String authority(); @Override RequestHeadersBuilder authority(String authority);  }
@Test void mutationAfterBuild() { final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, "/"); final DefaultRequestHeadersBuilder builder = (DefaultRequestHeadersBuilder) 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(headers2); assertThat(headers).isNotSameAs(headers3); assertThat(headers).containsExactly(Maps.immutableEntry(HttpHeaderNames.METHOD, "GET"), Maps.immutableEntry(HttpHeaderNames.PATH, "/")); assertThat(headers2).containsExactly(Maps.immutableEntry(HttpHeaderNames.METHOD, "GET"), Maps.immutableEntry(HttpHeaderNames.PATH, "/"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d")); assertThat(headers3).containsExactly(Maps.immutableEntry(HttpHeaderNames.METHOD, "GET"), Maps.immutableEntry(HttpHeaderNames.PATH, "/"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d"), Maps.immutableEntry(HttpHeaderNames.of("e"), "f")); }
@Test void noMutationNoCopy() { final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, "/"); final DefaultRequestHeadersBuilder builder = (DefaultRequestHeadersBuilder) headers.toBuilder(); assertThat(builder.build()).isSameAs(headers); assertThat(builder.delegate()).isNull(); }
@Test void buildTwice() { final RequestHeadersBuilder builder = RequestHeaders.builder(HttpMethod.GET, "/").add("foo", "bar"); assertThat(builder.build()).isEqualTo(RequestHeaders.of(HttpMethod.GET, "/", "foo", "bar")); assertThat(builder.build()).isEqualTo(RequestHeaders.of(HttpMethod.GET, "/", "foo", "bar")); }