Skip to content

Commit

Permalink
[GEOS-7653] tiled layers preview does not use Proxy Base URL (#3079)
Browse files Browse the repository at this point in the history
* Fix for GEOS-7653

* Unit tests for GEOS-7653 made by Ian Turton

* fix formatting and expected value
  • Loading branch information
ianturton committed Sep 5, 2018
1 parent 7ccf9cb commit cbc41be
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,18 @@ public int compare(MimeType o1, MimeType o2) {
menu.add(previewLinks);

// build the wms request, redirect to it in a new window, reset the selection
String demoUrl =
final String baseURL = ResponseUtils.baseURL(getGeoServerApplication().servletRequest());
// Since we're working with an absolute URL, build the URL this way to ensure proxy
// mangling is applied.
final String demoURL =
"'"
+ ResponseUtils.baseURL(getGeoServerApplication().servletRequest())
+ "gwc/demo/"
+ layer.getName()
+ ResponseUtils.buildURL(
baseURL, "gwc/demo/" + layer.getName(), null, URLType.EXTERNAL)
+ "?' + this.options[this.selectedIndex].value";
menu.add(
new AttributeAppender(
"onchange",
new Model<String>("window.open(" + demoUrl + ");this.selectedIndex=0"),
new Model<String>("window.open(" + demoURL + ");this.selectedIndex=0"),
";"));

f.add(menu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import static org.junit.Assert.*;

import java.lang.reflect.Method;
import java.util.List;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.LayerGroupInfo;
Expand All @@ -24,6 +29,21 @@
import org.junit.Test;

public class CachedLayersPageTest extends GeoServerWicketTestSupport {
private static final Method getReplaceModelMethod;
/*
* Yes, we do need to use reflection here as the stupid wicket model is private and has no setter which
* makes it hard to test!
* See https://cwiki.apache.org/confluence/display/WICKET/Testing+Pages
*/
static {
try {
getReplaceModelMethod = AttributeModifier.class.getDeclaredMethod("getReplaceModel");
getReplaceModelMethod.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

@Rule
public GeoServerExtensionsHelper.ExtensionsHelperRule extensions =
Expand Down Expand Up @@ -109,4 +129,59 @@ public void testMangleSeedLink() {
"table:listContainer:items:1:itemProperties:7:component:seedLink",
"http://rewrite/gwc/rest/seed/cgf:Polygons");
}

@Test
public void testNoManglePreviewLink() {

// Don't add a mangler

CachedLayersPage page = new CachedLayersPage();

tester.startPage(page);
// print(page, true, true);
Component component =
tester.getComponentFromLastRenderedPage(
"table:listContainer:items:1:itemProperties:6:component:menu");
List<AttributeModifier> attr = component.getBehaviors(AttributeModifier.class);
try {
IModel<?> model = (IModel<?>) getReplaceModelMethod.invoke(attr.get(0));
assertTrue(
"Unmangled names fail",
model.getObject()
.toString()
.contains("http://localhost:80/context/gwc/demo/cgf"));
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Test
public void testManglePreviewLink() {
// Mimic a Proxy URL mangler
URLMangler testMangler =
(base, path, map, type) -> {
base.setLength(0);
base.append("http://rewrite/");
};
extensions.singleton("testMangler", testMangler, URLMangler.class);

CachedLayersPage page = new CachedLayersPage();

tester.startPage(page);
Component component =
tester.getComponentFromLastRenderedPage(
"table:listContainer:items:1:itemProperties:6:component:menu");
List<AttributeModifier> attr = component.getBehaviors(AttributeModifier.class);
try {
IModel<?> model = (IModel<?>) getReplaceModelMethod.invoke(attr.get(0));

assertTrue(
"Mangled names fail",
model.getObject().toString().contains("http://rewrite/gwc/demo/cgf"));
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit cbc41be

Please sign in to comment.