Search in sources :

Example 1 with RequestMatcher

use of org.springframework.test.web.client.RequestMatcher in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method expectRequestShouldDelegateToExpectationManager.

@Test
public void expectRequestShouldDelegateToExpectationManager() throws Exception {
    ExpectedCount count = mock(ExpectedCount.class);
    RequestMatcher requestMatcher = mock(RequestMatcher.class);
    this.manager.expectRequest(count, requestMatcher);
    verify(this.delegate).expectRequest(count, requestMatcher);
}
Also used : RequestMatcher(org.springframework.test.web.client.RequestMatcher) ExpectedCount(org.springframework.test.web.client.ExpectedCount) Test(org.junit.Test)

Example 2 with RequestMatcher

use of org.springframework.test.web.client.RequestMatcher in project spring-framework by spring-projects.

the class ContentRequestMatchers method string.

/**
	 * 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 new RequestMatcher() {

        @Override
        public void match(ClientHttpRequest request) throws IOException, AssertionError {
            MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
            assertEquals("Request content", expectedContent, mockRequest.getBodyAsString());
        }
    };
}
Also used : RequestMatcher(org.springframework.test.web.client.RequestMatcher) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 3 with RequestMatcher

use of org.springframework.test.web.client.RequestMatcher in project spring-framework by spring-projects.

the class ContentRequestMatchers method string.

/**
	 * 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);
        }
    };
}
Also used : RequestMatcher(org.springframework.test.web.client.RequestMatcher) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 4 with RequestMatcher

use of org.springframework.test.web.client.RequestMatcher in project spring-framework by spring-projects.

the class ContentRequestMatchers method contentTypeCompatibleWith.

/**
	 * 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 new RequestMatcher() {

        @Override
        public void match(ClientHttpRequest request) throws IOException, AssertionError {
            MediaType actualContentType = request.getHeaders().getContentType();
            assertTrue("Content type not set", actualContentType != null);
            assertTrue("Content type [" + actualContentType + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType));
        }
    };
}
Also used : RequestMatcher(org.springframework.test.web.client.RequestMatcher) MediaType(org.springframework.http.MediaType) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 5 with RequestMatcher

use of org.springframework.test.web.client.RequestMatcher in project spring-framework by spring-projects.

the class ContentRequestMatchers method formData.

/**
	 * 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 new RequestMatcher() {

        @Override
        public void match(final ClientHttpRequest request) throws IOException, AssertionError {
            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));
        }
    };
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) RequestMatcher(org.springframework.test.web.client.RequestMatcher) ByteArrayInputStream(java.io.ByteArrayInputStream) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Aggregations

RequestMatcher (org.springframework.test.web.client.RequestMatcher)7 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)6 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)6 MediaType (org.springframework.http.MediaType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Test (org.junit.Test)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 FormHttpMessageConverter (org.springframework.http.converter.FormHttpMessageConverter)1 ExpectedCount (org.springframework.test.web.client.ExpectedCount)1