|
57 | 57 | import org.openqa.selenium.remote.http.HttpHandler; |
58 | 58 | import org.openqa.selenium.remote.http.HttpRequest; |
59 | 59 | import org.openqa.selenium.remote.http.HttpResponse; |
| 60 | +import org.openqa.selenium.remote.http.WebSocket; |
60 | 61 | import org.openqa.selenium.remote.service.DriverService; |
61 | 62 |
|
62 | 63 | /** |
@@ -94,12 +95,7 @@ public class RemoteWebDriverBuilder { |
94 | 95 | private final List<Capabilities> requestedCapabilities = new ArrayList<>(); |
95 | 96 | private final Map<String, Object> additionalCapabilities = new TreeMap<>(); |
96 | 97 | private final Map<String, Object> metadata = new TreeMap<>(); |
97 | | - private Function<ClientConfig, HttpHandler> handlerFactory = |
98 | | - config -> { |
99 | | - HttpClient.Factory factory = HttpClient.Factory.createDefault(); |
100 | | - HttpClient client = factory.createClient(config); |
101 | | - return client.with(new CloseHttpClientFilter(factory, client)); |
102 | | - }; |
| 98 | + private HttpClient.Factory clientFactory = HttpClient.Factory.createDefault(); |
103 | 99 | private ClientConfig clientConfig = ClientConfig.defaultConfig(); |
104 | 100 | private URI remoteHost = null; |
105 | 101 | private DriverService driverService; |
@@ -285,7 +281,25 @@ public RemoteWebDriverBuilder withDriverService(DriverService service) { |
285 | 281 | /** visible for testing only */ |
286 | 282 | RemoteWebDriverBuilder connectingWith(Function<ClientConfig, HttpHandler> handlerFactory) { |
287 | 283 | Require.nonNull("Handler factory", handlerFactory); |
288 | | - this.handlerFactory = handlerFactory; |
| 284 | + this.clientFactory = |
| 285 | + new HttpClient.Factory() { |
| 286 | + @Override |
| 287 | + public HttpClient createClient(ClientConfig config) { |
| 288 | + HttpHandler handler = handlerFactory.apply(config); |
| 289 | + |
| 290 | + return new HttpClient() { |
| 291 | + @Override |
| 292 | + public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) { |
| 293 | + throw new UnsupportedOperationException("sockets are not supported"); |
| 294 | + } |
| 295 | + |
| 296 | + @Override |
| 297 | + public HttpResponse execute(HttpRequest req) throws UncheckedIOException { |
| 298 | + return handler.execute(req); |
| 299 | + } |
| 300 | + }; |
| 301 | + } |
| 302 | + }; |
289 | 303 | return this; |
290 | 304 | } |
291 | 305 |
|
@@ -380,26 +394,37 @@ private WebDriver getRemoteDriver() { |
380 | 394 | driverClientConfig = driverClientConfig.authenticateAs(credentials); |
381 | 395 | } |
382 | 396 |
|
383 | | - HttpHandler client = handlerFactory.apply(driverClientConfig); |
| 397 | + HttpClient client = clientFactory.createClient(driverClientConfig); |
384 | 398 | HttpHandler handler = |
385 | 399 | Require.nonNull("Http handler", client) |
386 | 400 | .with( |
387 | 401 | new AddWebDriverSpecHeaders() |
388 | 402 | .andThen(new ErrorFilter()) |
389 | | - .andThen(new DumpHttpExchangeFilter())); |
| 403 | + .andThen(new DumpHttpExchangeFilter()) |
| 404 | + .andThen(new CloseHttpClientFilter(clientFactory, client))); |
390 | 405 |
|
391 | 406 | Either<SessionNotCreatedException, ProtocolHandshake.Result> result; |
392 | 407 | try { |
393 | 408 | result = new ProtocolHandshake().createSession(handler, getPayload()); |
394 | 409 | } catch (IOException e) { |
395 | | - throw new SessionNotCreatedException("Unable to create new remote session.", e); |
| 410 | + try (client) { |
| 411 | + throw new SessionNotCreatedException("Unable to create new remote session.", e); |
| 412 | + } |
396 | 413 | } |
397 | 414 |
|
398 | 415 | if (result.isRight()) { |
399 | | - CommandExecutor executor = result.map(res -> createExecutor(handler, res)); |
400 | | - return new RemoteWebDriver(executor, new ImmutableCapabilities()); |
| 416 | + try { |
| 417 | + CommandExecutor executor = result.map(res -> createExecutor(handler, res)); |
| 418 | + return new RemoteWebDriver(executor, new ImmutableCapabilities()); |
| 419 | + } catch (Throwable t) { |
| 420 | + try (client) { |
| 421 | + throw t; |
| 422 | + } |
| 423 | + } |
401 | 424 | } else { |
402 | | - throw result.left(); |
| 425 | + try (client) { |
| 426 | + throw result.left(); |
| 427 | + } |
403 | 428 | } |
404 | 429 | } |
405 | 430 |
|
|
0 commit comments