Polish "Implement DefaultErrorResponseBuilder#headers(Consumer)"
See gh-33156
This commit is contained in:
parent
bbbc95f773
commit
69850cad27
|
@ -30,7 +30,6 @@ import org.springframework.util.Assert;
|
|||
* Default implementation of {@link ErrorResponse.Builder}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author 海子 Yang
|
||||
* @since 6.0
|
||||
*/
|
||||
final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
||||
|
@ -68,18 +67,18 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
|
|||
@Override
|
||||
public ErrorResponse.Builder header(String headerName, String... headerValues) {
|
||||
for (String headerValue : headerValues) {
|
||||
httpHeaders().add(headerName, headerValue);
|
||||
getHeaders().add(headerName, headerValue);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
|
||||
headersConsumer.accept(httpHeaders());
|
||||
headersConsumer.accept(getHeaders());
|
||||
return this;
|
||||
}
|
||||
|
||||
private HttpHeaders httpHeaders() {
|
||||
private HttpHeaders getHeaders() {
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ErrorResponse}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class ErrorResponseTests {
|
||||
|
||||
@Test
|
||||
void createWithHttpHeader() {
|
||||
ErrorResponse response = ErrorResponse.builder(new IllegalStateException(), HttpStatus.BAD_REQUEST, "test")
|
||||
.header("header", "value").build();
|
||||
assertThat(response.getHeaders()).containsOnly(entry("header", List.of("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithHttpHeadersConsumer() {
|
||||
ErrorResponse response = ErrorResponse.builder(new IllegalStateException(), HttpStatus.BAD_REQUEST, "test")
|
||||
.header("header", "value")
|
||||
.headers(headers -> {
|
||||
headers.add("header", "value2");
|
||||
headers.add("another", "value3");
|
||||
}).build();
|
||||
assertThat(response.getHeaders()).containsOnly(entry("header", List.of("value", "value2")),
|
||||
entry("another", List.of("value3")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue