private Url parseUrl(String urlString) { Url url = Url.parse(urlString); // to avoid UrlRenderer touching the url (shouldRenderAsFull) url.setProtocol(null); url.setHost(null); url.setPort(null); return url; }
@Test public void renderFullUrlWithRelativeArgument() { Url baseUrl = Url.parse("one/two/three"); baseUrl.setProtocol("http"); baseUrl.setHost("www.example.com"); baseUrl.setPort(8888); UrlRenderer renderer = new UrlRenderer(new MockWebRequest(baseUrl)); renderer.setBaseUrl(baseUrl); // this is needed because MockWebRequest cuts data String fullUrl = renderer.renderFullUrl(Url.parse("../four")); assertEquals("http://www.example.com:8888/one/four", fullUrl); }
/** https://issues.apache.org/jira/browse/WICKET-4514 */ @Test public void renderFullUrlWithAbsoluteArgument() { Url baseUrl = Url.parse("one/two/three"); baseUrl.setProtocol("http"); baseUrl.setHost("www.example.com"); baseUrl.setPort(8888); UrlRenderer renderer = new UrlRenderer(new MockWebRequest(baseUrl)); renderer.setBaseUrl(baseUrl); // this is needed because MockWebRequest cuts data String fullUrl = renderer.renderFullUrl(Url.parse("/four")); // url starting with slash is // considered absolute assertEquals("http://www.example.com:8888/four", fullUrl); }
/** * Creates a url for the handler. Modifies it with the correct {@link Scheme} if necessary. * * @param handler * @param request * @return url */ final Url mapHandler(IRequestHandler handler, Request request) { Url url = delegate.mapHandler(handler); Scheme desired = getDesiredSchemeFor(handler); Scheme current = getSchemeOf(request); if (!desired.isCompatibleWith(current)) { // the generated url does not have the correct scheme, set it (which in turn will cause // the url to be rendered in its full representation) url.setProtocol(desired.urlName()); if (url.getPort() != null || !desired.usesStandardPort(config)) { url.setPort(desired.getPort(config)); } } return url; }
/** * https://issues.apache.org/jira/browse/WICKET-4561 * https://issues.apache.org/jira/browse/WICKET-4562 */ @Test public void renderUrlWithRelativeArgument() { Url baseUrl = Url.parse("one/two/three"); UrlRenderer renderer = new UrlRenderer(new MockWebRequest(baseUrl)); baseUrl.setProtocol("http"); baseUrl.setHost("www.example.com"); baseUrl.setPort(8888); renderer.setBaseUrl(baseUrl); Url newUrl = Url.parse("four"); newUrl.setProtocol("https"); String fullUrl = renderer.renderUrl(newUrl); assertEquals("https://www.example.com:8888/four", fullUrl); newUrl = Url.parse("./four"); newUrl.setProtocol("https"); fullUrl = renderer.renderUrl(newUrl); assertEquals("https://www.example.com:8888/four", fullUrl); newUrl = Url.parse("./././four"); newUrl.setProtocol("https"); fullUrl = renderer.renderUrl(newUrl); assertEquals("https://www.example.com:8888/four", fullUrl); newUrl = Url.parse("../four"); newUrl.setProtocol("https"); fullUrl = renderer.renderUrl(newUrl); assertEquals("https://www.example.com:8888/four", fullUrl); newUrl = Url.parse(".././four"); newUrl.setProtocol("https"); fullUrl = renderer.renderUrl(newUrl); assertEquals("https://www.example.com:8888/four", fullUrl); newUrl = Url.parse("../../../../four"); newUrl.setProtocol("https"); fullUrl = renderer.renderUrl(newUrl); assertEquals("https://www.example.com:8888/four", fullUrl); }