MockRestRequestMatchersTests.java 5.3 KB
Newer Older
R
Rob Winch 已提交
1
/*
2
 * Copyright 2002-2016 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.Matchers.containsString;
R
Rob Winch 已提交
29 30

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

S
Sam Brannen 已提交
38
	private final MockClientHttpRequest request = new MockClientHttpRequest();
R
Rob Winch 已提交
39 40 41 42 43 44


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

45
		MockRestRequestMatchers.requestTo("http://foo.com/bar").match(this.request);
R
Rob Winch 已提交
46 47
	}

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

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

S
Sam Brannen 已提交
55
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
56 57 58
	public void requestToNoMatch() throws Exception {
		this.request.setURI(new URI("http://foo.com/bar"));

59
		MockRestRequestMatchers.requestTo("http://foo.com/wrong").match(this.request);
R
Rob Winch 已提交
60 61 62 63 64 65
	}

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

66
		MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request);
R
Rob Winch 已提交
67 68 69 70 71 72
	}

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

73
		MockRestRequestMatchers.method(HttpMethod.GET).match(this.request);
R
Rob Winch 已提交
74 75
	}

S
Sam Brannen 已提交
76
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
77 78 79
	public void methodNoMatch() throws Exception {
		this.request.setMethod(HttpMethod.POST);

80
		MockRestRequestMatchers.method(HttpMethod.GET).match(this.request);
R
Rob Winch 已提交
81 82 83 84 85 86
	}

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

87
		MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
R
Rob Winch 已提交
88 89
	}

S
Sam Brannen 已提交
90
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
91
	public void headerMissing() throws Exception {
92
		MockRestRequestMatchers.header("foo", "bar").match(this.request);
R
Rob Winch 已提交
93 94
	}

S
Sam Brannen 已提交
95
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
96 97 98
	public void headerMissingValue() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

99
		MockRestRequestMatchers.header("foo", "bad").match(this.request);
R
Rob Winch 已提交
100 101 102 103 104 105
	}

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

106
		MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request);
R
Rob Winch 已提交
107 108
	}

S
Sam Brannen 已提交
109
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
110
	public void headerContainsWithMissingHeader() throws Exception {
111
		MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request);
R
Rob Winch 已提交
112 113
	}

S
Sam Brannen 已提交
114
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
115 116 117
	public void headerContainsWithMissingValue() throws Exception {
		this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));

118
		MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request);
R
Rob Winch 已提交
119 120 121 122 123 124
	}

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

125
		MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
R
Rob Winch 已提交
126 127
	}

S
Sam Brannen 已提交
128
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
129
	public void headersWithMissingHeader() throws Exception {
130
		MockRestRequestMatchers.header("foo", "bar").match(this.request);
R
Rob Winch 已提交
131 132
	}

S
Sam Brannen 已提交
133
	@Test(expected = AssertionError.class)
R
Rob Winch 已提交
134
	public void headersWithMissingValue() throws Exception {
135
		this.request.getHeaders().put("foo", Collections.singletonList("bar"));
R
Rob Winch 已提交
136

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

140
	@Test
141 142 143
	public void queryParam() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
		MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request);
144 145 146
	}

	@Test(expected = AssertionError.class)
147 148 149
	public void queryParamMissing() throws Exception {
		this.request.setURI(new URI("http://foo.com/a"));
		MockRestRequestMatchers.queryParam("foo", "bar").match(this.request);
150 151 152
	}

	@Test(expected = AssertionError.class)
153 154 155
	public void queryParamMissingValue() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
		MockRestRequestMatchers.queryParam("foo", "bad").match(this.request);
156 157 158
	}

	@Test
159 160 161
	public void queryParamContains() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
		MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request);
162 163 164
	}

	@Test(expected = AssertionError.class)
165 166 167
	public void queryParamContainsWithMissingValue() throws Exception {
		this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
		MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request);
168 169
	}

R
Rob Winch 已提交
170
}