org.springframework.test.web.client.RequestMatcher Java Examples

The following examples show how to use org.springframework.test.web.client.RequestMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Parse the body as form data and compare to the given {@code MultiValueMap}.
 * @since 4.3
 */
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
	return request -> {
		HttpInputMessage inputMessage = new HttpInputMessage() {
			@Override
			public InputStream getBody() throws IOException {
				MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
				return new ByteArrayInputStream(mockRequest.getBodyAsBytes());
			}
			@Override
			public HttpHeaders getHeaders() {
				return request.getHeaders();
			}
		};
		FormHttpMessageConverter converter = new FormHttpMessageConverter();
		assertEquals("Request content", expectedContent, converter.read(null, inputMessage));
	};
}
 
Example #2
Source File: ContentRequestMatchers.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Parse the body as form data and compare to the given {@code MultiValueMap}.
 * @since 4.3
 */
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
	return request -> {
		HttpInputMessage inputMessage = new HttpInputMessage() {
			@Override
			public InputStream getBody() throws IOException {
				MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
				return new ByteArrayInputStream(mockRequest.getBodyAsBytes());
			}
			@Override
			public HttpHeaders getHeaders() {
				return request.getHeaders();
			}
		};
		FormHttpMessageConverter converter = new FormHttpMessageConverter();
		assertEquals("Request content", expectedContent, converter.read(null, inputMessage));
	};
}
 
Example #3
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is a {@link Number}.
 * @since 4.2.1
 */
public RequestMatcher isNumber() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsNumber(request.getBodyAsString());
		}
	};
}
 
Example #4
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert the number of nodes found with the given
 * {@code Matcher<Integer>}.
 */
public <T> RequestMatcher nodeCount(final Matcher<Integer> matcher) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
		}
	};
}
 
Example #5
Source File: MockRestRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Assert request header values with the given Hamcrest matcher(s).
 */
@SafeVarargs
public static RequestMatcher header(final String name, final Matcher<? super String>... matchers) {
	return request -> {
		assertValueCount("header", name, request.getHeaders(), matchers.length);
		List<String> headerValues = request.getHeaders().get(name);
		Assert.state(headerValues != null, "No header values");
		for (int i = 0; i < matchers.length; i++) {
			assertThat("Request header [" + name + "]", headerValues.get(i), matchers[i]);
		}
	};
}
 
Example #6
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert that content exists at the given XPath.
 */
public <T> RequestMatcher exists() {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.exists(request.getBodyAsBytes(), DEFAULT_ENCODING);
		}
	};
}
 
Example #7
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert it with the given {@code Matcher<Node>}.
 */
public <T> RequestMatcher node(final Matcher<? super Node> matcher) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNode(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
		}
	};
}
 
Example #8
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is a {@link java.util.Map}.
 * @since 4.2.1
 */
public RequestMatcher isMap() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsMap(request.getBodyAsString());
		}
	};
}
 
Example #9
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert that content does not exist at the given XPath.
 */
public <T> RequestMatcher doesNotExist() {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.doesNotExist(request.getBodyAsBytes(), DEFAULT_ENCODING);
		}
	};
}
 
Example #10
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert the String content found.
 */
public RequestMatcher string(final String value) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
		}
	};
}
 
Example #11
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert the number found with the given matcher.
 */
public <T> RequestMatcher number(final Matcher<? super Double> matcher) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNumber(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
		}
	};
}
 
Example #12
Source File: JsonPathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is equal to the supplied value.
 */
public RequestMatcher value(final Object expectedValue) {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValue(request.getBodyAsString(), expectedValue);
		}
	};
}
 
Example #13
Source File: XpathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert the boolean value found.
 */
public <T> RequestMatcher booleanValue(final Boolean value) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertBoolean(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
		}
	};
}
 
Example #14
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert the request content type as a {@link MediaType}.
 */
public RequestMatcher contentType(final MediaType expectedContentType) {
	return request -> {
		MediaType actualContentType = request.getHeaders().getContentType();
		assertTrue("Content type not set", actualContentType != null);
		assertEquals("Content type", expectedContentType, actualContentType);
	};
}
 
Example #15
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Get the body of the request as a UTF-8 string and compare it to the given String.
 */
public RequestMatcher string(final String expectedContent) {
	return request -> {
		MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
		assertEquals("Request content", expectedContent, mockRequest.getBodyAsString());
	};
}
 
Example #16
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert the request content type is compatible with the given
 * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
 */
public RequestMatcher contentTypeCompatibleWith(final MediaType contentType) {
	return request -> {
		MediaType actualContentType = request.getHeaders().getContentType();
		assertTrue("Content type not set", actualContentType != null);
		if (actualContentType != null) {
			assertTrue("Content type [" + actualContentType + "] is not compatible with [" + contentType + "]",
					actualContentType.isCompatibleWith(contentType));
		}
	};
}
 
