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

The following examples show how to use org.springframework.mock.http.client.MockClientHttpRequest. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: ContentRequestMatchers.java    License: 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 Project: java-technology-stack   Source File: ContentRequestMatchers.java    License: 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
@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 4
/**
 * 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 Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: 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 6
Source Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: 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 7
Source Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: 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 8
/**
 * 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 9
Source Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert the String content found with the given matcher.
 */
public <T> RequestMatcher string(final Matcher<? super String> matcher) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
		}
	};
}
 
Example 10
Source Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: 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 Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: 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 Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: MIT License 5 votes vote down vote up
/**
 * Apply the XPath and assert the number of nodes found.
 */
public RequestMatcher number(final Double value) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertNumber(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
		}
	};
}
 
Example 13
Source Project: spring-analysis-note   Source File: XpathRequestMatchers.java    License: 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
/**
 * Get the body of the request as a UTF-8 string and appply the given {@link Matcher}.
 */
public RequestMatcher string(final Matcher<? super String> matcher) {
	return new RequestMatcher() {
		@Override
		public void match(ClientHttpRequest request) throws IOException, AssertionError {
			MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
			assertThat("Request content", mockRequest.getBodyAsString(), matcher);
		}
	};
}
 
Example 15
private RequestMatcher aqlContent(String buildName, String buildNumber) {
	return (request) -> {
		String body = ((MockClientHttpRequest) request).getBodyAsString();
		Matcher matcher = AQL_PATTERN.matcher(body);
		assertThat(matcher.matches()).isTrue();
		String actualJson = matcher.group(1);
		String expectedJson = "{\"@build.name\": \"" + buildName + "\", \"@build.number\": \"" + buildNumber
				+ "\"}";
		assertJson(expectedJson, actualJson);
	};
}
 
Example 16
Source Project: spring-analysis-note   Source File: ContentRequestMatchers.java    License: MIT License 5 votes vote down vote up
/**
 * Get the body of the request as a UTF-8 string and apply the given {@link Matcher}.
 */
public RequestMatcher string(final Matcher<? super String> matcher) {
	return request -> {
		MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
		assertThat("Request content", mockRequest.getBodyAsString(), matcher);
	};
}
 
Example 17
Source Project: spring-analysis-note   Source File: ContentRequestMatchers.java    License: 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 18
Source Project: spring-analysis-note   Source File: ContentRequestMatchers.java    License: 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 Project: spring-analysis-note   Source File: ContentRequestMatchers.java    License: 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 20
private HttpRequest invokeInterceptors(RestOperations restOperations)
		throws IOException {
	assertThat(restOperations).isInstanceOf(RestTemplate.class);
	RestTemplate restTemplate = (RestTemplate) restOperations;

	MockClientHttpRequest request = new MockClientHttpRequest();
	ClientHttpRequestExecution execution = Mockito
			.mock(ClientHttpRequestExecution.class);
	byte[] body = new byte[] {};
	for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
		interceptor.intercept(request, body, execution);
	}
	return request;
}
 
Example 21
/**
 * 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 22
private RequestMatcher matchContents(
		@SuppressWarnings("rawtypes") final ContentPattern pattern) {
	return new RequestMatcher() {
		@Override
		public void match(ClientHttpRequest request)
				throws IOException, AssertionError {
			MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
			@SuppressWarnings("unchecked")
			MatchResult result = pattern.match(mockRequest.getBodyAsString());
			MatcherAssert.assertThat(
					"Request as string [" + mockRequest.getBodyAsString() + "]",
					result.isExactMatch());
		}
	};
}
 
Example 23
@Override
public final void match(ClientHttpRequest request) throws IOException, AssertionError {
	try {
		MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
		matchInternal(mockRequest);
	}
	catch (Exception e) {
		throw new AssertionError("Failed to parse XML request content: " + e.getMessage());
	}
}
 
Example 24
/**
 * Apply the XPath and assert the String content found with the given matcher.
 */
public <T> RequestMatcher string(final Matcher<? super String> matcher) {
	return new AbstractXpathRequestMatcher() {
		@Override
		protected void matchInternal(MockClientHttpRequest request) throws Exception {
			xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
		}
	};
}
 
Example 25
Source Project: spring-analysis-note   Source File: JsonPathRequestMatchers.java    License: 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 26
Source Project: spring-analysis-note   Source File: JsonPathRequestMatchers.java    License: 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 27
Source Project: spring-analysis-note   Source File: JsonPathRequestMatchers.java    License: 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 28
private ClientHttpRequest createRequest(HttpMethod method, String url) {
	try {
		return new MockClientHttpRequest(method, new URI(url));
	}
	catch (URISyntaxException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example 29
Source Project: spring-analysis-note   Source File: JsonPathRequestMatchers.java    License: 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 30
Source Project: spring-analysis-note   Source File: JsonPathRequestMatchers.java    License: 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());
		}
	};
}