Search in sources :

Example 6 with ClientHttpRequest

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

use of org.springframework.http.client.ClientHttpRequest 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 8 with ClientHttpRequest

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

the class EndpointWebMvcAutoConfigurationTests method assertContent.

private void assertContent(String scheme, String url, int port, Object expected) throws Exception {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    ClientHttpRequest request = requestFactory.createRequest(new URI(scheme + "://localhost:" + port + url), HttpMethod.GET);
    try {
        ClientHttpResponse response = request.execute();
        if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
            throw new FileNotFoundException();
        }
        try {
            String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
            if (expected instanceof Matcher) {
                assertThat(actual).is(Matched.by((Matcher<?>) expected));
            } else {
                assertThat(actual).isEqualTo(expected);
            }
        } finally {
            response.close();
        }
    } catch (Exception ex) {
        if (expected == null) {
            if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) {
                return;
            }
        }
        throw ex;
    }
}
Also used : Matcher(org.hamcrest.Matcher) HttpClient(org.apache.http.client.HttpClient) FileNotFoundException(java.io.FileNotFoundException) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) FileNotFoundException(java.io.FileNotFoundException) WebServerException(org.springframework.boot.web.server.WebServerException) SocketException(java.net.SocketException) ExpectedException(org.junit.rules.ExpectedException)

Example 9 with ClientHttpRequest

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

the class EndpointWebMvcAutoConfigurationTests method hasHeader.

public boolean hasHeader(String url, int port, String header) throws Exception {
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI("http://localhost:" + port + url), HttpMethod.GET);
    ClientHttpResponse response = request.execute();
    return response.getHeaders().containsKey(header);
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 10 with ClientHttpRequest

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

the class BasicAuthorizationInterceptorTests method interceptShouldAddHeader.

@Test
public void interceptShouldAddHeader() throws Exception {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
    byte[] body = new byte[] {};
    new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, execution);
    verify(execution).execute(request, body);
    assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization"));
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Aggregations

ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)36 URI (java.net.URI)19 Test (org.junit.Test)13 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)13 IOException (java.io.IOException)11 HttpMethod (org.springframework.http.HttpMethod)9 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)9 HttpHeaders (org.springframework.http.HttpHeaders)6 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)6 RequestMatcher (org.springframework.test.web.client.RequestMatcher)6 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)4 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 MediaType (org.springframework.http.MediaType)2 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)2 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)2