-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathHttpWorkerTest.java
More file actions
114 lines (90 loc) · 4.54 KB
/
HttpWorkerTest.java
File metadata and controls
114 lines (90 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package jenkins.plugins.office365connector;
import hudson.ProxyConfiguration;
import hudson.util.ReflectionUtils;
import jenkins.model.Jenkins;
import jenkins.plugins.office365connector.workflow.AbstractTest;
import org.apache.commons.httpclient.HttpClient;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@PowerMockIgnore("jdk.internal.reflect.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Jenkins.class, HttpWorker.class})
public class HttpWorkerTest extends AbstractTest {
@Before
public void setUp() {
Jenkins mockJenkins = mock(Jenkins.class);
mockStatic(Jenkins.class);
Mockito.when(Jenkins.get()).thenReturn(mockJenkins);
}
@Test
public void HttpWorker_getHttpClient_NoJenkinsProxy() throws NoSuchMethodException {
// given
// from @Before
ProxyConfiguration thisPluginProxy = new ProxyConfiguration("", 0, "", "");
HttpWorker httpWorker = new HttpWorker("http://127.0.0.1", "{}", 30, System.out, thisPluginProxy);
Method method = HttpWorker.class.getDeclaredMethod("getHttpClient");
method.setAccessible(true);
// when
HttpClient httpClient = (HttpClient) ReflectionUtils.invokeMethod(method, httpWorker);
// then
assertThat(httpClient.getHostConfiguration().getProxyHost()).isNull();
assertThat(httpClient.getHostConfiguration().getProxyPort()).isEqualTo(-1);
}
@Test
public void HttpWorker_getHttpClient_JenkinsProxy() throws NoSuchMethodException {
// given
// from @Before
ProxyConfiguration thisPluginProxy = new ProxyConfiguration("", 0, "", "");
Jenkins jenkins = Jenkins.get();
ProxyConfiguration proxyConfiguration = new ProxyConfiguration("name", 123, null, null, "*mockwebsite.com*");
jenkins.proxy = proxyConfiguration;
HttpWorker httpWorker = new HttpWorker("http://127.0.0.1", "{}", 30, System.out, thisPluginProxy);
Method method = HttpWorker.class.getDeclaredMethod("getHttpClient");
method.setAccessible(true);
// when
HttpClient httpClient = (HttpClient) ReflectionUtils.invokeMethod(method, httpWorker);
// then
assertThat(httpClient.getHostConfiguration().getProxyHost()).isEqualTo(proxyConfiguration.getName());
assertThat(httpClient.getHostConfiguration().getProxyPort()).isEqualTo(proxyConfiguration.getPort());
}
@Test
public void HttpWorker_getHttpClient_JenkinsProxy_Ignored() throws NoSuchMethodException {
// given
// from @Before
ProxyConfiguration thisPluginProxy = new ProxyConfiguration("", 0, "", "");
Jenkins jenkins = Jenkins.get();
ProxyConfiguration proxyConfiguration = new ProxyConfiguration("name", 123, null, null, "*mockwebsite.com*");
jenkins.proxy = proxyConfiguration;
HttpWorker httpWorker = new HttpWorker("http://mockwebsite.com", "{}", 30, System.out, thisPluginProxy);
Method method = HttpWorker.class.getDeclaredMethod("getHttpClient");
method.setAccessible(true);
// when
HttpClient httpClient = (HttpClient) ReflectionUtils.invokeMethod(method, httpWorker);
// then
assertThat(httpClient.getHostConfiguration().getProxyHost()).isNull();
assertThat(httpClient.getHostConfiguration().getProxyPort()).isEqualTo(-1);
}
@Test
public void HttpWorker_getHttpClient_PluginProxy() throws NoSuchMethodException {
// given
// from @Before
ProxyConfiguration thisPluginProxy = new ProxyConfiguration("10.0.0.1", 12345, "", "");
HttpWorker httpWorker = new HttpWorker("http://127.0.0.1", "{}", 30, System.out, thisPluginProxy);
Method method = HttpWorker.class.getDeclaredMethod("getHttpClient");
method.setAccessible(true);
// when
HttpClient httpClient = (HttpClient) ReflectionUtils.invokeMethod(method, httpWorker);
// then
assertThat(httpClient.getHostConfiguration().getProxyHost()).isEqualTo(thisPluginProxy.getName());
assertThat(httpClient.getHostConfiguration().getProxyPort()).isEqualTo(thisPluginProxy.getPort());
}
}