MockRestRequestMatchersTests.java 6.5 KB
Newer Older
R
Rob Winch 已提交
1
/*
2
 * Copyright 2002-2018 the original author or authors.
R
Rob Winch 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
S
Sam Brannen 已提交
16

17
package org.springframework.test.web.client.match;
R
Rob Winch 已提交
18

19 20 21 22
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;

R
Rob Winch 已提交
23
import org.junit.Test;
24

R
Rob Winch 已提交
25 26
import org.springframework.http.HttpMethod;
import org.springframework.mock.http.client.MockClientHttpRequest;
27

28
import static org.hamcrest.MatcherAssert.assertThat;
29
import static org.hamcrest.Matchers.containsString;
30
import static org.junit.jupiter.api.Assertions.assertThrows;
R
Rob Winch 已提交
31 32

/**
S
Sam Brannen 已提交
33
 * Unit tests for {@link MockRestRequestMatchers}.
R
Rob Winch 已提交
34 35 36
 *
 * @author Craig Walls
 * @author Rossen Stoyanchev
37
 * @author Sam Brannen
R
Rob Winch 已提交
38
 */
39
public class MockRestRequestMatchersTests {
R
Rob Winch 已提交
40

S
Sam Brannen 已提交
41
	private final MockClientHttpRequest request = new MockClientHttpRequest();
R
Rob Winch 已提交
42 43 44 45 46 47


	@Test
	public void requestTo() throws Exception {
		this.request.setURI(new URI("http://foo.com/bar"));

48
		MockRestRequestMatchers.requestTo("http://foo.com/bar").match(this.request);
R
Rob Winch 已提交
49 50
	}

51
	@Test  // SPR-15819
52 53 54 55 56 57
	public void requestToUriTemplate() throws Exception {
		this.request.setURI(new URI("http://foo.com/bar"));

		MockRestRequestMatchers.requestToUriTemplate("http://foo.com/{bar}", "bar").match(this.request);
	}

58
	@Test
R
Rob Winch 已提交
59 60 61
	public void requestToNoMatch() throws Exception {
		this.request.setURI(new URI("http://foo.com/bar"));

62 63
		assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.requestTo("http://foo.com/wrong").match(this.request));
R
Rob Winch 已提交
64 65 66 67 68 69
	}

	@Test
	public void requestToContains() throws Exception {
		this.request.setURI(new URI("http://foo.com/bar"));

70
		MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request);
R
Rob Winch 已提交
71 72 73 74 75 76
	}

	@Test
	public void method() throws Exception {
		this.request.setMethod(HttpMethod.GET);

77
		MockRestRequestMatchers.method(HttpMethod.GET).match(this.request);
R
Rob Winch 已提交
78 79
	}

80
	@Test
R
Rob Winch 已提交
81 82 83
	public void methodNoMatch() throws Exception {
		this.request.setMethod(HttpMethod.POST);

84 85 86
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request));
		assertThat(error.getMessage(), containsString("expected:<GET> but was:<POST>"));
R
Rob Winch 已提交
87 88 89 90 91 92
	}

	@Test
	public void header() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

93
		MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
R
Rob Winch 已提交
94 95
	}

96
	@Test
R
Rob Winch 已提交
97
	public void headerMissing() throws Exception {
98 99 100
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.header("foo", "bar").match(this.request));
		assertThat(error.getMessage(), containsString("was null"));
R
Rob Winch 已提交
101 102
	}

103
	@Test
R
Rob Winch 已提交
104 105 106
	public void headerMissingValue() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

107 108 109
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.header("foo", "bad").match(this.request));
		assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>"));
R
Rob Winch 已提交
110 111 112 113 114 115
	}

	@Test
	public void headerContains() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

116
		MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request);
R
Rob Winch 已提交
117 118
	}

119
	@Test
R
Rob Winch 已提交
120
	public void headerContainsWithMissingHeader() throws Exception {
121 122 123
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request));
		assertThat(error.getMessage(), containsString("but was null"));
R
Rob Winch 已提交
124 125
	}

126
	@Test
R
Rob Winch 已提交
127 128 129
	public void headerContainsWithMissingValue() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

130 131 132
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request));
		assertThat(error.getMessage(), containsString("was \"bar\""));
R
Rob Winch 已提交
133 134 135 136 137 138
	}

	@Test
	public void headers() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

139
		MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
R
Rob Winch 已提交
140 141
	}

142
	@Test
R
Rob Winch 已提交
143
	public void headersWithMissingHeader() throws Exception {
144 145 146
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.header("foo", "bar").match(this.request));
		assertThat(error.getMessage(), containsString("but was null"));
R
Rob Winch 已提交
147 148
	}

149
	@Test
R
Rob Winch 已提交
150
	public void headersWithMissingValue() throws Exception {
151
		this.request.getHeaders().put("foo", Collections.singletonList("bar"));
R
Rob Winch 已提交
152

153 154 155
		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request));
		assertThat(error.getMessage(), containsString("to have at least <2> values"));
R
Rob Winch 已提交
156 157
	}

158
	@Test
159 160
	public void queryParam() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
161

162
		MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request);
163 164
	}

165
	@Test
166 167
	public void queryParamMissing() throws Exception {
		this.request.setURI(new URI("http://foo.com/a"));
168 169 170 171

		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request));
		assertThat(error.getMessage(), containsString("but was null"));
172 173
	}

174
	@Test
175 176
	public void queryParamMissingValue() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
177 178 179 180

		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request));
		assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>"));
181 182 183
	}

	@Test
184 185
	public void queryParamContains() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
186

187
		MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request);
188 189
	}

190
	@Test
191 192
	public void queryParamContainsWithMissingValue() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
193 194 195 196

		AssertionError error = assertThrows(AssertionError.class,
				() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request));
		assertThat(error.getMessage(), containsString("was \"bar\""));
197 198
	}

199
}