use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class Http1IntegrationTest method testPostIdentityTransfer.
@Test
public void testPostIdentityTransfer() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
client.start();
final int reqNo = 5;
for (int i = 0; i < reqNo; i++) {
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/hello"), new MultiLineEntityProducer("Hello", 16 * i)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
streamEndpoint.close();
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
final String entity = result.getBody();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
Assertions.assertEquals("Hi there", entity);
}
}
use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class H2TestServer method start.
public InetSocketAddress start(final HttpProcessor httpProcessor, final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator, final Http1Config http1Config) throws Exception {
start(new InternalServerProtocolNegotiationStarter(httpProcessor != null ? httpProcessor : HttpProcessors.server(), new DefaultAsyncResponseExchangeHandlerFactory(registry, exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new), HttpVersionPolicy.FORCE_HTTP_1, H2Config.DEFAULT, http1Config, CharCodingConfig.DEFAULT, sslContext, sslSessionInitializer, sslSessionVerifier));
final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
final ListenerEndpoint listener = future.get();
return (InetSocketAddress) listener.getAddress();
}
use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class InternalClientProtocolNegotiationStarter method createHandler.
@Override
public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
if (sslContext != null) {
ioSession.startTls(sslContext, null, null, sslSessionInitializer, sslSessionVerifier, null);
}
final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.client(), http1Config, charCodingConfig, LoggingHttp1StreamListener.INSTANCE_CLIENT);
final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), exchangeHandlerFactory, h2Config, charCodingConfig, LoggingH2StreamListener.INSTANCE);
ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ClientHttp1UpgradeHandler(http1StreamHandlerFactory));
ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ClientH2UpgradeHandler(http2StreamHandlerFactory));
switch(versionPolicy) {
case FORCE_HTTP_2:
return new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, false);
case FORCE_HTTP_1:
return new ClientHttp1IOEventHandler(http1StreamHandlerFactory.create(ioSession));
default:
return new HttpProtocolNegotiator(ioSession, null);
}
}
use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class InternalServerProtocolNegotiationStarter method createHandler.
@Override
public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
if (sslContext != null) {
ioSession.startTls(sslContext, null, null, sslSessionInitializer, sslSessionVerifier, null);
}
final ServerHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ServerHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.server(), exchangeHandlerFactory, http1Config, charCodingConfig, LoggingHttp1StreamListener.INSTANCE_SERVER);
final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory = new ServerH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.server(), exchangeHandlerFactory, h2Config, charCodingConfig, LoggingH2StreamListener.INSTANCE);
ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ServerHttp1UpgradeHandler(http1StreamHandlerFactory));
ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ServerH2UpgradeHandler(http2StreamHandlerFactory));
switch(versionPolicy) {
case FORCE_HTTP_2:
return new ServerH2PrefaceHandler(ioSession, http2StreamHandlerFactory);
case FORCE_HTTP_1:
return new ServerHttp1IOEventHandler(http1StreamHandlerFactory.create(sslContext != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id, ioSession));
default:
return new HttpProtocolNegotiator(ioSession, null);
}
}
use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testInvalidInput.
@Test
public void testInvalidInput() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.execute(null, conn, context);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.execute(request, null, context);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.execute(request, conn, null);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.preProcess(null, httprocessor, context);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.preProcess(request, null, context);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.preProcess(request, httprocessor, null);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.postProcess(null, httprocessor, context);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.postProcess(response, null, context);
});
Assertions.assertThrows(NullPointerException.class, () -> {
final HttpRequestExecutor executor = new HttpRequestExecutor();
executor.postProcess(response, httprocessor, null);
});
}
Aggregations