-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace client side HTTP requests using the hostname that is in the a…
…ddress bar Signed-off-by: Mario Loriedo <mloriedo@redhat.com>
- Loading branch information
Showing
8 changed files
with
175 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
33 changes: 33 additions & 0 deletions
33
ide/commons-gwt/src/main/java/org/eclipse/che/ide/util/UrlUtils.java
This file contains 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,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.ide.util; | ||
|
||
import com.google.gwt.core.client.GWT; | ||
import com.google.gwt.regexp.shared.RegExp; | ||
import com.google.gwt.user.client.Window; | ||
|
||
public class UrlUtils { | ||
|
||
public static String fixHostName(String internalUrl) { | ||
if (GWT.isScript()) { | ||
String hostnameInBrowser = Window.Location.getHostName(); | ||
return replaceHostNameInUrl(internalUrl, hostnameInBrowser); | ||
} else { | ||
return internalUrl; | ||
} | ||
} | ||
|
||
protected static String replaceHostNameInUrl(String url, String newHostName) { | ||
RegExp re = RegExp.compile("(\\://).+(?=[\\:])"); | ||
return re.replace(url, "$1" + newHostName); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
ide/commons-gwt/src/test/java/org/eclipse/che/ide/util/UrlUtilsTest.java
This file contains 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,59 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.ide.util; | ||
|
||
import org.eclipse.che.ide.ui.loaders.initialization.LoaderPresenter; | ||
import org.eclipse.che.ide.ui.loaders.initialization.LoaderView; | ||
import org.eclipse.che.ide.ui.loaders.initialization.LoadingInfo; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.eclipse.che.ide.ui.loaders.initialization.LoaderPresenter.State.WORKING; | ||
import static org.mockito.Matchers.anyList; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class UrlUtilsTest { | ||
|
||
@Test | ||
public void should_replaceHostName_when_hostname_is_an_IP_address() { | ||
// Given | ||
String hostnameInBrowser = "localhost"; | ||
String inputUrl = "http://192.58.16.2:8080/api/"; | ||
|
||
// When | ||
String fixedUrl = UrlUtils.replaceHostNameInUrl(inputUrl, hostnameInBrowser); | ||
|
||
// Then | ||
Assert.assertEquals("http://localhost:8080/api/", fixedUrl); | ||
} | ||
|
||
@Test | ||
public void should_replaceHostName_when_hostname_is_fqdn() { | ||
// Given | ||
String hostnameInBrowser = "localhost"; | ||
String inputUrl = "http://myhost.example.com:8080/api/"; | ||
|
||
// When | ||
String fixedUrl = UrlUtils.replaceHostNameInUrl(inputUrl, hostnameInBrowser); | ||
|
||
// Then | ||
Assert.assertEquals("http://localhost:8080/api/", fixedUrl); | ||
} | ||
|
||
} |
This file contains 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