1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File XWikiServletURLFactoryTest.java

 

Code metrics

0
155
33
1
562
375
33
0.21
4.7
33
1

Classes

Class Line # Actions
XWikiServletURLFactoryTest 60 155 0% 33 0
1.0100%
 

Contributing tests

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