001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet.result;
018
019import java.util.Arrays;
020import java.util.List;
021
022import org.hamcrest.Matcher;
023
024import org.springframework.http.HttpHeaders;
025import org.springframework.mock.web.MockHttpServletResponse;
026import org.springframework.test.web.servlet.ResultMatcher;
027
028import static org.hamcrest.MatcherAssert.assertThat;
029import static org.springframework.test.util.AssertionErrors.assertEquals;
030import static org.springframework.test.util.AssertionErrors.assertFalse;
031import static org.springframework.test.util.AssertionErrors.assertNotNull;
032import static org.springframework.test.util.AssertionErrors.assertTrue;
033
034/**
035 * Factory for response header assertions.
036 *
037 * <p>An instance of this class is available via
038 * {@link MockMvcResultMatchers#header}.
039 *
040 * @author Rossen Stoyanchev
041 * @author Sam Brannen
042 * @author Brian Clozel
043 * @since 3.2
044 */
045public class HeaderResultMatchers {
046
047        /**
048         * Protected constructor.
049         * See {@link MockMvcResultMatchers#header()}.
050         */
051        protected HeaderResultMatchers() {
052        }
053
054
055        /**
056         * Assert the primary value of the response header with the given Hamcrest
057         * String {@code Matcher}.
058         */
059        public ResultMatcher string(String name, Matcher<? super String> matcher) {
060                return result -> assertThat("Response header '" + name + "'", result.getResponse().getHeader(name), matcher);
061        }
062
063        /**
064         * Assert the values of the response header with the given Hamcrest
065         * Iterable {@link Matcher}.
066         * @since 4.3
067         */
068        public ResultMatcher stringValues(String name, Matcher<Iterable<String>> matcher) {
069                return result -> {
070                        List<String> values = result.getResponse().getHeaders(name);
071                        assertThat("Response header '" + name + "'", values, matcher);
072                };
073        }
074
075        /**
076         * Assert the primary value of the response header as a String value.
077         */
078        public ResultMatcher string(String name, String value) {
079                return result -> assertEquals("Response header '" + name + "'", value, result.getResponse().getHeader(name));
080        }
081
082        /**
083         * Assert the values of the response header as String values.
084         * @since 4.3
085         */
086        public ResultMatcher stringValues(String name, String... values) {
087                return result -> {
088                        List<Object> actual = result.getResponse().getHeaderValues(name);
089                        assertEquals("Response header '" + name + "'", Arrays.asList(values), actual);
090                };
091        }
092
093        /**
094         * Assert that the named response header exists.
095         * @since 5.0.3
096         */
097        public ResultMatcher exists(String name) {
098                return result -> assertTrue("Response should contain header '" + name + "'",
099                                result.getResponse().containsHeader(name));
100        }
101
102        /**
103         * Assert that the named response header does not exist.
104         * @since 4.0
105         */
106        public ResultMatcher doesNotExist(String name) {
107                return result -> assertFalse("Response should not contain header '" + name + "'",
108                                result.getResponse().containsHeader(name));
109        }
110
111        /**
112         * Assert the primary value of the named response header as a {@code long}.
113         * <p>The {@link ResultMatcher} returned by this method throws an
114         * {@link AssertionError} if the response does not contain the specified
115         * header, or if the supplied {@code value} does not match the primary value.
116         */
117        public ResultMatcher longValue(String name, long value) {
118                return result -> {
119                        MockHttpServletResponse response = result.getResponse();
120                        assertTrue("Response does not contain header '" + name + "'", response.containsHeader(name));
121                        String headerValue = response.getHeader(name);
122                        if (headerValue != null) {
123                                assertEquals("Response header '" + name + "'", value, Long.parseLong(headerValue));
124                        }
125                };
126        }
127
128        /**
129         * Assert the primary value of the named response header parsed into a date
130         * using the preferred date format described in RFC 7231.
131         * <p>The {@link ResultMatcher} returned by this method throws an
132         * {@link AssertionError} if the response does not contain the specified
133         * header, or if the supplied {@code value} does not match the primary value.
134         * @since 4.2
135         * @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
136         */
137        public ResultMatcher dateValue(String name, long value) {
138                return result -> {
139                        MockHttpServletResponse response = result.getResponse();
140                        String headerValue = response.getHeader(name);
141                        assertNotNull("Response does not contain header '" + name + "'", headerValue);
142
143                        HttpHeaders headers = new HttpHeaders();
144                        headers.setDate("expected", value);
145                        headers.set("actual", headerValue);
146
147                        assertEquals("Response header '" + name + "'='" + headerValue + "' " +
148                                                        "does not match expected value '" + headers.getFirst("expected") + "'",
149                                        headers.getFirstDate("expected"), headers.getFirstDate("actual"));
150                };
151        }
152
153}