Skip to content

Commit e59594f

Browse files
committed
When the host is unknown, make the HtmlUnitDriver return an error page.
Fixes issue #6512.
1 parent e9b0326 commit e59594f

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

java/client/src/org/openqa/selenium/htmlunit/HtmlUnitDriver.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.htmlunit;
1919

20+
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_FINDING_BY_CSS;
21+
2022
import com.google.common.collect.Collections2;
2123
import com.google.common.collect.ImmutableSet;
2224
import com.google.common.collect.Maps;
@@ -29,7 +31,9 @@
2931
import com.gargoylesoftware.htmlunit.ProxyConfig;
3032
import com.gargoylesoftware.htmlunit.ScriptResult;
3133
import com.gargoylesoftware.htmlunit.SgmlPage;
34+
import com.gargoylesoftware.htmlunit.StringWebResponse;
3235
import com.gargoylesoftware.htmlunit.TopLevelWindow;
36+
import com.gargoylesoftware.htmlunit.UnexpectedPage;
3337
import com.gargoylesoftware.htmlunit.WaitingRefreshHandler;
3438
import com.gargoylesoftware.htmlunit.WebClient;
3539
import com.gargoylesoftware.htmlunit.WebClientOptions;
@@ -116,8 +120,6 @@
116120
import java.util.concurrent.Callable;
117121
import java.util.concurrent.TimeUnit;
118122

119-
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_FINDING_BY_CSS;
120-
121123
public class HtmlUnitDriver implements WebDriver, JavascriptExecutor,
122124
FindsById, FindsByLinkText, FindsByXPath, FindsByName, FindsByCssSelector,
123125
FindsByTagName, FindsByClassName, HasCapabilities, HasInputDevices {
@@ -476,7 +478,10 @@ protected void get(URL fullUrl) {
476478
// A "get" works over the entire page
477479
currentWindow = getCurrentWindow().getTopWindow();
478480
} catch (UnknownHostException e) {
479-
// This should be fine
481+
getCurrentWindow().getTopWindow().setEnclosedPage(new UnexpectedPage(
482+
new StringWebResponse("Unknown host", fullUrl),
483+
getCurrentWindow().getTopWindow()
484+
));
480485
} catch (ConnectException e) {
481486
// This might be expected
482487
} catch (SocketTimeoutException e) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2014 Selenium committers
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package org.openqa.selenium.htmlunit;
18+
19+
20+
import static org.junit.Assert.assertNotEquals;
21+
22+
import org.junit.Test;
23+
import org.openqa.selenium.testing.JUnit4TestBase;
24+
25+
public class ErroneousPageLoadingTest extends JUnit4TestBase {
26+
27+
@Test
28+
public void shouldNotReturnSourceOfOldPageWhenLoadFailsDueToABadHost() {
29+
driver.get(pages.xhtmlTestPage);
30+
String originalSource = driver.getPageSource();
31+
32+
driver.get("http://thishostdoesnotexist.norshallitever");
33+
String currentSource = driver.getPageSource();
34+
35+
assertNotEquals(originalSource, currentSource);
36+
}
37+
}

0 commit comments

Comments
 (0)