001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.test.web.htmlunit.webdriver;
018
019import com.gargoylesoftware.htmlunit.BrowserVersion;
020import org.openqa.selenium.Capabilities;
021
022import org.springframework.core.env.Environment;
023import org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver;
024import org.springframework.util.Assert;
025
026/**
027 * {@link LocalHostWebConnectionHtmlUnitDriver} will automatically prefix relative URLs
028 * with <code>localhost:$&#123;local.server.port&#125;</code>.
029 *
030 * @author Phillip Webb
031 * @since 1.4.0
032 */
033public class LocalHostWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver {
034
035        private final Environment environment;
036
037        public LocalHostWebConnectionHtmlUnitDriver(Environment environment) {
038                Assert.notNull(environment, "Environment must not be null");
039                this.environment = environment;
040        }
041
042        public LocalHostWebConnectionHtmlUnitDriver(Environment environment,
043                        boolean enableJavascript) {
044                super(enableJavascript);
045                Assert.notNull(environment, "Environment must not be null");
046                this.environment = environment;
047        }
048
049        public LocalHostWebConnectionHtmlUnitDriver(Environment environment,
050                        BrowserVersion browserVersion) {
051                super(browserVersion);
052                Assert.notNull(environment, "Environment must not be null");
053                this.environment = environment;
054        }
055
056        public LocalHostWebConnectionHtmlUnitDriver(Environment environment,
057                        Capabilities capabilities) {
058                super(capabilities);
059                Assert.notNull(environment, "Environment must not be null");
060                this.environment = environment;
061        }
062
063        @Override
064        public void get(String url) {
065                if (url.startsWith("/")) {
066                        String port = this.environment.getProperty("local.server.port", "8080");
067                        url = "http://localhost:" + port + url;
068                }
069                super.get(url);
070        }
071
072}