|
| 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.test.http; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.ZoneId; |
| 21 | +import java.time.ZonedDateTime; |
| 22 | +import java.time.temporal.ChronoUnit; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.assertj.core.api.AbstractMapAssert; |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | + |
| 28 | +import org.springframework.http.HttpHeaders; |
| 29 | + |
| 30 | +/** |
| 31 | + * AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to |
| 32 | + * {@link HttpHeaders}. |
| 33 | + * |
| 34 | + * @author Stephane Nicoll |
| 35 | + * @since 6.2 |
| 36 | + */ |
| 37 | +public class HttpHeadersAssert extends AbstractMapAssert<HttpHeadersAssert, HttpHeaders, String, List<String>> { |
| 38 | + |
| 39 | + private static final ZoneId GMT = ZoneId.of("GMT"); |
| 40 | + |
| 41 | + |
| 42 | + public HttpHeadersAssert(HttpHeaders actual) { |
| 43 | + super(actual, HttpHeadersAssert.class); |
| 44 | + as("HTTP headers"); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Verify that the actual HTTP headers contain a header with the given |
| 49 | + * {@code name}. |
| 50 | + * @param name the name of an expected HTTP header |
| 51 | + * @see #containsKey |
| 52 | + */ |
| 53 | + public HttpHeadersAssert containsHeader(String name) { |
| 54 | + return containsKey(name); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Verify that the actual HTTP headers contain the headers with the given |
| 59 | + * {@code names}. |
| 60 | + * @param names the names of expected HTTP headers |
| 61 | + * @see #containsKeys |
| 62 | + */ |
| 63 | + public HttpHeadersAssert containsHeaders(String... names) { |
| 64 | + return containsKeys(names); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Verify that the actual HTTP headers do not contain a header with the |
| 69 | + * given {@code name}. |
| 70 | + * @param name the name of an HTTP header that should not be present |
| 71 | + * @see #doesNotContainKey |
| 72 | + */ |
| 73 | + public HttpHeadersAssert doesNotContainsHeader(String name) { |
| 74 | + return doesNotContainKey(name); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Verify that the actual HTTP headers do not contain any of the headers |
| 79 | + * with the given {@code names}. |
| 80 | + * @param names the names of HTTP headers that should not be present |
| 81 | + * @see #doesNotContainKeys |
| 82 | + */ |
| 83 | + public HttpHeadersAssert doesNotContainsHeaders(String... names) { |
| 84 | + return doesNotContainKeys(names); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Verify that the actual HTTP headers contain a header with the given |
| 89 | + * {@code name} and {@link String} {@code value}. |
| 90 | + * @param name the name of the cookie |
| 91 | + * @param value the expected value of the header |
| 92 | + */ |
| 93 | + public HttpHeadersAssert hasValue(String name, String value) { |
| 94 | + containsKey(name); |
| 95 | + Assertions.assertThat(this.actual.getFirst(name)) |
| 96 | + .as("check primary value for HTTP header '%s'", name) |
| 97 | + .isEqualTo(value); |
| 98 | + return this.myself; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Verify that the actual HTTP headers contain a header with the given |
| 103 | + * {@code name} and {@link Long} {@code value}. |
| 104 | + * @param name the name of the cookie |
| 105 | + * @param value the expected value of the header |
| 106 | + */ |
| 107 | + public HttpHeadersAssert hasValue(String name, long value) { |
| 108 | + containsKey(name); |
| 109 | + Assertions.assertThat(this.actual.getFirst(name)) |
| 110 | + .as("check primary long value for HTTP header '%s'", name) |
| 111 | + .asLong().isEqualTo(value); |
| 112 | + return this.myself; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Verify that the actual HTTP headers contain a header with the given |
| 117 | + * {@code name} and {@link Instant} {@code value}. |
| 118 | + * @param name the name of the cookie |
| 119 | + * @param value the expected value of the header |
| 120 | + */ |
| 121 | + public HttpHeadersAssert hasValue(String name, Instant value) { |
| 122 | + containsKey(name); |
| 123 | + Assertions.assertThat(this.actual.getFirstZonedDateTime(name)) |
| 124 | + .as("check primary date value for HTTP header '%s'", name) |
| 125 | + .isCloseTo(ZonedDateTime.ofInstant(value, GMT), Assertions.within(999, ChronoUnit.MILLIS)); |
| 126 | + return this.myself; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments