Skip to content

Commit b7ac960

Browse files
jamesantoslandelle
authored andcommitted
Fix NullPointerException while setting custom InetAddress (#1532)
1 parent 29cc347 commit b7ac960

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,14 @@ private <T> Future<List<InetSocketAddress>> resolveAddresses(Request request,
344344
} else {
345345
int port = uri.getExplicitPort();
346346

347+
InetSocketAddress unresolvedRemoteAddress = InetSocketAddress.createUnresolved(uri.getHost(), port);
348+
scheduleRequestTimeout(future, unresolvedRemoteAddress);
349+
347350
if (request.getAddress() != null) {
348351
// bypass resolution
349352
InetSocketAddress inetSocketAddress = new InetSocketAddress(request.getAddress(), port);
350353
return promise.setSuccess(singletonList(inetSocketAddress));
351-
352354
} else {
353-
InetSocketAddress unresolvedRemoteAddress = InetSocketAddress.createUnresolved(uri.getHost(), port);
354-
scheduleRequestTimeout(future, unresolvedRemoteAddress);
355355
return RequestHostnameResolver.INSTANCE.resolve(request.getNameResolver(), unresolvedRemoteAddress, asyncHandler);
356356
}
357357
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient;
15+
16+
import io.netty.util.internal.SocketUtils;
17+
import org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter;
18+
import org.asynchttpclient.testserver.HttpServer;
19+
import org.asynchttpclient.testserver.HttpTest;
20+
import org.testng.annotations.AfterClass;
21+
import org.testng.annotations.BeforeClass;
22+
import org.testng.annotations.Test;
23+
24+
import static java.util.concurrent.TimeUnit.SECONDS;
25+
import static org.asynchttpclient.Dsl.get;
26+
import static org.asynchttpclient.test.TestUtils.TIMEOUT;
27+
import static org.testng.Assert.assertEquals;
28+
29+
public class CustomRemoteAddressTest extends HttpTest {
30+
31+
private static HttpServer server;
32+
33+
@BeforeClass
34+
public static void start() throws Throwable {
35+
server = new HttpServer();
36+
server.start();
37+
}
38+
39+
@AfterClass
40+
public static void stop() throws Throwable {
41+
server.close();
42+
}
43+
44+
@Test
45+
public void getRootUrlWithCustomRemoteAddress() throws Throwable {
46+
withClient().run(client ->
47+
withServer(server).run(server -> {
48+
String url = server.getHttpUrl();
49+
server.enqueueOk();
50+
RequestBuilder request = get(url).setAddress(SocketUtils.addressByName("localhost"));
51+
Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS);
52+
assertEquals(response.getStatusCode(), 200);
53+
}));
54+
}
55+
}

0 commit comments

Comments
 (0)