Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 1797236

Browse files
committed
Correcting custom keystore
Using custom keystore even if "Accept Any Certificate" is disabled.
1 parent 0965b48 commit 1797236

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Changelog of Pull Request Notifier for Bitbucket.
66
### No issue
77
doc
88

9-
[c83497209554f52](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/c83497209554f52) Tomas Bjerre *2019-10-01 14:33:50*
9+
[0965b48f845c45b](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/0965b48f845c45b) Tomas Bjerre *2019-10-01 14:37:09*
1010

1111
Create FUNDING.yml
1212

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,4 @@ Changelog of Pull Request Notifier for Bitbucket.
337337
<quick.reload.version>2.0.0</quick.reload.version>
338338
<amps.version>8.0.2</amps.version>
339339
</properties>
340-
</project>
340+
</project>

src/main/java/se/bjurr/prnfb/http/UrlInvoker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ private SSLContext newSslContext() throws Exception {
307307
final SSLContextBuilder sslContextBuilder = SSLContexts.custom();
308308
if (this.shouldAcceptAnyCertificate) {
309309
doAcceptAnyCertificate(sslContextBuilder);
310-
if (this.clientKeyStore.getKeyStore().isPresent()) {
311-
sslContextBuilder.loadKeyMaterial(
312-
this.clientKeyStore.getKeyStore().get(), this.clientKeyStore.getPassword());
313-
}
310+
}
311+
if (this.clientKeyStore.getKeyStore().isPresent()) {
312+
sslContextBuilder.loadKeyMaterial(
313+
this.clientKeyStore.getKeyStore().get(), this.clientKeyStore.getPassword());
314314
}
315315

316316
return sslContextBuilder.build();

src/test/java/se/bjurr/prnfb/http/UrlInvokerTest.java

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.google.common.base.Optional.of;
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
67
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.DELETE;
78
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.GET;
89
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.POST;
@@ -31,11 +32,12 @@ public void before() {
3132
this.urlInvoker =
3233
new UrlInvoker() {
3334
@Override
34-
HttpResponse doInvoke(HttpRequestBase httpRequest, HttpClientBuilder builder) {
35+
HttpResponse doInvoke(
36+
final HttpRequestBase httpRequest, final HttpClientBuilder builder) {
3537
UrlInvokerTest.this.httpRequestBase = httpRequest;
3638
try {
3739
return new HttpResponse(new URI("http://fake.com"), 200, "");
38-
} catch (URISyntaxException e) {
40+
} catch (final URISyntaxException e) {
3941
e.printStackTrace();
4042
return null;
4143
}
@@ -60,7 +62,7 @@ public void testThatHeadersAreAdded() {
6062

6163
@Test
6264
public void testThatHeadersAreAddedForBasicAuth() throws Exception {
63-
PrnfbNotification notification =
65+
final PrnfbNotification notification =
6466
prnfbNotificationBuilder() //
6567
.withUrl("http://url.com/") //
6668
.withUser("user") //
@@ -82,7 +84,7 @@ public void testThatHeadersAreAddedForBasicAuth() throws Exception {
8284

8385
@Test
8486
public void testThatHttpEntityEnclosingRequestBaseCanBeCreatedAsPOSTWithoutContent() {
85-
HttpRequestBase response =
87+
final HttpRequestBase response =
8688
this.urlInvoker //
8789
.withMethod(POST) //
8890
.newHttpRequestBase();
@@ -95,22 +97,22 @@ public void testThatHttpEntityEnclosingRequestBaseCanBeCreatedAsPOSTWithoutConte
9597

9698
@Test
9799
public void testThatHttpEntityEnclosingRequestBaseCanBeCreatedAsPUTWithContent() {
98-
HttpRequestBase response =
100+
final HttpRequestBase response =
99101
this.urlInvoker //
100102
.withMethod(PUT) //
101103
.withPostContent(Optional.of("some content")) //
102104
.newHttpRequestBase();
103105

104106
assertThat(response.getMethod()) //
105107
.isEqualTo(PUT.name());
106-
HttpEntityEnclosingRequestBase c = (HttpEntityEnclosingRequestBase) response;
108+
final HttpEntityEnclosingRequestBase c = (HttpEntityEnclosingRequestBase) response;
107109
assertThat(c.getEntity().getContentLength()) //
108110
.isGreaterThan(0);
109111
}
110112

111113
@Test
112114
public void testThatHttpRequestBaseCanBeCreatedWithDelete() {
113-
HttpRequestBase response =
115+
final HttpRequestBase response =
114116
this.urlInvoker //
115117
.withMethod(DELETE) //
116118
.newHttpRequestBase();
@@ -123,43 +125,37 @@ public void testThatHttpRequestBaseCanBeCreatedWithDelete() {
123125

124126
@Test
125127
public void testThatNoneSslCanBeConfigured() throws Exception {
126-
HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
128+
final HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
127129

128130
this.urlInvoker //
129131
.withUrlParam("http://url.com/") //
130132
.configureSsl(mockedBuilder);
131-
132-
// verify(mockedBuilder, times(0)).setSSLSocketFactory(Matchers.any());
133133
}
134134

135135
@Test
136136
public void testThatProxyIsConfiguredIfThereIsAHostAndPort() throws Exception {
137-
HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
137+
final HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
138138

139139
this.urlInvoker //
140140
.withUrlParam("http://url.com/") //
141141
.withProxyServer(of("http://proxy.com/")) //
142142
.withProxyPort(123) //
143143
.configureProxy(mockedBuilder);
144-
145-
// verify(mockedBuilder, times(1)).setProxy(Matchers.any());
146144
}
147145

148146
@Test
149147
public void testThatProxyIsNotConfiguredIfThereIsNoHost() throws Exception {
150-
HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
148+
final HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
151149

152150
this.urlInvoker //
153151
.withUrlParam("http://url.com/") //
154152
.withProxyPort(123) //
155153
.configureProxy(mockedBuilder);
156-
157-
// verify(mockedBuilder, times(0)).setProxy(Matchers.any());
158154
}
159155

160156
@Test
161157
public void testThatProxyUserIsConfiguredIfItIsSet() throws Exception {
162-
HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
158+
final HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
163159

164160
this.urlInvoker //
165161
.withUrlParam("http://url.com/") //
@@ -168,37 +164,31 @@ public void testThatProxyUserIsConfiguredIfItIsSet() throws Exception {
168164
.withProxyUser(of("u")) //
169165
.withProxyPassword(of("p")) //
170166
.configureProxy(mockedBuilder);
171-
172-
// verify(mockedBuilder,
173-
// times(1)).setDefaultCredentialsProvider(Matchers.any());
174-
// verify(mockedBuilder, times(1)).setProxy(Matchers.any());
175167
}
176168

177169
@Test
178170
public void testThatProxyUserIsNotConfiguredIfNoPasswordSet() throws Exception {
179-
HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
171+
final HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
180172

181173
this.urlInvoker //
182174
.withUrlParam("http://url.com/") //
183175
.withProxyServer(of("http://proxy.com/")) //
184176
.withProxyPort(123) //
185177
.withProxyUser(of("u")) //
186178
.configureProxy(mockedBuilder);
187-
188-
// verify(mockedBuilder,
189-
// times(0)).setDefaultCredentialsProvider(Matchers.any());
190-
// verify(mockedBuilder, times(1)).setProxy(Matchers.any());
191179
}
192180

193181
@Test
194182
public void testThatSslCanBeConfigured() throws Exception {
195-
HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
183+
final HttpClientBuilder mockedBuilder = mock(HttpClientBuilder.class);
184+
final ClientKeyStore clientKeyStore = mock(ClientKeyStore.class);
185+
186+
when(clientKeyStore.getKeyStore()).thenReturn(Optional.absent());
196187

197188
this.urlInvoker //
198189
.withUrlParam("https://url.com/") //
190+
.withClientKeyStore(clientKeyStore) //
199191
.configureSsl(mockedBuilder);
200-
201-
// verify(mockedBuilder).setSSLSocketFactory(Matchers.any());
202192
}
203193

204194
@Test

0 commit comments

Comments
 (0)