Search in sources :

Example 6 with MockClientHttpRequest

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

the class TestRestTemplateTests method verifyRelativeUriHandling.

private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws IOException {
    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    MockClientHttpRequest request = new MockClientHttpRequest();
    request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
    URI absoluteUri = URI.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
    given(requestFactory.createRequest(eq(absoluteUri), (HttpMethod) any())).willReturn(request);
    RestTemplate delegate = new RestTemplate();
    TestRestTemplate template = new TestRestTemplate(delegate);
    delegate.setRequestFactory(requestFactory);
    LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
    template.setUriTemplateHandler(uriTemplateHandler);
    callback.doWithTestRestTemplate(template, URI.create("/a/b/c.txt?param=%7Bsomething%7D"));
    verify(requestFactory).createRequest(eq(absoluteUri), (HttpMethod) any());
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) InterceptingClientHttpRequestFactory(org.springframework.http.client.InterceptingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) CustomHttpComponentsClientHttpRequestFactory(org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory) MockEnvironment(org.springframework.mock.env.MockEnvironment) RestTemplate(org.springframework.web.client.RestTemplate) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) URI(java.net.URI) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 7 with MockClientHttpRequest

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

Example 8 with MockClientHttpRequest

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

the class ContentRequestMatchers method bytes.

/**
	 * Compare the body of the request to the given byte array.
	 */
public RequestMatcher bytes(final byte[] expectedContent) {
    return new RequestMatcher() {

        @Override
        public void match(ClientHttpRequest request) throws IOException, AssertionError {
            MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
            assertEquals("Request content", expectedContent, mockRequest.getBodyAsBytes());
        }
    };
}
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)

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