DefaultResponseHeadersBuilder extends AbstractHttpHeadersBuilder<ResponseHeadersBuilder> implements ResponseHeadersBuilder { @Override public ResponseHeaders build() { final HttpHeadersBase delegate = delegate(); if (delegate != null) { checkState(delegate.contains(HttpHeaderNames.STATUS), STATUS_HEADER_MISSING); return new DefaultResponseHeaders(promoteDelegate()); } final HttpHeadersBase parent = parent(); if (parent != null) { if (parent instanceof ResponseHeaders) { return (ResponseHeaders) parent; } else { return updateParent(new DefaultResponseHeaders(parent)); } } throw new IllegalStateException(STATUS_HEADER_MISSING); } DefaultResponseHeadersBuilder();   DefaultResponseHeadersBuilder(DefaultResponseHeaders headers); @Override ResponseHeaders build(); @Override HttpStatus status(); @Override ResponseHeadersBuilder status(int statusCode); @Override ResponseHeadersBuilder status(HttpStatus status);  }
@Test void mutationAfterBuild() { final ResponseHeaders headers = ResponseHeaders.of(200); final DefaultResponseHeadersBuilder builder = (DefaultResponseHeadersBuilder) 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).containsExactly(Maps.immutableEntry(HttpHeaderNames.STATUS, "200")); assertThat(headers2).containsExactly(Maps.immutableEntry(HttpHeaderNames.STATUS, "200"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d")); assertThat(headers3).containsExactly(Maps.immutableEntry(HttpHeaderNames.STATUS, "200"), Maps.immutableEntry(HttpHeaderNames.of("c"), "d"), Maps.immutableEntry(HttpHeaderNames.of("e"), "f")); }
@Test void noMutationNoCopy() { final ResponseHeaders headers = ResponseHeaders.of(200); final DefaultResponseHeadersBuilder builder = (DefaultResponseHeadersBuilder) headers.toBuilder(); assertThat(builder.build()).isSameAs(headers); assertThat(builder.delegate()).isNull(); }
@Test void buildTwice() { final ResponseHeadersBuilder builder = ResponseHeaders.builder(200).add("foo", "bar"); assertThat(builder.build()).isEqualTo(ResponseHeaders.of(HttpStatus.OK, "foo", "bar")); assertThat(builder.build()).isEqualTo(ResponseHeaders.of(HttpStatus.OK, "foo", "bar")); }
@Test void validation() { assertThatThrownBy(() -> ResponseHeaders.builder().build()) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(":status"); assertThatThrownBy(() -> ResponseHeaders.builder().add("a", "b").build()) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(":status"); }