Skip to content

Commit 69850ca

Browse files
committed
Polish "Implement DefaultErrorResponseBuilder#headers(Consumer)"
See gh-33156
1 parent bbbc95f commit 69850ca

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
* Default implementation of {@link ErrorResponse.Builder}.
3131
*
3232
* @author Rossen Stoyanchev
33-
* @author 海子 Yang
3433
* @since 6.0
3534
*/
3635
final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
@@ -68,18 +67,18 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
6867
@Override
6968
public ErrorResponse.Builder header(String headerName, String... headerValues) {
7069
for (String headerValue : headerValues) {
71-
httpHeaders().add(headerName, headerValue);
70+
getHeaders().add(headerName, headerValue);
7271
}
7372
return this;
7473
}
7574

7675
@Override
7776
public ErrorResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
78-
headersConsumer.accept(httpHeaders());
77+
headersConsumer.accept(getHeaders());
7978
return this;
8079
}
8180

82-
private HttpHeaders httpHeaders() {
81+
private HttpHeaders getHeaders() {
8382
if (this.headers == null) {
8483
this.headers = new HttpHeaders();
8584
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.http.HttpStatus;
24+
25+
import static java.util.Map.entry;
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ErrorResponse}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
class ErrorResponseTests {
34+
35+
@Test
36+
void createWithHttpHeader() {
37+
ErrorResponse response = ErrorResponse.builder(new IllegalStateException(), HttpStatus.BAD_REQUEST, "test")
38+
.header("header", "value").build();
39+
assertThat(response.getHeaders()).containsOnly(entry("header", List.of("value")));
40+
}
41+
42+
@Test
43+
void createWithHttpHeadersConsumer() {
44+
ErrorResponse response = ErrorResponse.builder(new IllegalStateException(), HttpStatus.BAD_REQUEST, "test")
45+
.header("header", "value")
46+
.headers(headers -> {
47+
headers.add("header", "value2");
48+
headers.add("another", "value3");
49+
}).build();
50+
assertThat(response.getHeaders()).containsOnly(entry("header", List.of("value", "value2")),
51+
entry("another", List.of("value3")));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)