org.springframework.mock.http.client.MockClientHttpRequest Java Examples

The following examples show how to use org.springframework.mock.http.client.MockClientHttpRequest. 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 spring4-understanding with Apache License 2.0 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 #4
Source File: JsonPathRequestMatchers.java    From spring4-understanding with Apache License 2.0 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 #5
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 #6
Source File: ContentRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Parse the request content as {@link Node} and apply the given {@link Matcher}.
 */
public RequestMatcher node(final Matcher<? super Node> matcher) {
	return new AbstractXmlRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xmlHelper.assertNode(request.getBodyAsString(), matcher);
		}
	};
}
 
Example #7
Source File: XpathRequestMatchers.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Apply the XPath and assert the number of nodes found.
 */
public <T> RequestMatcher nodeCount(final int expectedCount) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, expectedCount);
		}
	};
}
 
Example #8
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 #9
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 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 #10
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 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 #11
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 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 #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 an array.
 */
public RequestMatcher isArray() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsArray(request.getBodyAsString());
		}
	};
}
 
Example #13
Source File: JsonPathRequestMatchers.java    From spring4-understanding with Apache License 2.0 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 #14
Source File: JsonPathRequestMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public final void match(ClientHttpRequest request) throws IOException, AssertionError {
	try {
		MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
		matchInternal(mockRequest);
	}
	catch (ParseException ex) {
		throw new AssertionError("Failed to parse JSON request content", ex);
	}
}
 
Example #15
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 #16
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 #17
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 #18
Source File: XpathRequestMatchers.java    From spring4-understanding with Apache License 2.0 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 #19
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 #20
Source File: ConfigServicePropertySourceLocatorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void interceptorShouldAddHeadersWhenHeadersPropertySet() throws Exception {
	MockClientHttpRequest request = new MockClientHttpRequest();
	ClientHttpRequestExecution execution = Mockito
			.mock(ClientHttpRequestExecution.class);
	byte[] body = new byte[] {};
	Map<String, String> headers = new HashMap<>();
	headers.put("X-Example-Version", "2.1");
	new ConfigServicePropertySourceLocator.GenericRequestHeaderInterceptor(headers)
			.intercept(request, body, execution);
	Mockito.verify(execution).execute(request, body);
	assertThat(request.getHeaders().getFirst("X-Example-Version")).isEqualTo("2.1");
}
 
Example #21
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.
 */
public <T> RequestMatcher nodeCount(final int expectedCount) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, expectedCount);
		}
	};
}
 
Example #22
Source File: XpathRequestMatchers.java    From spring4-understanding with Apache License 2.0 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 #23
Source File: ContentRequestMatchers.java    From spring4-understanding with Apache License 2.0 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 #24
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 #25
Source File: MockMvcClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) {
	return new MockClientHttpRequest(httpMethod, uri) {
		@Override
		public ClientHttpResponse executeInternal() throws IOException {
			return getClientHttpResponse(httpMethod, uri, getHeaders(), getBodyAsBytes());
		}
	};
}
 
Example #26
Source File: ContentRequestMatchers.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the request content as {@link Node} and apply the given {@link Matcher}.
 */
public RequestMatcher node(final Matcher<? super Node> matcher) {
	return new AbstractXmlRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xmlHelper.assertNode(request.getBodyAsString(), matcher);
		}
	};
}
 
Example #27
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 #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 that the result is an array.
 */
public RequestMatcher isArray() {
	return new AbstractJsonPathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
			JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsArray(request.getBodyAsString());
		}
	};
}
 
Example #29
Source File: SimpleRequestExpectationManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ClientHttpRequest createRequest(HttpMethod method, String url) {
	try {
		return new MockClientHttpRequest(method, new URI(url));
	}
	catch (URISyntaxException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #30
Source File: XpathRequestMatchers.java    From spring4-understanding with Apache License 2.0 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);
		}
	};
}