|
6 | 6 | import com.github.tomakehurst.wiremock.extension.ResponseTransformer; |
7 | 7 | import com.github.tomakehurst.wiremock.http.Request; |
8 | 8 | import com.github.tomakehurst.wiremock.http.Response; |
| 9 | +import org.apache.commons.io.IOUtils; |
9 | 10 | import org.junit.After; |
10 | 11 | import org.junit.Assert; |
11 | 12 | import org.junit.Before; |
12 | 13 | import org.junit.Rule; |
13 | 14 | import org.kohsuke.github.junit.WireMockRule; |
14 | 15 |
|
| 16 | +import java.io.File; |
| 17 | +import java.io.FileInputStream; |
| 18 | +import java.io.IOException; |
15 | 19 | import java.util.Properties; |
16 | 20 |
|
17 | 21 | import static com.github.tomakehurst.wiremock.client.WireMock.*; |
|
21 | 25 | */ |
22 | 26 | public abstract class AbstractGitHubApiWireMockTest extends Assert { |
23 | 27 |
|
24 | | - // By default the wiremock tests will, run without proxy or taking a snapshot. |
| 28 | + // By default the wiremock tests will run without proxy or taking a snapshot. |
25 | 29 | // The tests will use only the stubbed data and will fail if requests are made for missing data. |
26 | 30 | // You can use the proxy without taking a snapshot while writing and debugging tests. |
27 | 31 | // You cannot take a snapshot without proxying. |
28 | 32 | protected final static boolean takeSnapshot = System.getProperty("test.github.takeSnapshot", "false") != "false"; |
29 | 33 | protected final static boolean useProxy = takeSnapshot || System.getProperty("test.github.useProxy", "false") != "false"; |
| 34 | + private final GitHubBuilder githubBuilder = createGitHubBuilder(); |
30 | 35 |
|
31 | 36 | public final static String STUBBED_USER_LOGIN = "placeholder-user"; |
32 | 37 | public final static String STUBBED_USER_PASSWORD = "placeholder-password"; |
@@ -67,33 +72,63 @@ public String getName() { |
67 | 72 | }) |
68 | 73 | ); |
69 | 74 |
|
70 | | - @Before |
71 | | - public void wireMockSetup() throws Exception { |
| 75 | + private static GitHubBuilder createGitHubBuilder() { |
72 | 76 |
|
73 | | - GitHubBuilder builder = GitHubBuilder.fromEnvironment() |
74 | | - .withEndpoint("http://localhost:" + githubApi.port()) |
75 | | - .withRateLimitHandler(RateLimitHandler.FAIL); |
| 77 | + GitHubBuilder builder = new GitHubBuilder(); |
| 78 | + |
| 79 | + try { |
| 80 | + File f = new File(System.getProperty("user.home"), ".github.kohsuke2"); |
| 81 | + if (f.exists()) { |
| 82 | + Properties props = new Properties(); |
| 83 | + FileInputStream in = null; |
| 84 | + try { |
| 85 | + in = new FileInputStream(f); |
| 86 | + props.load(in); |
| 87 | + } finally { |
| 88 | + IOUtils.closeQuietly(in); |
| 89 | + } |
| 90 | + // use the non-standard credential preferentially, so that developers of this library do not have |
| 91 | + // to clutter their event stream. |
| 92 | + builder = GitHubBuilder.fromProperties(props); |
| 93 | + } else { |
| 94 | + builder = GitHubBuilder.fromCredentials(); |
| 95 | + } |
| 96 | + } catch (IOException e) { |
| 97 | + } |
| 98 | + |
| 99 | + if (!useProxy) { |
| 100 | + // This sets the user and password to a placeholder for wiremock testing |
| 101 | + // This makes the tests believe they are running with permissions |
| 102 | + // The recorded stubs will behave like they running with permissions |
| 103 | + builder.withPassword(STUBBED_USER_LOGIN, STUBBED_USER_PASSWORD); |
| 104 | + } |
76 | 105 |
|
| 106 | + return builder.withRateLimitHandler(RateLimitHandler.FAIL); |
| 107 | + } |
| 108 | + |
| 109 | + protected GitHubBuilder getGitHubBuilder() { |
| 110 | + return githubBuilder; |
| 111 | + } |
77 | 112 |
|
| 113 | + @Before |
| 114 | + public void wireMockSetup() throws Exception { |
78 | 115 | if(useProxy) { |
79 | 116 | githubApi.stubFor( |
80 | 117 | proxyAllTo("https://api.github.com/") |
81 | 118 | .atPriority(100) |
82 | 119 | ); |
83 | 120 | } else { |
84 | | - // This sets the user and password to a placeholder for wiremock testing |
85 | | - // This makes the tests believe they are running with permissions |
86 | | - // The recorded stubs will behave like they running with permissions |
87 | | - builder.withPassword(STUBBED_USER_LOGIN, STUBBED_USER_PASSWORD); |
88 | | - |
89 | 121 | // Just to be super clear |
90 | 122 | githubApi.stubFor( |
91 | 123 | any(urlPathMatching(".*")) |
92 | 124 | .willReturn(status(500).withBody("Stubbed data not found. Set test.github.use-proxy to have WireMock proxy to github")) |
93 | 125 | .atPriority(100)); |
94 | 126 | } |
95 | 127 |
|
96 | | - gitHub = builder.build(); |
| 128 | + |
| 129 | + gitHub = getGitHubBuilder() |
| 130 | + .withEndpoint("http://localhost:" + githubApi.port()) |
| 131 | + .build(); |
97 | 132 | } |
98 | 133 |
|
99 | 134 | @After |
|
0 commit comments