use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestBasicMessageBuilders method testRequestCopy.
@Test
public void testRequestCopy() throws Exception {
final HttpRequest request = new BasicHttpRequest(Method.GET, URI.create("https://host:3456/stuff?blah"));
request.addHeader("h1", "v1");
request.addHeader("h1", "v2");
request.addHeader("h2", "v2");
request.setVersion(HttpVersion.HTTP_2);
final BasicRequestBuilder builder = BasicRequestBuilder.copy(request);
Assertions.assertEquals("GET", builder.getMethod());
Assertions.assertEquals("https", builder.getScheme());
Assertions.assertEquals(new URIAuthority("host", 3456), builder.getAuthority());
Assertions.assertEquals("/stuff?blah", builder.getPath());
Assertions.assertEquals(HttpVersion.HTTP_2, builder.getVersion());
assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class BenchmarkWorker method createRequestProducer.
private AsyncRequestProducer createRequestProducer() {
String method = config.getMethod();
if (method == null) {
method = config.isHeadInsteadOfGet() ? Method.HEAD.name() : Method.GET.name();
}
final BasicHttpRequest request = new BasicHttpRequest(method, config.getUri());
final String[] headers = config.getHeaders();
if (headers != null) {
for (final String s : headers) {
final int pos = s.indexOf(':');
if (pos != -1) {
request.addHeader(new BasicHeader(s.substring(0, pos).trim(), s.substring(pos + 1)));
}
}
}
if (!config.isKeepAlive() && !config.isForceHttp2()) {
request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE));
}
if (config.isUseAcceptGZip()) {
request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
}
if (config.getSoapAction() != null && config.getSoapAction().length() > 0) {
request.addHeader(new BasicHeader("SOAPAction", config.getSoapAction()));
}
final AsyncEntityProducer entityProducer;
if (config.getPayloadFile() != null) {
entityProducer = new FileEntityProducer(config.getPayloadFile(), config.getContentType(), config.isUseChunking());
} else if (config.getPayloadText() != null) {
entityProducer = new BasicAsyncEntityProducer(config.getPayloadText(), config.getContentType(), config.isUseChunking());
} else {
entityProducer = null;
}
return new AsyncRequestProducer() {
@Override
public void sendRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
channel.sendRequest(request, entityProducer, context);
}
@Override
public boolean isRepeatable() {
return entityProducer == null || entityProducer.isRepeatable();
}
@Override
public int available() {
return entityProducer != null ? entityProducer.available() : 0;
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
if (entityProducer != null) {
entityProducer.produce(channel);
}
}
@Override
public void failed(final Exception cause) {
if (config.getVerbosity() >= 1) {
System.out.println("Failed HTTP request: " + cause.getMessage());
}
}
@Override
public void releaseResources() {
if (entityProducer != null) {
entityProducer.releaseResources();
}
}
};
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromMessageConnectMissingAuthority.
@Test
public void testConvertFromMessageConnectMissingAuthority() throws Exception {
final HttpRequest request = new BasicHttpRequest("CONNECT", null, null);
request.addHeader("Custom123", "Value");
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "CONNECT request authority is not set");
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromMessageConnectionHeader.
@Test
public void testConvertFromMessageConnectionHeader() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
request.addHeader("Connection", "Keep-Alive");
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "Header 'Connection: Keep-Alive' is illegal for HTTP/2 messages");
}
use of org.apache.hc.core5.http.message.BasicHttpRequest in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsTEHeader.
@Test
public void testConvertFromFieldsTEHeader() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
request.addHeader("TE", "gzip");
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
Assertions.assertThrows(HttpException.class, () -> converter.convert(request), "Header 'TE: gzip' is illegal for HTTP/2 messages");
}
Aggregations