summaryrefslogtreecommitdiff
path: root/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
diff options
context:
space:
mode:
Diffstat (limited to 'spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java')
-rw-r--r--spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java88
1 files changed, 63 insertions, 25 deletions
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
index c57387ee..aac0bab9 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,22 +16,26 @@
package org.springframework.test.web.servlet.result;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
+
import org.hamcrest.Matcher;
+import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
-import static org.hamcrest.MatcherAssert.*;
-import static org.springframework.test.util.AssertionErrors.*;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
-import java.util.TimeZone;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.springframework.test.util.AssertionErrors.assertEquals;
+import static org.springframework.test.util.AssertionErrors.assertTrue;
/**
* Factory for response header assertions.
- * <p>An instance of this class is usually accessed via
+ * <p>An instance of this class is available via
* {@link MockMvcResultMatchers#header}.
*
* @author Rossen Stoyanchev
@@ -41,16 +45,18 @@ import java.util.TimeZone;
*/
public class HeaderResultMatchers {
+
/**
* Protected constructor.
- * Use {@link MockMvcResultMatchers#header()}.
+ * See {@link MockMvcResultMatchers#header()}.
*/
protected HeaderResultMatchers() {
}
+
/**
- * Assert the primary value of the named response header with the given
- * Hamcrest {@link Matcher}.
+ * Assert the primary value of the response header with the given Hamcrest
+ * String {@code Matcher}.
*/
public ResultMatcher string(final String name, final Matcher<? super String> matcher) {
return new ResultMatcher() {
@@ -62,7 +68,22 @@ public class HeaderResultMatchers {
}
/**
- * Assert the primary value of the named response header as a {@link String}.
+ * Assert the values of the response header with the given Hamcrest
+ * Iterable {@link Matcher}.
+ * @since 4.3
+ */
+ public <T> ResultMatcher stringValues(final String name, final Matcher<Iterable<String>> matcher) {
+ return new ResultMatcher() {
+ @Override
+ public void match(MvcResult result) {
+ List<String> values = result.getResponse().getHeaders(name);
+ assertThat("Response header " + name, values, matcher);
+ }
+ };
+ }
+
+ /**
+ * Assert the primary value of the response header as a String value.
*/
public ResultMatcher string(final String name, final String value) {
return new ResultMatcher() {
@@ -74,6 +95,20 @@ public class HeaderResultMatchers {
}
/**
+ * Assert the values of the response header as String values.
+ * @since 4.3
+ */
+ public ResultMatcher stringValues(final String name, final String... values) {
+ return new ResultMatcher() {
+ @Override
+ public void match(MvcResult result) {
+ List<Object> actual = result.getResponse().getHeaderValues(name);
+ assertEquals("Response header " + name, Arrays.asList(values), actual);
+ }
+ };
+ }
+
+ /**
* Assert that the named response header does not exist.
* @since 4.0
*/
@@ -81,23 +116,25 @@ public class HeaderResultMatchers {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
- assertTrue("Response should not contain header " + name, !result.getResponse().containsHeader(name));
+ assertTrue("Response should not contain header " + name,
+ !result.getResponse().containsHeader(name));
}
};
}
/**
* Assert the primary value of the named response header as a {@code long}.
- * <p>The {@link ResultMatcher} returned by this method throws an {@link AssertionError}
- * if the response does not contain the specified header, or if the supplied
- * {@code value} does not match the primary value.
+ * <p>The {@link ResultMatcher} returned by this method throws an
+ * {@link AssertionError} if the response does not contain the specified
+ * header, or if the supplied {@code value} does not match the primary value.
*/
public ResultMatcher longValue(final String name, final long value) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
- assertTrue("Response does not contain header " + name, result.getResponse().containsHeader(name));
- assertEquals("Response header " + name, value, Long.parseLong(result.getResponse().getHeader(name)));
+ MockHttpServletResponse response = result.getResponse();
+ assertTrue("Response does not contain header " + name, response.containsHeader(name));
+ assertEquals("Response header " + name, value, Long.parseLong(response.getHeader(name)));
}
};
}
@@ -105,10 +142,9 @@ public class HeaderResultMatchers {
/**
* Assert the primary value of the named response header as a date String,
* using the preferred date format described in RFC 7231.
- * <p>The {@link ResultMatcher} returned by this method throws an {@link AssertionError}
- * if the response does not contain the specified header, or if the supplied
- * {@code value} does not match the primary value.
- *
+ * <p>The {@link ResultMatcher} returned by this method throws an
+ * {@link AssertionError} if the response does not contain the specified
+ * header, or if the supplied {@code value} does not match the primary value.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
* @since 4.2
*/
@@ -118,8 +154,10 @@ public class HeaderResultMatchers {
public void match(MvcResult result) {
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
- assertTrue("Response does not contain header " + name, result.getResponse().containsHeader(name));
- assertEquals("Response header " + name, format.format(new Date(value)), result.getResponse().getHeader(name));
+ String formatted = format.format(new Date(value));
+ MockHttpServletResponse response = result.getResponse();
+ assertTrue("Response does not contain header " + name, response.containsHeader(name));
+ assertEquals("Response header " + name, formatted, response.getHeader(name));
}
};
}