Example #17
Source File: JsonPathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * An overloaded variant of {@link #value(Matcher)} that also accepts a
 * target type for the resulting value that the matcher can work reliably
 * against.
 * <p>This can be useful for matching numbers reliably &mdash; for example,
 * to coerce an integer into a double.
 * @since 4.3.3
 */
public <T> RequestMatcher value(final Matcher<T> matcher, final Class<T> targetType) {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			String body = request.getBodyAsString();
			JsonPathRequestMatchers.this.jsonPathHelper.assertValue(body, matcher, targetType);
		}
	};
}
 
Example #18
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Compare the body of the request to the given byte array.
 */
public RequestMatcher bytes(final byte[] expectedContent) {
	return request -> {
		MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
		assertEquals("Request content", expectedContent, mockRequest.getBodyAsBytes());
	};
}
 
Example #19
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is a {@link Boolean}.
 * @since 4.2.1
 */
public RequestMatcher isBoolean() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsBoolean(request.getBodyAsString());
		}
	};
}
 
Example #20
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is a {@link String}.
 * @since 4.2.1
 */
public RequestMatcher isString() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsString(request.getBodyAsString());
		}
	};
}
 
Example #21
Source File: XpathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert it with the given {@code Matcher<Node>}.
 */
public <T> RequestMatcher node(final Matcher<? super Node> matcher) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNode(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
		}
	};
}
 
Example #22
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Parse the request body and the given String as XML and assert that the
 * two are "similar" - i.e. they contain the same elements and attributes
 * regardless of order.
 * <p>Use of this matcher assumes the
 * <a href="http://xmlunit.sourceforge.net/">XMLUnit</a> library is available.
 * @param expectedXmlContent the expected XML content
 */
public RequestMatcher xml(final String expectedXmlContent) {
	return new AbstractXmlRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xmlHelper.assertXmlEqual(expectedXmlContent, request.getBodyAsString());
		}
	};
}
 
Example #23
Source File: JsonPathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is a {@link java.util.Map}.
 * @since 4.2.1
 */
public RequestMatcher isMap() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsMap(request.getBodyAsString());
		}
	};
}
 
Example #24
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that a value does not exist at the given path.
 * <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
 * definite}, this method asserts that the value at the given path is
 * <em>empty</em>.
 */
public RequestMatcher doesNotExist() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.doesNotExist(request.getBodyAsString());
		}
	};
}
 
Example #25
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that a non-null value exists at the given path.
 * <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
 * definite}, this method asserts that the value at the given path is not
 * <em>empty</em>.
 */
public RequestMatcher exists() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.exists(request.getBodyAsString());
		}
	};
}
 
Example #26
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert that the result is equal to the supplied value.
 */
public RequestMatcher value(final Object expectedValue) {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValue(request.getBodyAsString(), expectedValue);
		}
	};
}
 
Example #27
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * An overloaded variant of {@link #value(Matcher)} that also accepts a
 * target type for the resulting value that the matcher can work reliably
 * against.
 * <p>This can be useful for matching numbers reliably &mdash; for example,
 * to coerce an integer into a double.
 * @since 4.3.3
 */
public <T> RequestMatcher value(final Matcher<T> matcher, final Class<T> targetType) {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			String body = request.getBodyAsString();
			JsonPathRequestMatchers.this.jsonPathHelper.assertValue(body, matcher, targetType);
		}
	};
}
 
Example #28
Source File: JsonPathRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the JSON path expression against the request content and
 * assert the resulting value with the given Hamcrest {@link Matcher}.
 */
public <T> RequestMatcher value(final Matcher<T> matcher) {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValue(request.getBodyAsString(), matcher);
		}
	};
}
 
Example #29
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Parse the request content as {@link DOMSource} and apply the given {@link Matcher}.
 * @see <a href="http://code.google.com/p/xml-matchers/">http://code.google.com/p/xml-matchers/</a>
 */
public RequestMatcher source(final Matcher<? super Source> matcher) {
	return new AbstractXmlRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xmlHelper.assertSource(request.getBodyAsString(), matcher);
		}
	};
}
 
Example #30
Source File: MockRestRequestMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Assert request header values.
 */
public static RequestMatcher header(final String name, final String... expectedValues) {
	return request -> {
		assertValueCount("header", name, request.getHeaders(), expectedValues.length);
		List<String> headerValues = request.getHeaders().get(name);
		Assert.state(headerValues != null, "No header values");
		for (int i = 0; i < expectedValues.length; i++) {
			assertEquals("Request header [" + name + "]", expectedValues[i], headerValues.get(i));
		}
	};
}