Skip to content

Commit e06115c

Browse files
committed
Merge pull request #33156 from
* pr/33156: Polish "Implement DefaultErrorResponseBuilder#headers(Consumer)" Implement DefaultErrorResponseBuilder#headers(Consumer) Closes gh-33156
2 parents 83f7996 + 69850ca commit e06115c

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,18 +66,25 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
6666

6767
@Override
6868
public ErrorResponse.Builder header(String headerName, String... headerValues) {
69-
this.headers = (this.headers != null ? this.headers : new HttpHeaders());
7069
for (String headerValue : headerValues) {
71-
this.headers.add(headerName, headerValue);
70+
getHeaders().add(headerName, headerValue);
7271
}
7372
return this;
7473
}
7574

7675
@Override
7776
public ErrorResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
77+
headersConsumer.accept(getHeaders());
7878
return this;
7979
}
8080

81+
private HttpHeaders getHeaders() {
82+
if (this.headers == null) {
83+
this.headers = new HttpHeaders();
84+
}
85+
return this.headers;
86+
}
87+
8188
@Override
8289
public ErrorResponse.Builder type(URI type) {
8390
this.problemDetail.setType(type);
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)