Skip to content

Commit

Permalink
Match multiple values in HeaderAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed May 13, 2020
1 parent e88eb0e commit 875e7f8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
Expand All @@ -17,6 +17,7 @@
package org.springframework.test.web.reactive.server;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import org.hamcrest.Matcher;
Expand All @@ -28,6 +29,7 @@
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.test.util.AssertionErrors;
import org.springframework.util.CollectionUtils;

/**
* Assertions on headers of the response.
Expand Down Expand Up @@ -70,6 +72,33 @@ public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
return this.responseSpec;
}

/**
* Match all values of the response header with the given regex
* patterns which are applied to the values of the header in the
* same order. Note that the number of pattenrs must match the
* number of actual values.
* @param name the header name
* @param patterns one or more regex patterns, one per expected value
* @since 5.3
*/
public WebTestClient.ResponseSpec valuesMatch(String name, String... patterns) {
this.exchangeResult.assertWithDiagnostics(() -> {
List<String> values = getRequiredValues(name);
AssertionErrors.assertTrue(
getMessage(name) + " has fewer or more values " + values +
" than number of patterns to match with " + Arrays.toString(patterns),
values.size() == patterns.length);
for (int i = 0; i < values.size(); i++) {
String value = values.get(i);
String pattern = patterns[i];
AssertionErrors.assertTrue(
getMessage(name) + "[" + i + "]='" + value + "' does not match '" + pattern + "'",
value.matches(pattern));
}
});
return this.responseSpec;
}

/**
* Assert the first value of the response header with a Hamcrest {@link Matcher}.
* @param name the header name
Expand All @@ -83,7 +112,19 @@ public WebTestClient.ResponseSpec value(String name, Matcher<? super String> mat
}

/**
* Consume the first value of the response header.
* Assert all values of the response header with a Hamcrest {@link Matcher}.
* @param name the header name
* @param matcher the matcher to use
* @since 5.3
*/
public WebTestClient.ResponseSpec values(String name, Matcher<Iterable<String>> matcher) {
List<String> values = getRequiredValues(name);
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat(values, matcher));
return this.responseSpec;
}

/**
* Consume the first value of the named response header.
* @param name the header name
* @param consumer the consumer to use
* @since 5.1
Expand All @@ -94,12 +135,28 @@ public WebTestClient.ResponseSpec value(String name, Consumer<String> consumer)
return this.responseSpec;
}

/**
* Consume all values of the named response header.
* @param name the header name
* @param consumer the consumer to use
* @since 5.3
*/
public WebTestClient.ResponseSpec values(String name, Consumer<List<String>> consumer) {
List<String> values = getRequiredValues(name);
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(values));
return this.responseSpec;
}

private String getRequiredValue(String name) {
String value = getHeaders().getFirst(name);
if (value == null) {
return getRequiredValues(name).get(0);
}

private List<String> getRequiredValues(String name) {
List<String> values = getHeaders().get(name);
if (CollectionUtils.isEmpty(values)) {
AssertionErrors.fail(getMessage(name) + " not found");
}
return value;
return values;
}

/**
Expand Down
Expand Up @@ -36,6 +36,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItems;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -97,11 +98,33 @@ public void valueMatches() {
assertions.valueMatches("Content-Type", ".*UTF-8.*");

// Wrong pattern
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
"'Content-Type'=[application/json;charset=UTF-8] does not match " +
"[.*ISO-8859-1.*]"));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
"'Content-Type'=[application/json;charset=UTF-8] does not match " +
"[.*ISO-8859-1.*]"));
}

@Test
public void valuesMatch() {
HttpHeaders headers = new HttpHeaders();
headers.add("foo", "value1");
headers.add("foo", "value2");
headers.add("foo", "value3");
HeaderAssertions assertions = headerAssertions(headers);

assertions.valuesMatch("foo", "val.*1", "val.*2", "val.*3");

assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
"Response header 'foo' has fewer or more values [value1, value2, value3] " +
"than number of patterns to match with [.*, val.*5]"));

assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5", ".*"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
"Response header 'foo'[1]='value2' does not match 'val.*5'"));
}

@Test
Expand All @@ -113,6 +136,16 @@ public void valueMatcher() {
assertions.value("foo", containsString("a"));
}

@Test
public void valuesMatcher() {
HttpHeaders headers = new HttpHeaders();
headers.add("foo", "bar");
headers.add("foo", "baz");
HeaderAssertions assertions = headerAssertions(headers);

assertions.values("foo", hasItems("bar", "baz"));
}

@Test
public void exists() {
HttpHeaders headers = new HttpHeaders();
Expand Down

0 comments on commit 875e7f8

Please sign in to comment.