Search in sources :

Example 1 with MockClientHttpRequest

use of org.springframework.mock.http.client.MockClientHttpRequest in project spring-boot by spring-projects.

the class ClassPathChangeUploaderTests method sendsClassLoaderFiles.

@Test
public void sendsClassLoaderFiles() throws Exception {
    File sourceFolder = this.temp.newFolder();
    ClassPathChangedEvent event = createClassPathChangedEvent(sourceFolder);
    this.requestFactory.willRespond(HttpStatus.OK);
    this.uploader.onApplicationEvent(event);
    assertThat(this.requestFactory.getExecutedRequests()).hasSize(1);
    MockClientHttpRequest request = this.requestFactory.getExecutedRequests().get(0);
    verifyUploadRequest(sourceFolder, request);
}
Also used : ClassPathChangedEvent(org.springframework.boot.devtools.classpath.ClassPathChangedEvent) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) File(java.io.File) ChangedFile(org.springframework.boot.devtools.filewatch.ChangedFile) Test(org.junit.Test)

Example 2 with MockClientHttpRequest

use of org.springframework.mock.http.client.MockClientHttpRequest in project spring-framework by spring-projects.

the class MockMvcClientHttpRequestFactory method createRequest.

@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    return new MockClientHttpRequest(httpMethod, uri) {

        @Override
        public ClientHttpResponse executeInternal() throws IOException {
            try {
                MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
                requestBuilder.content(getBodyAsBytes());
                requestBuilder.headers(getHeaders());
                MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
                MockHttpServletResponse servletResponse = mvcResult.getResponse();
                HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
                byte[] body = servletResponse.getContentAsByteArray();
                HttpHeaders headers = getResponseHeaders(servletResponse);
                MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
                clientResponse.getHeaders().putAll(headers);
                return clientResponse;
            } catch (Exception ex) {
                byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
                return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IOException(java.io.IOException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 3 with MockClientHttpRequest

use of org.springframework.mock.http.client.MockClientHttpRequest 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 4 with MockClientHttpRequest

use of org.springframework.mock.http.client.MockClientHttpRequest 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 5 with MockClientHttpRequest

use of org.springframework.mock.http.client.MockClientHttpRequest in project spring-framework by spring-projects.

the class XpathRequestMatchersTests method setUp.

@Before
public void setUp() throws IOException {
    this.request = new MockClientHttpRequest();
    this.request.getBody().write(RESPONSE_CONTENT.getBytes());
}
Also used : MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) Before(org.junit.Before)

Aggregations

MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)8 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)4 RequestMatcher (org.springframework.test.web.client.RequestMatcher)4 MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ClassPathChangedEvent (org.springframework.boot.devtools.classpath.ClassPathChangedEvent)1 ChangedFile (org.springframework.boot.devtools.filewatch.ChangedFile)1 ClassLoaderFile (org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)1 CustomHttpComponentsClientHttpRequestFactory (org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)1 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)1 InterceptingClientHttpRequestFactory (org.springframework.http.client.InterceptingClientHttpRequestFactory)1