File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\jenkins\model\JenkinsGetRootUrlTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2013 RedHat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package jenkins.model;
25
26 import static org.hamcrest.CoreMatchers.equalTo;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.powermock.api.mockito.PowerMockito.mock;
29 import static org.powermock.api.mockito.PowerMockito.mockStatic;
30 import static org.powermock.api.mockito.PowerMockito.when;
31
32 import java.net.URL;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.jvnet.hudson.test.Bug;
38 import org.kohsuke.stapler.Stapler;
39 import org.kohsuke.stapler.StaplerRequest;
40 import org.mockito.Mockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({JenkinsLocationConfiguration.class, Stapler.class})
46 public class JenkinsGetRootUrlTest {
47
48 private Jenkins jenkins;
49 private JenkinsLocationConfiguration config;
50
51 @Before
52 public void setUp() {
53
54 jenkins = mock(Jenkins.class, Mockito.CALLS_REAL_METHODS);
55 config = mock(JenkinsLocationConfiguration.class);
56
57 mockStatic(JenkinsLocationConfiguration.class);
58 when(JenkinsLocationConfiguration.get()).thenReturn(config);
59
60 mockStatic(Stapler.class);
61 }
62
63 @Test
64 public void getConfiguredRootUrl() {
65
66 configured("http://configured.host");
67
68 rootUrlIs("http://configured.host/");
69 }
70
71 @Test
72 public void getAccessedRootUrl() {
73
74 accessing("https://real.host/jenkins/");
75
76 rootUrlIs("https://real.host/jenkins/");
77 }
78
79 @Test
80 public void preferConfiguredOverAccessed() {
81
82 configured("http://configured.host/");
83 accessing("http://real.host/");
84
85 rootUrlIs("http://configured.host/");
86 }
87
88 @Bug(16368)
89 @Test
90 public void doNotInheritProtocolWhenDispatchingRequest() {
91
92 configured("http://configured.host/");
93 accessing("https://real.host/");
94
95 rootUrlIs("http://configured.host/");
96 }
97
98 @Bug(16511)
99 @Test
100 public void doNotInheritProtocolWhenDispatchingRequest2() {
101 configured("https://ci/jenkins/");
102 accessing("http://localhost:8080/");
103 rootUrlIs("https://ci/jenkins/");
104 }
105
106 private void rootUrlIs(final String expectedRootUrl) {
107
108 assertThat(jenkins.getRootUrl(), equalTo(expectedRootUrl));
109 }
110
111 private void configured(final String configuredHost) {
112
113 when(config.getUrl()).thenReturn(configuredHost);
114 }
115
116 private void accessing(final String realUrl) {
117
118 final URL url = getUrl(realUrl);
119
120 final StaplerRequest req = mock(StaplerRequest.class);
121 when(req.getScheme()).thenReturn(url.getProtocol());
122 when(req.getServerName()).thenReturn(url.getHost());
123 when(req.getServerPort()).thenReturn(url.getPort() == -1 ? 80 : url.getPort());
124 when(req.getContextPath()).thenReturn(url.getPath().replaceAll("/$", ""));
125
126 when(Stapler.getCurrentRequest()).thenReturn(req);
127 }
128
129 private URL getUrl(final String realUrl) {
130
131 try {
132
133 return new URL(realUrl);
134 } catch(Exception ex) {
135
136 throw new RuntimeException(ex);
137 }
138 }
139 }
140