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));
}
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));
}
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());
}
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);
}
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));
}
Aggregations