Search in sources :

Example 16 with ProtocolVersion

use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.

the class TestAsyncRequestContext method testRequestContext.

@Test
public void testRequestContext() throws Exception {
    final AtomicReference<ProtocolVersion> versionRef = new AtomicReference<>();
    clientBuilder.addRequestInterceptorFirst((request, entity, context) -> versionRef.set(context.getProtocolVersion()));
    final HttpHost target = start();
    final Future<SimpleHttpResponse> future = httpclient.execute(SimpleRequestBuilder.get().setHttpHost(target).setPath("/random/2048").build(), null);
    final SimpleHttpResponse response = future.get();
    assertThat(response, CoreMatchers.notNullValue());
    assertThat(response.getCode(), CoreMatchers.equalTo(200));
    final String body = response.getBodyText();
    assertThat(body, CoreMatchers.notNullValue());
    assertThat(body.length(), CoreMatchers.equalTo(2048));
    assertThat(versionRef.get(), CoreMatchers.equalTo(version));
}
Also used : HttpHost(org.apache.hc.core5.http.HttpHost) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) Test(org.junit.Test)

Example 17 with ProtocolVersion

use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.

the class TestProtocolRequirements method testRequestsWithLowerProtocolVersionsGetUpgradedTo1_1.

/*
     * "Due to interoperability problems with HTTP/1.0 proxies discovered since
     * the publication of RFC 2068[33], caching proxies MUST, gateways MAY, and
     * tunnels MUST NOT upgrade the request to the highest version they support.
     * The proxy/gateway's response to that request MUST be in the same major
     * version as the request."
     *
     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.1
     */
@Test
public void testRequestsWithLowerProtocolVersionsGetUpgradedTo1_1() throws Exception {
    request = new BasicClassicHttpRequest("GET", "/foo");
    request.setVersion(new ProtocolVersion("HTTP", 1, 0));
    final ClassicHttpRequest upgraded = new BasicClassicHttpRequest("GET", "/foo");
    upgraded.setVersion(HttpVersion.HTTP_1_1);
    Mockito.when(mockExecChain.proceed(RequestEquivalent.eq(upgraded), Mockito.any())).thenReturn(originResponse);
    final ClassicHttpResponse result = execute(request);
    Assertions.assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result));
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) Test(org.junit.jupiter.api.Test)

Example 18 with ProtocolVersion

use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.

the class TestProtocolRequirements method testResponseToA1_0RequestShouldUse1_1.

@Test
public void testResponseToA1_0RequestShouldUse1_1() throws Exception {
    request = new BasicClassicHttpRequest("GET", "/foo");
    request.setVersion(new ProtocolVersion("HTTP", 1, 0));
    Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(originResponse);
    final ClassicHttpResponse result = execute(request);
    Assertions.assertEquals(HttpVersion.HTTP_1_1, result.getVersion());
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) Test(org.junit.jupiter.api.Test)

Example 19 with ProtocolVersion

use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.

the class TestProtocolRequirements method testHigherMajorProtocolVersionsOnRequestSwitchToTunnelBehavior.

/*
     * "Proxy and gateway applications need to be careful when forwarding
     * messages in protocol versions different from that of the application.
     * Since the protocol version indicates the protocol capability of the
     * sender, a proxy/gateway MUST NOT send a message with a version indicator
     * which is greater than its actual version. If a higher version request is
     * received, the proxy/gateway MUST either downgrade the request version, or
     * respond with an error, or switch to tunnel behavior."
     *
     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.1
     */
@Test
public void testHigherMajorProtocolVersionsOnRequestSwitchToTunnelBehavior() throws Exception {
    // tunnel behavior: I don't muck with request or response in
    // any way
    request = new BasicClassicHttpRequest("GET", "/foo");
    request.setVersion(new ProtocolVersion("HTTP", 2, 13));
    Mockito.when(mockExecChain.proceed(RequestEquivalent.eq(request), Mockito.any())).thenReturn(originResponse);
    final ClassicHttpResponse result = execute(request);
    Assertions.assertSame(originResponse, result);
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) Test(org.junit.jupiter.api.Test)

Example 20 with ProtocolVersion

use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.

the class TestProtocolRequirements method test100ContinueResponsesAreNotForwardedTo1_0ClientsWhoDidNotAskForThem.

/*
     * "A proxy MUST NOT forward a 100 (Continue) response if the request
     * message was received from an HTTP/1.0 (or earlier) client and did not
     * include an Expect request-header field with the '100-continue'
     * expectation. This requirement overrides the general rule for forwarding
     * of 1xx responses (see section 10.1)."
     *
     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
     */
@Test
public void test100ContinueResponsesAreNotForwardedTo1_0ClientsWhoDidNotAskForThem() throws Exception {
    final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
    post.setVersion(new ProtocolVersion("HTTP", 1, 0));
    post.setEntity(body);
    post.setHeader("Content-Length", "128");
    originResponse = new BasicClassicHttpResponse(100, "Continue");
    Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(originResponse);
    // if a 100 response gets up to us from the HttpClient
    // backend, we can't really handle it at that point
    Assertions.assertThrows(ClientProtocolException.class, () -> execute(post));
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)55 Test (org.junit.jupiter.api.Test)12 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)10 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)9 ProtocolException (org.apache.hc.core5.http.ProtocolException)8 UnsupportedHttpVersionException (org.apache.hc.core5.http.UnsupportedHttpVersionException)8 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)8 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)7 Header (org.apache.hc.core5.http.Header)6 HttpException (org.apache.hc.core5.http.HttpException)6 HttpHost (org.apache.hc.core5.http.HttpHost)5 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HeaderElement (org.apache.hc.core5.http.HeaderElement)3 ParseException (org.apache.hc.core5.http.ParseException)3 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3