Commit 2ee0d459 authored by Mathijs den Burger's avatar Mathijs den Burger

CMS7-8367: use actual context path in generated JavaScript

parent a110a370
......@@ -22,6 +22,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/
class AutoReloadServiceImpl implements AutoReloadService {
static final String DEFAULT_CONTEXT_PATH_VALUE = "/site";
private final AutoReloadServiceConfig config;
private final AutoReloadServer autoReloadServer;
private final AtomicBoolean enabled;
......@@ -45,8 +47,17 @@ class AutoReloadServiceImpl implements AutoReloadService {
}
@Override
public String getJavaScript() {
return cachedJavaScript;
public String getJavaScript(final String contextPath) {
if (contextPath == null) {
throw new IllegalArgumentException("context path cannot be null");
}
if (!contextPath.isEmpty() && (!contextPath.startsWith("/") || contextPath.endsWith("/"))) {
throw new IllegalArgumentException("illegal context path: '" + contextPath + "'");
}
if (cachedJavaScript != null) {
return cachedJavaScript.replace(DEFAULT_CONTEXT_PATH_VALUE, contextPath);
}
return null;
}
@Override
......
......@@ -16,6 +16,7 @@
(function(window, console) {
var AUTO_RELOAD_PATH = "/autoreload",
CONTEXT_PATH = "/site",
RECONNECT_DELAY_MILLIS = 5000,
MAX_RECONNECT_ATTEMPTS = 120, // retry for 120 * 5000 ms = 10 minutes
isReloadingPage,
......@@ -84,11 +85,7 @@
}
function serverUrl() {
var contextPath = document.location.pathname.split('/').slice(0, 2).join('/');
return "ws://" + document.location.host + contextPath + AUTO_RELOAD_PATH;
}
function init() {
return "ws://" + document.location.host + CONTEXT_PATH + AUTO_RELOAD_PATH;
}
if (window.addEventListener && window.WebSocket) {
......
......@@ -18,6 +18,7 @@ package org.onehippo.cms7.services.autoreload;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
......@@ -54,13 +55,51 @@ public class AutoReloadServiceImplTest {
@Test
public void uses_javascript_from_loader() {
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl(DUMMY_JAVASCRIPT);
assertEquals(DUMMY_JAVASCRIPT, autoReload.getJavaScript());
assertEquals(DUMMY_JAVASCRIPT, autoReload.getJavaScript("/site"));
}
@Test
public void uses_context_path_in_loaded_javascript() {
String script = "var CONTEXT_PATH = \"" + AutoReloadServiceImpl.DEFAULT_CONTEXT_PATH_VALUE + "\";";
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl(script);
assertEquals("var CONTEXT_PATH = \"/intranet\";", autoReload.getJavaScript("/intranet"));
}
@Test
public void uses_empty_context_path_in_loaded_javascript() {
String script = "var CONTEXT_PATH = \"" + AutoReloadServiceImpl.DEFAULT_CONTEXT_PATH_VALUE + "\";";
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl(script);
assertEquals("var CONTEXT_PATH = \"\";", autoReload.getJavaScript(""));
}
@Test(expected = IllegalArgumentException.class)
public void context_path_that_is_null_throws_exception() {
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl("");
autoReload.getJavaScript(null);
}
@Test(expected = IllegalArgumentException.class)
public void context_path_that_does_not_start_with_slash_throws_exception() {
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl("");
autoReload.getJavaScript("wrongContextPath");
}
@Test(expected = IllegalArgumentException.class)
public void context_path_that_ends_with_slash_throws_exception() {
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl("");
autoReload.getJavaScript("wrongContextPath/");
}
@Test(expected = IllegalArgumentException.class)
public void context_path_that_is_only_a_slash_throws_exception() {
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl("");
autoReload.getJavaScript("/");
}
@Test
public void uses_null_javascript_when_loading_fails() {
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl(null);
assertNull(autoReload.getJavaScript());
assertNull(autoReload.getJavaScript(""));
}
@Test
......@@ -115,7 +154,7 @@ public class AutoReloadServiceImplTest {
String script = DUMMY_JAVASCRIPT;
AutoReloadServiceImpl autoReload = createAutoReloadServiceImpl(script);
autoReload.setEnabled(false);
assertEquals(script, autoReload.getJavaScript());
assertEquals(script, autoReload.getJavaScript(""));
}
@Test
......@@ -146,4 +185,13 @@ public class AutoReloadServiceImplTest {
verify(autoReloadServer);
}
@Test
public void replaces_context_path_value_once_in_real_script() {
AutoReloadScriptLoader realScriptLoader = new AutoReloadScriptLoader();
final AutoReloadServiceImpl autoReload = new AutoReloadServiceImpl(config, realScriptLoader, autoReloadServer);
final String javaScript = autoReload.getJavaScript("/veryLongAndUniqueContextPath");
assertEquals("the context path should have been replaced only once in the loaded script",
1, StringUtils.countMatches(javaScript, "/veryLongAndUniqueContextPath"));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment