1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File XWikiServletURLFactoryTest.java

 

Code metrics

0
170
35
1
586
394
35
0.21
4.86
35
1

Classes

Class Line # Actions
XWikiServletURLFactoryTest 56 170 0% 35 0
1.0100%
 

Contributing tests

This file is covered by 29 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.net.MalformedURLException;
23    import java.net.URL;
24    import java.util.Arrays;
25    import java.util.HashMap;
26    import java.util.Map;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.mockito.invocation.InvocationOnMock;
32    import org.mockito.stubbing.Answer;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.LocalDocumentReference;
35    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
36    import org.xwiki.test.annotation.BeforeComponent;
37   
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.doc.XWikiDocument;
40    import com.xpn.xwiki.objects.BaseObject;
41    import com.xpn.xwiki.test.MockitoOldcoreRule;
42    import com.xpn.xwiki.test.reference.ReferenceComponentList;
43   
44    import static org.junit.Assert.assertEquals;
45    import static org.mockito.ArgumentMatchers.any;
46    import static org.mockito.Mockito.doReturn;
47    import static org.mockito.Mockito.mock;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Validate {@link XWikiServletURLFactory}.
52    *
53    * @version $Id: 1ceb7862b0f16d8366fba3b63d29f2061853ab34 $
54    */
55    @ReferenceComponentList
 
56    public class XWikiServletURLFactoryTest
57    {
58    private static final String MAIN_WIKI_NAME = "xwiki";
59   
60    @Rule
61    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
62   
63    private XWikiServletURLFactory urlFactory;
64   
65    /**
66    * Flag indicating if the request is secure. A request is secure if either its URL uses the HTTPS scheme or the
67    * receiving web application is behind a secure reverse proxy (e.g. the request was send to the reverse proxy
68    * through HTTPS).
69    * <p>
70    * Tests can set this flag to control the value returned by {@link XWikiRequest#isSecure()}.
71    */
72    private boolean secure;
73   
74    /**
75    * The map of HTTP headers.
76    * <p>
77    * Tests can add values to this map to control the value returned by {@link XWikiRequest#getHeader(String)}.
78    */
79    private final Map<String, String> httpHeaders = new HashMap<>();
80   
 
81  29 toggle @BeforeComponent
82    public void beforeComponent() throws Exception
83    {
84  29 EntityResourceActionLister actionLister =
85    this.oldcore.getMocker().registerMockComponent(EntityResourceActionLister.class);
86  29 when(actionLister.listActions()).thenReturn(Arrays.asList("view"));
87    }
88   
 
89  29 toggle @Before
90    public void before() throws Exception
91    {
92  29 doReturn("DefaultSpace").when(this.oldcore.getSpyXWiki()).getDefaultSpace(any(XWikiContext.class));
93   
94    // Request
95  29 XWikiRequest mockXWikiRequest = mock(XWikiRequest.class);
96  29 when(mockXWikiRequest.getScheme()).thenReturn("http");
97  29 when(mockXWikiRequest.isSecure()).then(new Answer<Boolean>()
98    {
 
99  46 toggle @Override
100    public Boolean answer(InvocationOnMock invocation) throws Throwable
101    {
102  46 return secure;
103    }
104    });
105  29 when(mockXWikiRequest.getServletPath()).thenReturn("");
106  29 when(mockXWikiRequest.getContextPath()).thenReturn("/xwiki");
107  29 when(mockXWikiRequest.getHeader(any())).then(new Answer<String>()
108    {
 
109  42 toggle @Override
110    public String answer(InvocationOnMock invocation) throws Throwable
111    {
112  42 return httpHeaders.get(invocation.getArgument(0));
113    }
114    });
115  29 this.oldcore.getXWikiContext().setRequest(mockXWikiRequest);
116   
117    // Response
118  29 XWikiResponse xwikiResponse = mock(XWikiResponse.class);
119  29 when(xwikiResponse.encodeURL(any())).then(new Answer<String>()
120    {
 
121  33 toggle @Override
122    public String answer(InvocationOnMock invocation) throws Throwable
123    {
124  33 return invocation.getArgument(0);
125    }
126    });
127  29 this.oldcore.getXWikiContext().setResponse(xwikiResponse);
128   
129    // Create sub-wikis.
130  29 createWiki("wiki1");
131  29 createWiki("wiki2");
132   
133  29 this.oldcore.getXWikiContext().setURL(new URL("http://127.0.0.1/xwiki/view/InitialSpace/InitialPage"));
134   
135  29 this.urlFactory = new XWikiServletURLFactory();
136  29 this.urlFactory.init(this.oldcore.getXWikiContext());
137    }
138   
139    /**
140    * Creates a new sub-wiki with the given name.
141    *
142    * @param wikiName the wiki name
143    * @throws Exception if creating the wiki fails
144    */
 
145  58 toggle private void createWiki(String wikiName) throws Exception
146    {
147  58 String wikiDocName = "XWikiServer" + wikiName.substring(0, 1).toUpperCase() + wikiName.substring(1);
148  58 XWikiDocument wikiDoc = this.oldcore.getSpyXWiki()
149    .getDocument(new DocumentReference(MAIN_WIKI_NAME, "XWiki", wikiDocName), this.oldcore.getXWikiContext());
150  58 BaseObject wikiObj =
151    wikiDoc.newXObject(new LocalDocumentReference("XWiki", "XWikiServerClass"), this.oldcore.getXWikiContext());
152  58 wikiObj.setStringValue("server", wikiName + "server");
153  58 this.oldcore.getSpyXWiki().saveDocument(wikiDoc, this.oldcore.getXWikiContext());
154    }
155   
156    // Tests
157   
 
158  1 toggle @Test
159    public void createURLOnMainWiki() throws MalformedURLException
160    {
161  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
162    this.oldcore.getXWikiContext());
163  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
164    }
165   
 
166  1 toggle @Test
167    public void createURLOnSubWiki() throws MalformedURLException
168    {
169  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
170    this.oldcore.getXWikiContext());
171  1 assertEquals(new URL("http://127.0.0.1/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor"), url);
172    }
173   
 
174  1 toggle @Test
175    public void createURLOnSubWikiInVirtualMode() throws MalformedURLException
176    {
177  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
178   
179  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
180    this.oldcore.getXWikiContext());
181  1 assertEquals(new URL("http://127.0.0.1/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor"), url);
182    }
183   
 
184  1 toggle @Test
185    public void createURLOnMainWikiInPathMode() throws MalformedURLException
186    {
187  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "1");
188   
189  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
190    this.oldcore.getXWikiContext());
191  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
192    }
193   
 
194  1 toggle @Test
195    public void createURLOnSubWikiInPathMode() throws MalformedURLException
196    {
197  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "1");
198   
199  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
200    this.oldcore.getXWikiContext());
201  1 assertEquals(new URL("http://127.0.0.1/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor"), url);
202    }
203   
 
204  1 toggle @Test
205    public void createURLOnSubWikiInVirtualModeInPathMode() throws MalformedURLException
206    {
207  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
208  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "1");
209   
210  1 secure = true;
211    // Change the context URL to include a port number and to use HTTPS.
212  1 this.oldcore.getXWikiContext().setURL(new URL("https://localhost:8080/xwiki/view/Main/"));
213    // Reinitialize the URL factory to take into account the new context URL.
214  1 urlFactory.init(this.oldcore.getXWikiContext());
215   
216  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
217    this.oldcore.getXWikiContext());
218  1 assertEquals(new URL("https://localhost:8080/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor"), url);
219  1 assertEquals("/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor",
220    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
221    }
222   
 
223  1 toggle @Test
224    public void createURLOnMainWikiInDomainMode() throws MalformedURLException
225    {
226  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "0");
227   
228  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
229    this.oldcore.getXWikiContext());
230  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
231    }
232   
 
233  1 toggle @Test
234    public void createURLOnSubWikiInDomainMode() throws MalformedURLException
235    {
236  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "0");
237   
238  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
239    this.oldcore.getXWikiContext());
240  1 assertEquals(new URL("http://wiki1server/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
241    }
242   
 
243  1 toggle @Test
244    public void createURLOnSubWikiInVirtualModeInDomainMode() throws MalformedURLException
245    {
246  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
247  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "0");
248   
249  1 URL url = this.urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
250    this.oldcore.getXWikiContext());
251  1 assertEquals(new URL("http://wiki1server/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
252    }
253   
254    /**
255    * Checks the URLs created on the main wiki when XWiki is behind a reverse proxy.
256    *
257    * @throws MalformedURLException shouldn't happen
258    */
 
259  1 toggle @Test
260    public void createURLOnMainWikiInDomainModeInReverseProxyMode() throws MalformedURLException
261    {
262  1 secure = true;
263  1 httpHeaders.put("x-forwarded-host", "www.xwiki.org");
264    // Reinitialize the URL factory to take into account the new security level and HTTP headers.
265  1 urlFactory.init(this.oldcore.getXWikiContext());
266   
267  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "0");
268   
269  1 URL url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
270    this.oldcore.getXWikiContext());
271  1 assertEquals(new URL("https://www.xwiki.org/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
272  1 assertEquals("/xwiki/bin/view/Space/Page?param1=1#anchor",
273    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
274    }
275   
 
276  1 toggle @Test
277    public void createURLOnSubWikiInDomainModeInReverseProxyMode() throws MalformedURLException
278    {
279  1 httpHeaders.put("x-forwarded-host", "www.xwiki.org");
280    // Reinitialize the URL factory to take into account the new HTTP headers.
281  1 urlFactory.init(this.oldcore.getXWikiContext());
282   
283  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "0");
284   
285  1 URL url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
286    this.oldcore.getXWikiContext());
287  1 assertEquals(new URL("http://wiki1server/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
288    // The URL remains absolute in this case.
289  1 assertEquals("http://wiki1server/xwiki/bin/view/Space/Page?param1=1#anchor",
290    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
291    }
292   
 
293  1 toggle @Test
294    public void createURLOnSubWikiInVirtualModeInDomainModeInReverseProxyMode() throws MalformedURLException
295    {
296  1 secure = true;
297  1 httpHeaders.put("x-forwarded-host", "www.xwiki.org");
298    // Reinitialize the URL factory to take into account the new security level and HTTP headers.
299  1 urlFactory.init(this.oldcore.getXWikiContext());
300   
301  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
302  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "0");
303   
304  1 URL url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
305    this.oldcore.getXWikiContext());
306  1 assertEquals(new URL("https://wiki1server/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
307    // The URL remains absolute in this case.
308  1 assertEquals("https://wiki1server/xwiki/bin/view/Space/Page?param1=1#anchor",
309    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
310    }
311   
 
312  1 toggle @Test
313    public void createURLOnMainWikiInPathModeInReverseProxyMode() throws MalformedURLException
314    {
315  1 httpHeaders.put("x-forwarded-host", "www.xwiki.org");
316    // Reinitialize the URL factory to take into account the new HTTP headers.
317  1 urlFactory.init(this.oldcore.getXWikiContext());
318   
319  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "1");
320   
321  1 URL url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
322    this.oldcore.getXWikiContext());
323  1 assertEquals(new URL("http://www.xwiki.org/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
324  1 assertEquals("/xwiki/bin/view/Space/Page?param1=1#anchor",
325    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
326    }
327   
 
328  1 toggle @Test
329    public void createURLOnSubWikiInPathModeInReverseProxyMode() throws MalformedURLException
330    {
331  1 secure = true;
332  1 httpHeaders.put("x-forwarded-host", "www.xwiki.org");
333    // Reinitialize the URL factory to take into account the new security level and HTTP headers.
334  1 urlFactory.init(this.oldcore.getXWikiContext());
335   
336  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "1");
337   
338  1 URL url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
339    this.oldcore.getXWikiContext());
340  1 assertEquals(new URL("https://www.xwiki.org/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor"), url);
341  1 assertEquals("/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor",
342    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
343    }
344   
 
345  1 toggle @Test
346    public void createURLOnSubWikiInVirtualModeInPathModeInReverseProxyMode() throws MalformedURLException
347    {
348  1 httpHeaders.put("x-forwarded-host", "www.xwiki.org:8080");
349    // Reinitialize the URL factory to take into account the new HTTP headers.
350  1 urlFactory.init(this.oldcore.getXWikiContext());
351   
352  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
353  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual.usepath", "1");
354   
355  1 URL url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "wiki1",
356    this.oldcore.getXWikiContext());
357  1 assertEquals(new URL("http://www.xwiki.org:8080/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor"), url);
358  1 assertEquals("/xwiki/wiki/wiki1server/view/Space/Page?param1=1#anchor",
359    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
360    }
361   
362    /**
363    * Tests how URLs are serialized when the request wiki (taken from the request URL) and the context wiki (explicitly
364    * set from code on the XWiki context) are different.
365    */
 
366  1 toggle @Test
367    public void getURLWhenRequestWikiAndContextWikiAreDifferent() throws MalformedURLException
368    {
369  1 this.oldcore.getXWikiContext().setURL(new URL("http://wiki1server/xwiki/view/InitialSpace/InitialPage"));
370    // Reinitialize the URL factory to take into account the new request URL.
371  1 urlFactory.init(this.oldcore.getXWikiContext());
372   
373  1 this.oldcore.getXWikiContext().setWikiId("wiki2");
374   
375  1 String url =
376    urlFactory.getURL(new URL("http://wiki1server/xwiki/bin/view/Space/Page"), this.oldcore.getXWikiContext());
377  1 assertEquals("/xwiki/bin/view/Space/Page", url);
378   
379  1 url =
380    urlFactory.getURL(new URL("http://wiki2server/xwiki/bin/view/Space/Page"), this.oldcore.getXWikiContext());
381  1 assertEquals("http://wiki2server/xwiki/bin/view/Space/Page", url);
382    }
383   
384    /** When the URL contains only the hostname, without a path, / is returned instead of the empty string. */
 
385  1 toggle @Test
386    public void getURLWithEmptyPathReturnsSlash() throws MalformedURLException
387    {
388  1 this.oldcore.getXWikiContext().setURL(new URL("http://wiki1server/xwiki/view/InitialSpace/InitialPage"));
389    // Reinitialize the URL factory to take into account the new request URL.
390  1 this.urlFactory.init(this.oldcore.getXWikiContext());
391   
392  1 String url = this.urlFactory.getURL(new URL("http://wiki1server/"), this.oldcore.getXWikiContext());
393  1 assertEquals("/", url);
394    }
395   
396    /**
397    * When getServerURL is called on a resource from the main wiki, the user is in a subwiki, and xwiki.home is set,
398    * xwiki.home should be returned. see: XWIKI-5981
399    */
 
400  1 toggle @Test
401    public void getServerURLFromVirtualWithXWikiDotHomeEnabled() throws MalformedURLException
402    {
403  1 this.oldcore.getXWikiContext()
404    .setURL(new URL("http://virtual1.mywiki.tld/xwiki/view/InitialSpace/InitialPage"));
405  1 this.oldcore.getXWikiContext().setWikiId("subwiki");
406   
407    // This is called by XWiki#getXWiki() and is set to whatever the user asks for.
408    // The test sets it to "xwiki" which is wrong for this test.
409  1 this.oldcore.getXWikiContext().setOriginalWikiId("subwiki");
410   
411  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.home", "http://mainwiki.mywiki.tld/");
412  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
413  1 urlFactory.init(this.oldcore.getXWikiContext());
414  1 assertEquals("http://mainwiki.mywiki.tld/",
415    urlFactory.getServerURL("xwiki", this.oldcore.getXWikiContext()).toString());
416    }
417   
418    /**
419    * Proves that from a virtual wiki, URLs generated to point to the main wiki will use xwiki.home. see: XWIKI-5981
420    */
 
421  1 toggle @Test
422    public void createURLWhenWikiDotHomeParameterFromVirtualWiki() throws MalformedURLException
423    {
424  1 this.oldcore.getXWikiContext()
425    .setURL(new URL("http://virtual1.mywiki.tld/xwiki/view/InitialSpace/InitialPage"));
426  1 this.oldcore.getXWikiContext().setWikiId("subwiki");
427   
428    // This is called by XWiki#getXWiki() and is set to whatever the user asks for.
429    // The test sets it to "xwiki" which is wrong for this test.
430  1 this.oldcore.getXWikiContext().setOriginalWikiId("subwiki");
431   
432  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.home", "http://mainwiki.mywiki.tld/");
433  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.virtual", "1");
434   
435    // Reinitialize the URL factory to take into account the new request URL.
436  1 urlFactory.init(this.oldcore.getXWikiContext());
437   
438    // No wiki passed, assume same wiki. we should expect it to return http://virtual1.mywiki.tld/
439  1 URL url =
440    urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", null, this.oldcore.getXWikiContext());
441  1 assertEquals(new URL("http://virtual1.mywiki.tld/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
442    // We are already in virtual1 so it should be a relative reference.
443  1 assertEquals("/xwiki/bin/view/Space/Page?param1=1#anchor",
444    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
445   
446    // Pass "xwiki" as the wiki, expect it to return the main wiki as set in the xwiki.home parameter.
447  1 url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
448    this.oldcore.getXWikiContext());
449  1 assertEquals(new URL("http://mainwiki.mywiki.tld/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
450  1 assertEquals("http://mainwiki.mywiki.tld/xwiki/bin/view/Space/Page?param1=1#anchor",
451    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
452    }
453   
454    /**
455    * When getServerURL is called on a resource from the main wiki, the user is in the main wiki, and xwiki.home is
456    * set, xwiki.home should be returned. see: XWIKI-5981
457    */
 
458  1 toggle @Test
459    public void getServerURLNonVirtualModeWithXWikiDotHomeEnabled() throws MalformedURLException
460    {
461  1 this.oldcore.getXWikiContext().setURL(new URL("http://127.0.0.1:8080/xwiki/view/InitialSpace/InitialPage"));
462  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.home", "http://mainwiki.mywiki.tld/");
463  1 urlFactory.init(this.oldcore.getXWikiContext());
464    // TODO: Fix getServerURL() so that is is consistent about returning a trailing / or not.
465  1 assertEquals("http://mainwiki.mywiki.tld",
466    urlFactory.getServerURL("xwiki", this.oldcore.getXWikiContext()).toString());
467  1 assertEquals("http://mainwiki.mywiki.tld",
468    urlFactory.getServerURL(null, this.oldcore.getXWikiContext()).toString());
469    }
470   
471    /**
472    * Proves that in a single wiki instance, URLs are always generated using xwiki.home if present. see: XWIKI-5981
473    */
 
474  1 toggle @Test
475    public void createURLWhenXWikiDotHomeParameterNonVirtualMode() throws MalformedURLException
476    {
477    // Some proxies will modify the host field without adding a x-forwarded-host field,
478    // Using xwiki.home we should be able to make it work anyway.
479  1 this.oldcore.getXWikiContext().setURL(new URL("http://localhost:8080/xwiki/view/InitialSpace/InitialPage"));
480  1 this.oldcore.getMockXWikiCfg().setProperty("xwiki.home", "http://mainwiki.mywiki.tld/");
481    // Reinitialize the URL factory to take into account the new request URL.
482  1 urlFactory.init(this.oldcore.getXWikiContext());
483   
484    // No wiki passed, assume main wiki. we should expect it to return mainwiki.mywiki.tld and not
485    // xwiki.mywiki.tld.
486  1 URL url =
487    urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", null, this.oldcore.getXWikiContext());
488  1 assertEquals(new URL("http://mainwiki.mywiki.tld/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
489  1 assertEquals("/xwiki/bin/view/Space/Page?param1=1#anchor",
490    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
491   
492    // Pass "xwiki" as the wiki, expect same result.
493  1 url = urlFactory.createURL("Space", "Page", "view", "param1=1", "anchor", "xwiki",
494    this.oldcore.getXWikiContext());
495  1 assertEquals(new URL("http://mainwiki.mywiki.tld/xwiki/bin/view/Space/Page?param1=1#anchor"), url);
496  1 assertEquals("/xwiki/bin/view/Space/Page?param1=1#anchor",
497    urlFactory.getURL(url, this.oldcore.getXWikiContext()));
498    }
499   
500    /**
501    * Verify that jsessionid is removed from URL.
502    */
 
503  1 toggle @Test
504    public void normalizeURL() throws Exception
505    {
506  1 assertEquals(new URL("http://www.xwiki.org/xwiki/bin/view/Blog/Bug+Fixing+Day+35?language=en"),
507    XWikiServletURLFactory.normalizeURL("http://www.xwiki.org/xwiki/bin/view/Blog/Bug+Fixing+Day+35"
508    + ";jsessionid=0AF95AFB8997826B936C0397DF6A0C7F?language=en", this.oldcore.getXWikiContext()));
509    }
510   
 
511  1 toggle @Test
512    public void createURLWithNestedSpaces() throws Exception
513    {
514  1 URL url = this.urlFactory.createURL("Space1.Space2", "Page", this.oldcore.getXWikiContext());
515  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/view/Space1/Space2/Page"), url);
516    }
517   
 
518  1 toggle @Test
519    public void createAttachmentURLWhenViewRevAndRevSpecifiedAndIsNotContextDoc() throws Exception
520    {
521  1 XWikiContext xwikiContext = this.oldcore.getXWikiContext();
522  1 xwikiContext.put("rev", "1.0");
523  1 xwikiContext.setAction("viewrev");
524  1 xwikiContext.setDoc(new XWikiDocument(new DocumentReference("currentwiki", "currentspace", "currentpage")));
525  1 URL url = this.urlFactory.createAttachmentURL("file", "space", "page", "viewrev", null, xwikiContext);
526  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/viewrev/space/page/file"), url);
527    }
528   
 
529  1 toggle @Test
530    public void createAttachmentURLWhenViewRevAndRevSpecifiedAndIsContextDocAndAttachmentDoesntExist()
531    throws Exception
532    {
533  1 XWikiContext xwikiContext = this.oldcore.getXWikiContext();
534  1 xwikiContext.put("rev", "1.0");
535  1 xwikiContext.setAction("viewrev");
536   
537  1 XWikiDocument document = new XWikiDocument(new DocumentReference("currentwiki", "currentspace", "currentpage"));
538  1 this.oldcore.getSpyXWiki().saveDocument(document, this.oldcore.getXWikiContext());
539   
540  1 xwikiContext.setDoc(document);
541  1 xwikiContext.setWikiId("currentwiki");
542   
543  1 URL url =
544    this.urlFactory.createAttachmentURL("file", "currentspace", "currentpage", "viewrev", null, xwikiContext);
545  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/viewattachrev/currentspace/currentpage/file"), url);
546    }
547   
 
548  1 toggle @Test
549    public void createURLWhenShowViewActionFalse() throws Exception
550    {
551  1 doReturn(false).when(this.oldcore.getSpyXWiki()).showViewAction(any(XWikiContext.class));
552   
553  1 URL url = this.urlFactory.createURL("Space", "Page", "view", this.oldcore.getXWikiContext());
554  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/Space/Page"), url);
555    }
556   
 
557  1 toggle @Test
558    public void createURLWhenShowViewActionFalseAndSpaceIsNamedAfterAnAction() throws Exception
559    {
560  1 doReturn(false).when(this.oldcore.getSpyXWiki()).showViewAction(any(XWikiContext.class));
561   
562  1 URL url = this.urlFactory.createURL("view.space2", "page", "view", this.oldcore.getXWikiContext());
563  1 assertEquals(new URL("http://127.0.0.1/xwiki/bin/view/view/space2/page"), url);
564    }
565   
 
566  1 toggle @Test
567    public void createResourceURL() throws Exception
568    {
569    // Verify that the URL factory encodes each path segment.
570  1 URL url = this.urlFactory.createResourceURL("o;ne/t?w&o/t=hr#e e", false, this.oldcore.getXWikiContext());
571  1 assertEquals(new URL("http://127.0.0.1/xwiki/resources/o;ne/t%3Fw&o/t=hr%23e%20e"), url);
572    }
573   
 
574  1 toggle @Test
575    public void createURLWhenCharactersNeedToBeEncoded() throws Exception
576    {
577    // Note: The query string is not encoded, and used as is. It's the responsibility of the caller to
578    // url-encode it.
579    // See XWikiServletURLFactory#encodeWithinPath() and XWikiServletURLFactory#encodeWithinQuery() for explanations
580    // Note: We also verify that the single quote is encoded since otherwise that could cause problems in HTML when
581    // the returned URL is used in the HREF attribute of a A tag (when using single quote delimiters).
582  1 URL url = this.urlFactory.createURL("a b.c+d'", "e f", "view", "g=h%20i", "j k+l", this.oldcore.getXWikiContext());
583   
584  1 assertEquals("http://127.0.0.1/xwiki/bin/view/a%20b/c%2Bd%27/e%20f?g=h%20i#j%20k%2Bl", url.toString());
585    }
586    }