use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class Http1IntegrationTest method testAbsentHostHeader.
@Test
public void testAbsentHostHeader() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final InetSocketAddress serverEndpoint = server.start();
client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, RequestConnControl.INSTANCE), Http1Config.DEFAULT);
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/hello"));
request1.setVersion(HttpVersion.HTTP_1_0);
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
Assertions.assertEquals("Hi there", result1.getBody());
final HttpRequest request2 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/hello"));
request2.setVersion(HttpVersion.HTTP_1_1);
final Future<Message<HttpResponse, String>> future2 = streamEndpoint.execute(new BasicRequestProducer(request2, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result2 = future2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result2);
final HttpResponse response2 = result2.getHead();
Assertions.assertNotNull(response2);
Assertions.assertEquals(400, response2.getCode());
Assertions.assertEquals("Host header is absent", result2.getBody());
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class BasicRequestBuilder method build.
@Override
public BasicHttpRequest build() {
String path = getPath();
final List<NameValuePair> parameters = getParameters();
if (parameters != null && !parameters.isEmpty()) {
try {
final URI uri = new URIBuilder(path).setCharset(getCharset()).addParameters(parameters).build();
path = uri.toASCIIString();
} catch (final URISyntaxException ex) {
// should never happen
}
}
final BasicHttpRequest result = new BasicHttpRequest(getMethod(), getScheme(), getAuthority(), path);
result.setVersion(getVersion());
result.setHeaders(getHeaders());
result.setAbsoluteRequestUri(isAbsoluteRequestUri());
return result;
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class AsyncRequestBuilder method build.
@Override
public AsyncRequestProducer build() {
String path = getPath();
if (TextUtils.isEmpty(path)) {
path = "/";
}
AsyncEntityProducer entityProducerCopy = entityProducer;
final String method = getMethod();
final List<NameValuePair> parameters = getParameters();
if (parameters != null && !parameters.isEmpty()) {
final Charset charset = getCharset();
if (entityProducerCopy == null && (Method.POST.isSame(method) || Method.PUT.isSame(method))) {
final String content = WWWFormCodec.format(parameters, charset != null ? charset : ContentType.APPLICATION_FORM_URLENCODED.getCharset());
entityProducerCopy = new StringAsyncEntityProducer(content, ContentType.APPLICATION_FORM_URLENCODED);
} else {
try {
final URI uri = new URIBuilder(path).setCharset(charset).addParameters(parameters).build();
path = uri.toASCIIString();
} catch (final URISyntaxException ex) {
// should never happen
}
}
}
if (entityProducerCopy != null && Method.TRACE.isSame(method)) {
throw new IllegalStateException(Method.TRACE + " requests may not include an entity");
}
final BasicHttpRequest request = new BasicHttpRequest(method, getScheme(), getAuthority(), path);
request.setVersion(getVersion());
request.setHeaders(getHeaders());
request.setAbsoluteRequestUri(isAbsoluteRequestUri());
return new BasicRequestProducer(request, entityProducerCopy);
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultConnectionReuseStrategy method testRequestExplicitClose.
@Test
public void testRequestExplicitClose() throws Exception {
final HttpRequest request = new BasicHttpRequest(Method.GET, "/");
request.addHeader("Connection", "close");
final HttpResponse response = new BasicHttpResponse(200, "OK");
response.addHeader("Transfer-Encoding", "chunked");
response.addHeader("Connection", "keep-alive");
Assertions.assertFalse(reuseStrategy.keepAlive(request, response, context));
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultConnectionReuseStrategy method testRequestNoExplicitClose.
@Test
public void testRequestNoExplicitClose() throws Exception {
final HttpRequest request = new BasicHttpRequest(Method.GET, "/");
request.addHeader("Connection", "blah, blah, blah");
final HttpResponse response = new BasicHttpResponse(200, "OK");
response.addHeader("Transfer-Encoding", "chunked");
response.addHeader("Connection", "keep-alive");
Assertions.assertTrue(reuseStrategy.keepAlive(request, response, context));
}
Aggregations