Repository navigation
HTTPCLIENT-2429: Improve support for custom transport layers #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arturobernalg
wants to merge
1
commit into
apache:master
Choose a base branch
from
arturobernalg:HTTPCLIENT-2429
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ient5/src/test/java/org/apache/hc/client5/http/impl/async/CustomTransportBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * ==================================================================== | ||
| * | ||
| * This software consists of voluntary contributions made by many | ||
| * individuals on behalf of the Apache Software Foundation. For more | ||
| * information on the Apache Software Foundation, please see | ||
| * <http://www.apache.org/>. | ||
| * | ||
| */ | ||
|
|
||
| package org.apache.hc.client5.http.impl.async; | ||
|
|
||
| import java.net.Socket; | ||
| import java.net.SocketAddress; | ||
| import java.nio.channels.SocketChannel; | ||
|
|
||
| import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; | ||
| import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; | ||
| import org.apache.hc.client5.http.io.DetachedSocketFactory; | ||
| import org.apache.hc.core5.io.IOFunction; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CustomTransportBuilderTest { | ||
|
|
||
| @Test | ||
| void testClassicBuilderAcceptsDetachedSocketFactory() { | ||
| final DetachedSocketFactory socketFactory = proxy -> new Socket(); | ||
| final PoolingHttpClientConnectionManager connectionManager = | ||
| PoolingHttpClientConnectionManagerBuilder.create() | ||
| .setDetachedSocketFactory(socketFactory) | ||
| .build(); | ||
| try { | ||
| Assertions.assertNotNull(connectionManager); | ||
| } finally { | ||
| connectionManager.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testAsyncBuildersAcceptSocketChannelFactory() throws Exception { | ||
| final IOFunction<SocketAddress, SocketChannel> socketChannelFactory = remoteAddress -> SocketChannel.open(); | ||
| try (CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() | ||
| .setSocketChannelFactory(socketChannelFactory) | ||
| .build()) { | ||
| Assertions.assertNotNull(client); | ||
| } | ||
| try (CloseableHttpAsyncClient client = H2AsyncClientBuilder.create() | ||
| .setSocketChannelFactory(socketChannelFactory) | ||
| .build()) { | ||
| Assertions.assertNotNull(client); | ||
| } | ||
| } | ||
|
|
||
| } |
88 changes: 88 additions & 0 deletions
88
.../src/test/java/org/apache/hc/client5/http/impl/async/SocketChannelFactoryBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * ==================================================================== | ||
| * | ||
| * This software consists of voluntary contributions made by many | ||
| * individuals on behalf of the Apache Software Foundation. For more | ||
| * information on the Apache Software Foundation, please see | ||
| * <http://www.apache.org/>. | ||
| * | ||
| */ | ||
|
|
||
| package org.apache.hc.client5.http.impl.async; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.SocketAddress; | ||
| import java.nio.channels.SocketChannel; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; | ||
| import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; | ||
| import org.apache.hc.core5.io.IOFunction; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SocketChannelFactoryBuilderTest { | ||
|
|
||
| @Test | ||
| void testHttpAsyncClientBuilderUsesSocketChannelFactory() throws Exception { | ||
| final AtomicInteger invocationCount = new AtomicInteger(); | ||
| final IOException expected = new IOException("custom transport"); | ||
| final IOFunction<SocketAddress, SocketChannel> socketChannelFactory = remoteAddress -> { | ||
| invocationCount.incrementAndGet(); | ||
| throw expected; | ||
| }; | ||
| try (CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() | ||
| .setSocketChannelFactory(socketChannelFactory) | ||
| .disableAutomaticRetries() | ||
| .build()) { | ||
| assertFactoryUsed(client, invocationCount); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testH2AsyncClientBuilderUsesSocketChannelFactory() throws Exception { | ||
| final AtomicInteger invocationCount = new AtomicInteger(); | ||
| final IOException expected = new IOException("custom transport"); | ||
| final IOFunction<SocketAddress, SocketChannel> socketChannelFactory = remoteAddress -> { | ||
| invocationCount.incrementAndGet(); | ||
| throw expected; | ||
| }; | ||
| try (CloseableHttpAsyncClient client = H2AsyncClientBuilder.create() | ||
| .setSocketChannelFactory(socketChannelFactory) | ||
| .disableAutomaticRetries() | ||
| .build()) { | ||
| assertFactoryUsed(client, invocationCount); | ||
| } | ||
| } | ||
|
|
||
| private static void assertFactoryUsed( | ||
| final CloseableHttpAsyncClient client, | ||
| final AtomicInteger invocationCount) throws Exception { | ||
| client.start(); | ||
| final Future<SimpleHttpResponse> future = client.execute( | ||
| SimpleRequestBuilder.get("http://127.0.0.1:18080/").build(), null); | ||
| Assertions.assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS)); | ||
| Assertions.assertEquals(1, invocationCount.get()); | ||
| } | ||
|
|
||
| } |
64 changes: 64 additions & 0 deletions
64
...t5/src/test/java/org/apache/hc/client5/http/impl/io/DetachedSocketFactoryBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * ==================================================================== | ||
| * | ||
| * This software consists of voluntary contributions made by many | ||
| * individuals on behalf of the Apache Software Foundation. For more | ||
| * information on the Apache Software Foundation, please see | ||
| * <http://www.apache.org/>. | ||
| * | ||
| */ | ||
|
|
||
| package org.apache.hc.client5.http.impl.io; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.hc.client5.http.classic.methods.HttpGet; | ||
| import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
| import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
| import org.apache.hc.client5.http.io.DetachedSocketFactory; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class DetachedSocketFactoryBuilderTest { | ||
|
|
||
| @Test | ||
| void testPoolingBuilderUsesDetachedSocketFactory() throws Exception { | ||
| final AtomicInteger invocationCount = new AtomicInteger(); | ||
| final IOException expected = new IOException("custom transport"); | ||
| final DetachedSocketFactory socketFactory = proxy -> { | ||
| invocationCount.incrementAndGet(); | ||
| throw expected; | ||
| }; | ||
| final PoolingHttpClientConnectionManager connectionManager = | ||
| PoolingHttpClientConnectionManagerBuilder.create() | ||
| .setDetachedSocketFactory(socketFactory) | ||
| .build(); | ||
| try (CloseableHttpClient client = HttpClients.custom() | ||
| .setConnectionManager(connectionManager) | ||
| .disableAutomaticRetries() | ||
| .build()) { | ||
| Assertions.assertThrows(IOException.class, () -> | ||
| client.executeOpen(null, new HttpGet("http://localhost:18080/"), null)); | ||
| Assertions.assertEquals(1, invocationCount.get()); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.