Skip to content

Commit 9a2bfea

Browse files
committed
AbstractMockHttpServletRequestBuilder exposes HttpHeaders
Closes gh-35576
1 parent 9ed3c03 commit 9a2bfea

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/AbstractMockHttpServletRequestBuilder.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.nio.charset.StandardCharsets;
2727
import java.security.Principal;
2828
import java.text.SimpleDateFormat;
29+
import java.time.ZonedDateTime;
2930
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.Collection;
@@ -35,6 +36,7 @@
3536
import java.util.Locale;
3637
import java.util.Map;
3738
import java.util.TimeZone;
39+
import java.util.function.Consumer;
3840

3941
import jakarta.servlet.ServletContext;
4042
import jakarta.servlet.ServletRequest;
@@ -348,6 +350,37 @@ public B accept(String... mediaTypes) {
348350
return self();
349351
}
350352

353+
/**
354+
* Set the list of acceptable {@linkplain Charset charsets}, as specified
355+
* by the {@code Accept-Charset} header.
356+
* @param acceptableCharsets the acceptable charsets
357+
* @since 7.0
358+
*/
359+
public B acceptCharset(Charset... acceptableCharsets) {
360+
this.headers.setAcceptCharset(Arrays.asList(acceptableCharsets));
361+
return self();
362+
}
363+
364+
/**
365+
* Set the value of the {@code If-Modified-Since} header.
366+
* @param ifModifiedSince the new value of the header
367+
* @since 7.0
368+
*/
369+
public B ifModifiedSince(ZonedDateTime ifModifiedSince) {
370+
this.headers.setIfModifiedSince(ifModifiedSince);
371+
return self();
372+
}
373+
374+
/**
375+
* Set the values of the {@code If-None-Match} header.
376+
* @param ifNoneMatches the new value of the header
377+
* @since 7.0
378+
*/
379+
public B ifNoneMatch(String... ifNoneMatches) {
380+
this.headers.setIfNoneMatch(Arrays.asList(ifNoneMatches));
381+
return self();
382+
}
383+
351384
/**
352385
* Add a header to the request. Values are always added.
353386
* @param name the header name
@@ -386,6 +419,18 @@ public B headers(HttpHeaders httpHeaders) {
386419
return self();
387420
}
388421

422+
/**
423+
* Provides access to every header declared so far with the possibility
424+
* to add, replace, or remove values.
425+
* @param headersConsumer the consumer to provide access to
426+
* @return this builder
427+
* @since 7.0
428+
*/
429+
public B headers(Consumer<HttpHeaders> headersConsumer) {
430+
headersConsumer.accept(this.headers);
431+
return self();
432+
}
433+
389434
/**
390435
* Add a request parameter to {@link MockHttpServletRequest#getParameterMap()}.
391436
* <p>In the Servlet API, a request parameter may be parsed from the query

0 commit comments

Comments
 (0)